Variable and string concat to select variable in lua - variables

I have a set of variables that holds quantity info and x to select the one I use. How can I concatenate the letter s and var x and have it read as s2 or s3 etc. The code I managed to find does not work.
x = 2
s1 = false
s2 = 64
s3 = 64
s4 = 64
s5 = 0
if s2 >= 0 then
x = 2
elseif s3 >= 0 then
x = 3
elseif s4 >= 0 then
x = 4
elseif s5 >= 0 then
x = 5
end
if turtle.placeDown() then
tryUp()
turtle.select(1)
_G["s"..x] = _G["s"..x] - 1
end

Why would you need to do that?
My suggestion to improve your code would be something like this:
local s = {false, 64, 64, 64, 0}
for i = 2, #s do
if s[i] >= 0 then
x = s[i]
end
end
if turtle.placeDown() then
tryUp()
turtle.select(1)
x = x-1
end
Using a loop makes the code somewhat neater, and there is no real need for you to use global variables. If you insist on using _G with the string concatenation with your original code, try this:
x = 2
s1 = false
s2 = 64
s3 = 64
s4 = 64
s5 = 0
if s2 >= 0 then
x = "2" --Notice the string here
elseif s3 >= 0 then
x = "3"
elseif s4 >= 0 then
x = "4"
elseif s5 >= 0 then
x = "5"
end
if turtle.placeDown() then
tryUp()
turtle.select(1)
_G["s"..x] = _G["s"..x] - 1
end
This replaces all the x values with strings instead of numbers, which was probably what was causing the error

Related

RAPIDS/NUMBA: Faster way to parallelize a for-loop on small data?

If I have data that easily fits into memory, but I need to iterate over it hundreds or thousands of times, is there a faster way?
For instance, if I have 400k datapoints and I need to iterate over it with 1000 filters. It is 4-10 times slower to to do a for-loop than it is to do a single operation on data that is length 400k*1000.
#setup
import cudf
import numpy as np
import cupy as cp
from numba import cuda
cp.seed = 42
signal_ranges = []
signal_len = 1000
data_size = 400000
for signal in range(signal_len):
s_low = cp.random.rand(1, dtype='float')
def get_high():
return cp.random.rand(1, dtype='float')
s_high = 0
while s_high <= s_low:
s_high = get_high()
signal_ranges.append((s_low,s_high))
EXAMPLE 1 - length 400k*1000
#cuda.jit
def filter_signal(in_col, s1, s2, out):
i = cuda.grid(1)
if i < in_col.size: # boundary guard
out[i] = 1 if in_col[i] <= s1 else -1 if in_col[i] >= s2 else 0
%%timeit -r 1
s1 = float(signal_ranges[0][0])
s2 = float(signal_ranges[0][1])
cu_df_big = cudf.DataFrame(cp.random.rand((data_size*signal_len)), columns=['in1'])
cu_df_big['0'] = 0
size = len(cu_df_big)
filter_signal.forall(size)(cu_df_big['in1'], s1, s2, cu_df_big['0'])
*314ms*
EXAMPLE 2 - 400k iterated 1000 times
#cuda.jit
def filter_signal(in_col, s1, s2, out):
i = cuda.grid(1)
if i < in_col.size: # boundary guard
out[i] = 1 if in_col[i] <= s1 else -1 if in_col[i] >= s2 else 0
%%timeit -r 1
cu_df = cudf.DataFrame(cp.random.rand((data_size)), columns=['in1'])
size = len(cu_df)
col_id = 0
for sigs in signal_ranges:
s1 = float(sigs[0])
s2 = float(sigs[1])
col = str(col_id)
cu_df[col] = 0
filter_signal.forall(size)(cu_df['in1'], s1, s2, cu_df[col])
col_id += 1
*2.3secs*

In which language we use to define a variable as $value = x

I have to know what language is used here. In which language do we define a variable as $value = x
Does this language have loop controls? I need this information, can any one provide?
Public sub Annual_Leave()
$Decimal_Output1 = 0 //variables
$Decimal_Output2 = 0
IF #ServiceMonths# = 1 OR #ServiceMonths# = 2 THEN
$Decimal_Output1 = 0
ELSEIF #ServiceMonths# => 3 AND #ServiceMonths# <= 12 THEN
IF #ServiceMonths# = 3 THEN
$Decimal_Output1 = 1.7
END IF
ELSE
$Decimal_Output1 = 21
$Decimal_Output2 = 0
END IF
End sub

project euler problem17 vb code bug

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
my answer is coming 21148. i cant find the mistake here is my code in vb
the answer should be 21124
Dim length As UInt32 = 11
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i = 1 To 999
If i / 10 < 1 Then
single_digit(i)
ElseIf i / 10 = 1 Then
ten()
ElseIf i / 10 > 1 And i / 10 < 2 Then
ele_twe_thi(i)
ElseIf i / 10 >= 2 And i / 10 < 10 Then
multiple_10(Math.Floor(i / 10) * 10)
single_digit(i Mod 10)
ElseIf i = 100 Then
single_digit(i / 100)
length += 7
ElseIf i Mod 100 >= 11 And i Mod 100 <= 19 Then
length += 10
single_digit(Math.Floor(i / 100))
ele_twe_thi(i Mod 100)
ElseIf i Mod 100 = 10 Then
length += 3
single_digit(Math.Floor(i / 100))
length += 10
Else
length += 10
single_digit(Math.Floor(i / 100))
multiple_10(Math.Floor((i Mod 100) / 10) * 10)
single_digit(i Mod 10)
End If
Next
MsgBox(length)
End Sub
Private Sub single_digit(num)
If num = 1 Then
length = length + 3
ElseIf num = 2 Then
length = length + 3
ElseIf num = 3 Then
length = length + 5
ElseIf num = 4 Then
length = length + 4
ElseIf num = 5 Then
length = length + 4
ElseIf num = 6 Then
length = length + 3
ElseIf num = 7 Then
length = length + 5
ElseIf num = 8 Then
length = length + 5
ElseIf num = 9 Then
length = length + 4
End If
End Sub
Private Sub ele_twe_thi(num)
If num = 11 Or num = 12 Then
length = length + 6
ElseIf num = 13 Or num = 14 Or num = 18 Or num = 19 Then
length = length + 8
ElseIf num = 17 Then
length = length + 9
ElseIf num = 15 Or num = 16 Then
length = length + 7
End If
End Sub
Private Sub multiple_10(num)
If num = 20 Then
length = length + 6
ElseIf num = 30 Then
length = length + 6
ElseIf num = 40 Then
length = length + 5
ElseIf num = 50 Then
length = length + 5
ElseIf num = 60 Then
length = length + 5
ElseIf num = 70 Then
length = length + 7
ElseIf num = 80 Then
length = length + 6
ElseIf num = 90 Then
length = length + 6
End If
End Sub
Private Sub ten()
length = length + 3
End Sub
every time the program reaches 100, 200, 300 etc. is it adding an "and" unnecessarily?

what to do with fraction as index number

given these inputs x = 4, S = [1 2 3 4 5 6 7 8 9 10], and n = 10
search (x,S,n) {
i = 1
j = n
while i < j {
m = [(i+j)/2]
if x > Sm then i=n+1
else j = m
end
if x = Si then location = i
else location = 0
This code is not from any particular language its just from my discrete math hw, but I'm confused as to what Sm would equal on the first iteration because m would be 11/2. If i use a fraction as the index do I round down? Is there a general rule for this? Am I making any sense? Help pls

do not understand if else block behaviour

I have the following code:
Sub Main()
Dim a As Integer = 8 * 60
Dim b As Integer
Dim c As Integer
If a < (6 * 60) Then
b = 0 And c = 0
ElseIf a >= 6 * 60 And a < 9 * 60 Then
b = 30 And c = 1
Else
b = 45 And
c = 1
End If
MsgBox(b)
End Sub
Thinks i dont understand and where i need someones help:
"c=0" and "c=1" are underlined with the error: Strict on doesnt allow implicit convertation from boolean to integer. WHY? I declared c as integer!
Variable "b" and "c" are always "0" even though in the case above they should be b=30 and c = 1.
can anyone please explain me this behaviour.
You are using the And keyword where it is not allowed. And is a logical operator (along with Or, AndAlso, OrElse.)
The following should work.
Sub Main()
Dim a As Integer = 8 * 60
Dim b As Integer
Dim c As Integer
If a < (6 * 60) Then
b = 0
c = 0
ElseIf a >= 6 * 60 And a < 9 * 60 Then
b = 30
c = 1
Else
b = 45
c = 1
End If
MsgBox(b)
End Sub