-1

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

3 Answers3

0

LOL to all the overkill answers

#include <Array.au3>
Local $parts = StringSplit("1574", "")
_ArrayDisplay($parts)
Milos
  • 2,927
  • 1
  • 15
  • 27
-1

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.

yspreen
  • 1,759
  • 2
  • 20
  • 44
  • So for i = 1 to stringlen will split it 1 by 1 for as long as the string is? the bigvar will btw change by either +1 or -1 1 at a time –  Jul 20 '14 at 11:10
  • The For indicates a loop. This loop goes through every character of the var and adds it to an array. The loop only manages to execute the commands inside the loop n times, with n being the length of the string and i increasing every time the loop is run through. So at the end you have an array with Array[0] being the amount of characters in the array and Array[n] being the nth char of your string or number (bigvar). – yspreen Jul 20 '14 at 11:18
  • wow this is nice :D. Just what I need I think I will try and see if I can apply this to my script. The 4 variables returned is called $SmallVar1, $SmallVar2, $SmallVar3, $SmallVar4 Right? –  Jul 20 '14 at 11:23
  • I would suggest using the function below, since it's simply giving you more possibilites. That way you can either set the 2nd parameter to be 4 if you know that your bigvar is between 0 and 9999 or just leave it out to have the functionality thats mentioned in the example above. If you have questions about the functionality of any part, feel free to ask. I'll do my best in explaining. – yspreen Jul 20 '14 at 11:28
  • I tried some stuff and i figured a way to send the value of it as one variable. i just need to take this variable and start it where the user tells it to then add 1 all the time. Is this hard –  Jul 20 '14 at 11:49
  • So you don't need this anymore? Or do you need something else now? I don't quite get it. – yspreen Jul 20 '14 at 11:51
  • I don't need this anymore sir. But I like your soloution:). I need it to simply start and end at the user defined values increasing by 1 for each try IE $UserinputStart=0000 $UserinputEnd=0010 then it should start at 0000 if it didnt suceed go to 0001 then 0002 etc etc, ending either if it suceeded or if it reached the $userinputend –  Jul 20 '14 at 12:03
  • From the looks of it it writes 0000,1,2,3,4,5,6 instead of 0001,0002 etc. but ill just see if I can stringformat that –  Jul 20 '14 at 17:54
  • an easy way is to use StringRight("0000" & $string, 4) – yspreen Jul 20 '14 at 18:25
-1

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.

Andreas
  • 5,393
  • 9
  • 44
  • 53