Sign in to follow this  
Followers 0
Davman

Combining variables

22 posts in this topic

Ok, let me try and explain the problem, and ask for a solution.

Say I want to store some data in a set of variables.

$var1

$var2

$var3

And i want to write a loop to tell me the contents of those variables.

While $var > 0

$var = $var - 1

MsgBox(0, "Test", "Test Data:" <CODE HERE>)

Wend

Now, in a previous scripting language (the joys of mIRC), I'd achieve this by:

$eval($eval(%var,0) $+ %var,2)

Which would (apparently), present this variable name to the interpreter:

$Var($var)

or, in real world terms

$Var1

The interpreter would evaluate $var1 and give the contents.

The whole thing would probably look like

on *:LOAD:{

//set %var1 test1

//set %var2 test2

//set %var3 test3

//set %var 0

//set %totalvar 3

:one

//inc %var 1

if (%var == %totalvar) {

//echo -s Done

//halt

}

else {

//echo -s Var Contents: $eval($eval(%var,0) $+ %var,2)

//goto one

}

}

And the output would be:

test1

test2

test3

So, after that long winded explanation, does anyone have a simple solution?

In my n00bishness, i tried $var & $var, but that just returned 33, 22, 11.

:ph34r:


Sitting comfortably behind the code.

Share this post


Link to post
Share on other sites



Never mind...answered my own question...Teach me to post first and search later.

$test1 = "test1"

$test2 = "test2"

$test = 3

While $test > 1

$test = $test - 1

$s = Eval("test" & $test)

MsgBox(0, "Test", "Test Data:" & $s)

Wend

Joyous thanks be to the forum search function on "eval".


Sitting comfortably behind the code.

Share this post


Link to post
Share on other sites

Ok, so the next problem, which i have attempted to research.

This time i want to write to variables rather than read them.

So, writing to $test1, $test2, $test3, etc.

I want to be able to write to an infinite number of variables, i.e. in a loop... so anything up to $test999, if you see what i mean.

is this going to be like:

$test = $test + 1

$test($test) = "Data"

Cant figure this one out, having problems getting my small brain around a new syntax. :ph34r:

:iamstupid:


Sitting comfortably behind the code.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I know what you mean.

Like this:

$test1 = "something"

$test2 = "something"

...

$test900 = "something"

$test901 = "something"

Currently, it's not possible.

I also want to be able to dynamically create variables.

I hope it will be possible one day.

Edited by SlimShady

Share this post


Link to post
Share on other sites

Rather use an array :ph34r:

;assign values discretely..
    Dim $asData[10]
    $asData[1] = "The"    
    $asData[2] = "Quick"    
    $asData[3] = "Brown"    
    $asData[4] = "Fox"    
    
;Assign automagically
    $asMonth = StringSplit("Jan.Feb.Mar.Apr.May.Jun.Jul.Aug.Sep.Oct.Nov.Dec", ".")
    Msgbox(0,"","This month is: " & $asMonth[@MON])

HTH

Share this post


Link to post
Share on other sites

Is reading the data out going to be the same as i posted before?

Can i use

$no = $no + 1

$var[$no]


Sitting comfortably behind the code.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Yep :ph34r: See the Msgbox line in the example I posted. Or, to walk the entire array (see helpfile for StringSplit)..

;Assign automagically, and walk the array
   $asMonth = StringSplit("Jan.Feb.Mar.Apr.May.Jun.Jul.Aug.Sep.Oct.Nov.Dec", ".")
    For $nMonth = 1 to $asMonth[0]
        Msgbox(0,"","Month " & $nMonth & " = " & $asMonth[$nMonth])
    Next

Edit: also read about the Dim statement

Edited by trids

Share this post


Link to post
Share on other sites

:iamstupid:

:ph34r:

Ok, I'm really having trouble getting my head around this.

Let me try and explain better.

I have an existing loop that reads through a text file. I want to add the output of this read (var name = $brandnewresult) to a new variable each time the loop completes.

So, first time the loop runs, $brandnewresult = "the first line i found", and i want

$test1 to be "the first line i found"

then the next time it runs, $brandnewresult = "the second line i found", and i want

$test2 to be "the second line i found"

But i want to make the infinite, so even if i get to the 999'th run of the loop,

$brandnewresult should be "the 999th line i found", and $test999 should be "the 999th line i found".

I hope this explains what I'm trying to achieve. I'm not normally this n00b-ish, but I seem to be having a mental block today.

:(


Sitting comfortably behind the code.

Share this post


Link to post
Share on other sites

Ofcourse, arrays :ph34r:

I always forget.

Here's what you can do:

;===============================================================================
;
; Description:      Returns the number of lines in the specified file.
; Syntax:           _FileCountLines( $sFilePath )
; Parameter(s):     $sFilePath - Path and filename of the file to be read
; Requirement(s):   None
; Return Value(s):  On Success - Returns number of lines in the file
;                   On Failure - Returns 0 and sets @error = 1
; Author(s):        J. Sierra
; Note(s):          None
;
;===============================================================================
Func _FileCountLines( $sFilePath )
 ;==============================================
 ; Local Constant/Variable Declaration Section
 ;==============================================
  Local $hFile
  Local $iLines

  $hFile = FileOpen($sFilePath, 0)
  If $hFile <> -1 Then
    While 1
      FileReadLine( $hFile )
      If @error = -1 Then ExitLoop
      $iLines = $iLines + 1
    Wend
    FileClose($hFile)
    Return $iLines
  Else
    SetError( 1 )
    Return 0
  EndIf
EndFunc

$PathToFile = "enter the file path here"

$TotalNumOfLines = _FileCountLines($PathToFile)

Dim $Line[$TotalNumOfLines + 1]

$Line[0] = $TotalNumOfLines

For $i = 1 To $TotalNumOfLines
    $Line[$i] = "line number you found: " & $i
Next

Share this post


Link to post
Share on other sites

I still feel sufficiently n00b-ish...

If i understand correctly, i can go:

$testno = 1

$test[$testno] = "this is data1"

and then the variable "$test1" will contain "this is data1"...

and then if i

$testno = $testno + 1

and do

$test[$testno] = "this is data2"

then variable name "$test2" will contain "this is data2"

Am i right?


Sitting comfortably behind the code.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I still feel sufficiently n00b-ish...

If i understand correctly, i can go:

$testno = 1

$test[$testno] = "this is data1"

and then the variable "$test1" will contain "this is data1"...

and then if i

$testno = $testno + 1

and do

$test[$testno] = "this is data2"

then variable name "$test2" will contain "this is data2"

Am i right?

No.

See this:

$testno = 1

$test[$testno]= "this is data1"

and then the variable $test[1] will contain this is data1

and then if i

$testno = $testno + 1

and do

$test[$testno] = "this is data2"

then variable name $test[2] will contain this is data2

Edited by SlimShady

Share this post


Link to post
Share on other sites

Ok, just going to ramble to myself so i dont forget what i've experimented with when i get home...

;Assign automagically

$asMonth = StringSplit("Jan.Feb.Mar.Apr.May.Jun.Jul.Aug.Sep.Oct.Nov.Dec", ".")

Msgbox(0,"","This month is: " & $asMonth[@MON])

^^^^^

Looks interesting. I think that the msgbox looks through the $asMonth variable for the @MON'th entry. I.e for this month, its the 7th entry. Delete "jul" and it returns august, coz thats the 7th entry.

So, get rid of @MON, replace with $test and throw this in a loop.

$test = 0

While $test < 12

$test = $test + 1

Msgbox(0,"","This month is: " & $asMonth[$test])

Wend

Will return Jan,Feb,Mar, etc.

So, add data to a single variable then? hmm... how about, each entry from the text file, add a splitting character (*) and then the new entry. Then

$newstring = StringSplit("$oldstring", "*")

then

$test = 0

While $test < 12

$test = $test + 1

Msgbox(0,"","This data is: " & $newstring[$test])

Wend

should return the data piece by piece....

I hope.


Sitting comfortably behind the code.

Share this post


Link to post
Share on other sites

Ahah!

Dim $test[100]

$testno = 1

$test[$testno] = "this is data1"

$testno = $testno + 1

$test[$testno] = "this is data2"

$newtest = 0

While $newtest < 2

$newtest = $newtest + 1

MsgBox(0, "Error", "Data:" & $test[$newtest])

Wend

The problem being this will only hold 100 items, right? Dont suppose I can make this infinite...


Sitting comfortably behind the code.

Share this post


Link to post
Share on other sites

How about this for an interesting solution: you could read each byte into an array (expanding as you go.)

You can expand an array as you go? What new devilry is this?

:ph34r:


Sitting comfortably behind the code.

Share this post


Link to post
Share on other sites

It's called ReDim and it's available (Finally) in the "unstable" .102 builds.

And now a rant:

I saw in another thread you don't use beta software. Well, to me, that's just stupid. I've seen just as many and just as severe bugs in so-called stable software as I have beta's (And that's not even factoring Microsoft into that equation). On top of that, if there are bugs in the beta stuff, how will they ever be found and fixed if everybody has the "ooh, beta = bad, me no use" attitude?

I mean, it's not like us developer-types have something like:

#ifdef BETA
#define BUGS TRUE
#elif RELEASE
#define BUGS FALSE
#endif

in our code just for beta releases.

Share this post


Link to post
Share on other sites

:ph34r: *chuckles*

you do like a good rant dont you :(

to say "i dont use beta software", i guess is a bit of a lie... i use Firefox, i'm beta testing XP SP2, i code my own software, and obviously have to test that. What i mean in the case of auto-it, is that i read someplace that

"the GUI system is constantly being re-written, and as such, code you write now probably wont work in the future"

I dont want to write stuff that wont work next release.

Thats all I meant. Nothing wrong with BETA as a whole.

Hope this clarifies.


Sitting comfortably behind the code.

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

And now a rant:

I saw in another thread you don't use beta software.

I suggest you just fold and come to the beta side. I used to fear it, but now I love it. And really, Valik's right. I mean, that's all there is to it. Valik's right.

(and such a nice guy too!)

and yeah, the GUI's very fluid, but the rest of it is fairly stable. I haven't had any problems of new releases breaking old code, and usually if it will, there's good notice of it's doing so.

Edited by emmanuel

"I'm not even supposed to be here today!" -Dante (Hicks)

Share this post


Link to post
Share on other sites

heh, i am on the beta site, lol. Like i said, beta testing XP SP2 RC2 right now, running skype (also beta), i write Oblivion (beta mIRC script), use firefox, and twiddle with some Ultima Online tools, such as RunUO, etc.... so yeah, i do beta. I'm just going by what i read in the manual, or some place... cant remember where, i read it at work, and that is a big blur...


Sitting comfortably behind the code.

Share this post


Link to post
Share on other sites

"the GUI system is constantly being re-written, and as such, code you write now probably wont work in the future"

I agree with emmanuel on this one. Just because the GUI is there doesn't mean you need to use it. The rest of the functions are not likely to change at all (especially as a stable .102 is planned to be released shortly with no GUI functions.) And just because there is a bug somewhere in .102 doesn't mean you will run across it. Once you compile your code, as long as it works, there's nothing to stop you from using that binary even as version changes occur. After all, why replace a working piece of software unless it doesn't work, or needs to be expanded?

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Share this post


Link to post
Share on other sites

*runs around screaming*

Ok ok! you have broken my will!

*downloads newest available build... when he can find it... link me!!!*


Sitting comfortably behind the code.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0