while (cyclesc > 0) and (FC = 1 or FC = 3 or FC = 4) do
--dostuff
end
Lua 101 or even coding 101 I'm sure so forgive me - what is best way to write this - nested while loops? seems a waste - is there a way to have multiple conditions in one line of a while loop?
In your example, you've got
while (cyclesc > 0) and (FC = 1 or FC = 3 or FC = 4) do
--dostuff
end
which almost works, but you've used = instead of ==. = is the variable assignment operator, and == compares two values.
Your code should be
while (cyclesc > 0) and (FC == 1 or FC == 3 or FC == 4) do
--dostuff
end
Community wiki as this was solved in the comments
Related
I am manipulating strings.
I want the output string to be only between 2 specific characters (= and o)
I can do this by repeat this twice:
For f = 1 To Len(line5)
If Mid(line5, f, 1) = "=" Then
line5 = Mid(line5, f, Len(line5) - f + 1)
line5 = line5_out
End If
One time for = and one for o
Is there a quicker way to do this?
There are multiple ways to do it, the "best" way depends on what exactly you need.
Besides those comments, here are two more ways to do it:
'Delete everything behind o and infront of =
YourString = YourString.Remove(YourString.LastIndexOf("o") + 1, YourString.Length - YourString.LastIndexOf("o") - 1).Remove(0, YourString.IndexOf("="))
'Get part of string between = and o
YourString = YourString.Substring(IndexOf("="), YourString.LastIndexOf("o") + 1 - YourString.IndexOf("="))
I am trying to figure out another way to write this line. Currently, I have it to where if any of the ranges AA2:AA7 = 1 then call code OneLineItem. Issue is, the parameter I need set is if only one of those cells equals 1 and only one other cell to be greater than 1. I.e. AA2 = 1 and AA7=200 (for example). A problem i'm running into is that AA2 = 1, AA3 = 100, AA7 = 200. However I just need one cell to equal 1 and another cell to be >1 and everything else to be 0. If that criteria is met, then call code OneLineItem. Thank You.
If ActiveSheet.Range("AA2") = 1 Or ActiveSheet.Range("AA3") = 1 Or
ActiveSheet.Range("AA4") = 1 Or ActiveSheet.Range("AA5") = 1 Or
ActiveSheet.Range("AA6") = 1 Or _
ActiveSheet.Range("AA7") = 1 Then
Call OneLineItem
Else
There are 6 numbers so:
1 should be 1
1 should be greater than 1
4 should be 0
so we can use COUNTIF() to find if it follows the pattern
Dim OneTrue As Boolean
Dim MoreTrue As Boolean
Dim RestTrue As Boolean
RestTrue = Application.WorksheetFunction.CountIf([AA2:AA7], 0) = 4 [AA2:AA7].Cells.Count - 2
OneTrue = Application.WorksheetFunction.CountIf([AA2:AA7], 1) = 1
MoreTrue = Application.WorksheetFunction.CountIf([AA2:AA7], ">1") = 1
If RestTrue And OneTrue And MoreTrue Then
Call OneLineItem
End If
Another method would be to nest the IF:
IF Application.WorksheetFunction.CountIf([AA2:AA7], 0) = [AA2:AA7].Cells.Count - 2 Then
IF Application.WorksheetFunction.CountIf([AA2:AA7], 1) = 1 Then
'we do not need the third, If the others are true then the last must be true.
'Unless you can have negative numbers. Then you can add the third.
Call OneLineItem
End If
End If
The advantage to the second is that it only does the COUNTIFs necessary till it find a False return, then it does not do any more. while the first does all three no matter what.
I want to compare if n1-10 one of them is = box
then write nx1 and ny1 and so on ...
what could I do to compare them faster .
the below way will take years from me
box=readString('[[[[[_name]+11c]+670]+394]+2fc]+1c',20)
n1=readString('[_player]+184',20)
n2=readString('[_player]+f94',20)
n3=readString('[_player]+1da4',20)
nx1= readInteger('[_player]+48d8')
nx2= readInteger('[_player]+56e8')
nx3= readInteger('[_player]+64f8')
ny1= readInteger('[_player]+48dc')
ny2= readInteger('[_player]+56ec')
ny3= readInteger('[_player]+64fc')
if box =n1 or n2 or n3 --lets say n1=box
then writeInteger('[_player]+28c',n1x)
writeInteger('[_player]+28c',n1y)
if box =n1 or n2 or n3 --lets say n2=box
then writeInteger('[_player]+28c',n2x)
writeInteger('[_player]+28c',n2y)
You implementation is not good practive here. You'd better store these variable in to an array like
ns[1] = ...
ns[2] = ...
...
nys[1] = ...
nys[2] = ...
then
key = -1
for k,v in na do
if box == v then
key = k
end
end
if key == -1 then
print("no match values")
else
writeInteger('[_player]+28c',nys[key])
writeInteger('[_player]+28c',nys[key])
end
I'm trying to plot a function of two variables with pyplot in Julia. The working starting-point is the following (found here at StackOverflow):
function f(z,t)
return z*t
end
z = linspace(0,5,11)
t = linspace(0,40,4)
for tval in t
plot(z, f(z, tval))
end
show()
This works right for me and is giving me exactly what I wanted:
a field of lines.
My own functions are as follows:
## needed functions ##
const gamma_0 = 6
const Ksch = 1.2
const Kver = 1.5
function Kvc(vc)
if vc <= 0
return 0
elseif vc < 20
return (100/vc)^0.1
elseif vc < 100
return 2.023/(vc^0.153)
elseif vc == 100
return 1
elseif vc > 100
return 1.380/(vc^0.07)
else
return 0
end
end
function Kgamma(gamma_t)
return 1-((gamma_t-gamma_0)/100)
end
function K(gamma_t, vc)
return Kvc(vc)*Kgamma(gamma_t)*Ksch*Kver
end
I've tried to plot them as follows:
i = linspace(0,45,10)
j = linspace(0,200,10)
for i_val in i
plot(i,K(i,j))
end
This gives me the following Error:
isless has no method matching isless(::Int64, ::Array{Float64,1})
while loading In[51], in expression starting on line 3
in Kvc at In[17]:2 in anonymous at no file:4
Obviously, my function cant deal with an array.
Next try:
i = linspace(0,200,11)
j = linspace(0,45,11)
for i_val in i
plot(i_val,map(K,i_val,j))
end
gives me a empty plot only with axes
Can anybody please give me a hint...
EDIT
A simpler example:
using PyPlot
function P(n,M)
return (M*n^3)/9550
end
M = linspace(1,5,5)
n = linspace(0,3000,3001)
for M_val in M
plot(n,P(n,M_val))
end
show()
Solution
OK, with your help I found this solution for the shortened example which works for me as intended:
function P(n,M)
result = Array(Float64, length(n))
for (idx, val) in enumerate(n)
result[idx] = (M*val^3)/9550
end
return result
end
n = linspace(0,3000,3001)
for M_val = 1:5
plot(n,P(n,M_val))
end
show()
This gives me what I wanted for this shortened example. The remainig question is: could it be done in a simpler more elegant way?
I'll try to apply it to the original example and post it when I'll succed.
I don't completely follow all the details of what you're trying to accomplish, but here are examples on how you can modify a couple of your functions so that they accept and return arrays:
function Kvc(vc)
result = Array(Float64, length(vc))
for (idx, val) in enumerate(vc)
if val <= 0
result[idx] = 0
elseif val < 20
result[idx] = (100/val)^0.1
elseif val < 100
result[idx] = 2.023/(val^0.153)
elseif val == 100
result[idx] = 1
elseif val > 100
result[idx] = 1.380/(val^0.07)
else
result[idx] = 0
end
end
return result
end
function Kgamma(gamma_t)
return ones(length(gamma_t))-((gamma_t - gamma_0)/100)
end
Also, for your loop, I think you probably want something like:
for i_val in i
plot(i_val,K(i_val,j))
end
rather than plot(i, K(i,j), as that would just print the same thing over and over.
< is defined for scalars. I think you need to broadcast it for arrays, i.e. use .<. Example:
julia> x = 2
2
julia> x < 3
true
julia> x < [3 4]
ERROR: MethodError: no method matching isless(::Int64, ::Array{Int64,2})
Closest candidates are:
isless(::Real, ::AbstractFloat)
isless(::Real, ::Real)
isless(::Integer, ::Char)
in <(::Int64, ::Array{Int64,2}) at .\operators.jl:54
in eval(::Module, ::Any) at .\boot.jl:234
in macro expansion at .\REPL.jl:92 [inlined]
in (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at .\event.jl:46
julia> x .< [3 4]
1x2 BitArray{2}:
true true
I have one 4 digit variable i wish to split into 4 seperate variables fromt the range 0000 to 9999
Local $Data ="Element 1|Element 2|Element 3|Element 4"
Local $arr = StringSplit($Data, "|")
If IsArray($arr) Then
$Imax = Ubound($arr)
For $i = 0 to $Imax -1
Next
EndIf
This is what I got so far
I want it to do this:
Lets say the bigvar = 2345
$BigVar=2345 Then
$SmallVar1 = 2
$SmallVar2 = 3
$SmallVar3 = 4
$SmallVar4 = 5
Also the bigvar changes all the time so i need it to keep reading of that
LOL to all the overkill answers
#include <Array.au3>
Local $parts = StringSplit("1574", "")
_ArrayDisplay($parts)
String Approach
If you simply want to split it, you can go with
#include <Array.au3>
Func _Split($BigVar)
Local $SmallVar[1] = [0]
For $i = 1 To StringLen($BigVar)
_ArrayAdd($SmallVar, StringMid($BigVar, $i, 1))
$SmallVar[0] += 1
Next
Return $SmallVar
EndFunc
$Array = _Split("2345")
_ArrayDisplay($Array)
Now you can use
$Array[0] = 4 ;Amount of digits
$Array[1] = 2
$Array[2] = 3
$Array[3] = 4
$Array[4] = 5
If the Number might be 123 and you want to interpret it as 0123 therefore $SmallVar[1] being 0 not 1, this method might fit your needs:
#include <Array.au3>
Func _Split($BigVar, $Digits = 0)
Local $SmallVar[1] = [0]
For $i = 1 To StringLen($BigVar)
_ArrayAdd($SmallVar, StringMid($BigVar, $i, 1))
$SmallVar[0] += 1
Next
If $Digits = 0 Then Return $SmallVar
If $SmallVar[0] >= $Digits Then
For $i = 1 To $SmallVar[0] - $Digits
_ArrayDelete($SmallVar, $i)
Next
$SmallVar[0] = $Digits
Return $SmallVar
EndIf
For $i = 1 To $Digits - $SmallVar[0]
_ArrayInsert($SmallVar, 1, 0)
Next
$SmallVar[0] = $Digits
Return $SmallVar
EndFunc
$Array = _Split("123", 4) ;4 being the amount of digits
_ArrayDisplay($Array)
The code example above still works with this version, since digits is an optional parameter, and leaving it out, _Split will act as it did before.
Just use modulo 10 division to get the single integers.
#include <array.au3>
Global $BigVar=2345
Global $TmpVar=$BigVar
Global $aResult[StringLen(String($BigVar))]
For $i=UBound($aResult)-1 To 0 Step -1
$aResult[$i] = Int(Mod($TmpVar, 10))
$TmpVar /= 10
Next
_ArrayDisplay($aResult)
Now you got an array that has each number of the big integer stored in a separate field.