Lua: What is the difference in these variable assignments? - variables

what is the difference between the variable assignment
local newpos = {}
newpos.x = 1 ----- or --------- newpos[x] = 1
i know not what i speak but to me these seem to be the same thing if not similar?

newpos.x = 1 is the same as newpos["x"] = 1 that is they both set the value stored at key string "x" to 1.
newpos[x] = 1 is different. This is set the value stored at key contents of variable x to 1.
Try it and see.
local newpos = {}
newpos.x = 1
print(newpos.x, newpos["x"], x, newpos[x])
newpos["x"] = 2
print(newpos.x, newpos["x"], x, newpos[x])
local x = "var"
print(newpos.x, newpos["x"], x, newpos[x])
newpos[x] = 3
print(newpos.x, newpos["x"], x, newpos[x])
Results for the above:
1 1 nil nil
2 2 nil nil
2 2 var nil
2 2 var 3

Related

F# Clearly defined mutable variable errors it is not a mutable variable

I have several mutable variables in my code. All of them works except one!
Variable d gets several errors like
learn.fsx(33,25): error FS0027: This value is not mutable. Consider using the mutable keyword, e.g. 'let mutable d = expression'
The problem is that when you look in my code then the variable has clearly been defined as a mutable variable.
I think it is a necessity since the only thing I can think of should cause the problem is that it is something after the variable definition that makes it immutable again.
let seq = [2;2;3;3;5;6]
let exp = [[];[]]
let mutable points = 0
let mutable e = 1
let mutable state = ""
let mutable d = 1
let rec guess (x:int) =
match points with
|100 -> "learned"
|_ -> match seq.[x] with
|d -> match (exp.[((List.length exp)-2)]) with
|[] -> if state = "right" then
exp.[((List.length exp)-1)]#[d]
else
state <- "right"
exp#[[d]]
points <- points + 1
if d = 6 then
d <- 1
else
d <- d + 1
if x = 5 then
(guess 0)
else
(guess (x+1))
|_ -> if state = "right" then
exp.[((List.length exp)-1)]#[d]
else
state <- "right"
exp#[[d]]
if (List.length exp.[((List.length exp)-2)]) >= 2 then
d <- (exp.[((List.length exp)-2)]).[e]
else
if d = 6 then
d <- 1
else
d <- d + 1
e <- e + 1
if x = 5 then
(guess 0)
else
(guess (x+1))
|_ -> points <- points - 1
e <- 1
state <- "wrong"
if d = 6 then
d <- 1
else
d <- d + 1
if x = 5 then
(guess 0)
else
(guess (x+1))
Using d in the match causes that version of d to be used instead of the d defined as a mutable value.
Change the name of the value to something else of use 1 directly.
for example: | d -> match (exp.[((List.length exp)-2)]) can become | 1 -> match (exp.[((List.length exp)-2)])

AutoIt - Split 1 variable into 4

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.

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

What does += mean in Visual Basic?

I tried to google the answer for this but could not find it. I am working on VB.Net. I would like to know what does the operator += mean in VB.Net ?
It means that you want to add the value to the existing value of the variable. So, for instance:
Dim x As Integer = 1
x += 2 ' x now equals 3
In other words, it would be the same as doing this:
Dim x As Integer = 1
x = x + 2 ' x now equals 3
For future reference, you can see the complete list of VB.NET operators on the MSDN.
a += b
is equivalent to
a = a + b
In other words, it adds to the current value.
It is plus equals. What it does is take the same variable, adds it with the right hand number (using the + operator), and then assigns it back to the variable. For example,
Dim a As Integer
Dim x As Integer
x = 1
a = 1
x += 2
a = a + 2
if x = a then
MsgBox("This will print!")
endif
those 2 lines compiled produce the same IL code:
x += 1
and
x = x + 1
Just makes code more efficient -
Dim x as integer = 3
x += 1
'x = 4
is the same as
x = x + 1
'x = 4
It can also be used with a (-):
x -= 1
' x = 2
Is the same as
x = x - 1
'x = 2

Swap two integers without using a third variable

I have an assignment in which I need to swap two integers without using third variable.
I'm not sure how to do this. How would I code this?
Yes, it's possible:
Dim var1 = 1
Dim var2 = 2
var1 = var1 + var2
var2 = var1 - var2
var1 = var1 - var2
But why do you need it? The code becomes abstruse.
Lets assume
a = 10;
b = 20;
a = a + b; // a = 30
b = a - b; // b = 10
a = a - b; // a = 20
Values swapped.
Read up on the "xor swap algorithm."
You can find an answer here:
http://www.java2s.com/Tutorial/VB/0040__Data-Type/Swaptwointegerswithoutusingathird.htm
firstValue = firstValue Xor secondValue
secondValue = firstValue Xor secondValue
firstValue = firstValue Xor secondValue
Dim a As Integer
Dim b As Integer
a= 1
b= 2
a = a Xor b
b = a Xor b
a = a Xor b
To swap two numeric variables do like this
a = a + b;
b = a - b;
a = a - b;
OR
a = a xor b;
b = a xor b;
a = a xor b;
where a and b are variables to be swapped
theoretically 3 ways
a = 4 , b = 5
1. Using XOR
a = a XOR b = 4 XOR 5 = 9
b = a XOR b = 9 XOR 5 = 4
a = a XOR b = 9 XOR 4 = 5
2. Using +,-
a = a+b = 4+5 = 9 // should not overflow
b = a-b = 9-5 = 4
a = a-b = 9-4 = 5
3. Using *,/
a = a*b = 4*5 = 20 // should not overflow
b = a/b = 20/5 = 4 // should not overflow and should not be irrational number
a = a/b = 20/4 = 5 // should not overflow and should not be irrational number
The Xor or a+b algorithms above work and are the best way to do this, but just an example of a weird way to do it. Still not sure why you would want to do this. Just build a function that you supply two values ByRef and have it do the standard swap method.
Dim newList as New List(Of Integer)
newList.Add firstvalue
newList.Add secondValue
newList.Reverse
secondValue = newList.Item(0)
firstValue = newList.Item(1)
Take two text boxes and a command box.In command box type this code.
text1.text=val(text1.text) + val(text2.text)
text2.text=val(text1.text) - val(text2.text)
text1.text=val(text1.text) - val(text2.text)
Check link written for you
Approach#1.
Addition and Subtraction Method
Integer a, b
read a and b
a= a+b;
b=a-b;
a=a-b;
Problem:
Incorrect result when sum of numbers will exceed the Integer range.
Approach#2.
Multiplication and Division Method
Integer a, b
read a and b
a=a*b;
b=a/b;
a=a/b;
Problems:
If the value of a*b exceeds the range of integer.
If the value of a or b is zero then it will give wrong results.
Approach#3.
XOR Method
Integer a , b
read a and b
a=a^b;
b=a^b;
a=a^b;
Best approach to solve this problem without any pitfalls.