Page 1 of 1 1
Topic Options
#120169 - 2004-05-26 03:22 PM Variable name incrementing in iterations
Scripter2004 Offline
Fresh Scripter

Registered: 2004-03-01
Posts: 31
Loc: Minneapolis, MN (USA)
I'm parsing each line of a text file to see if it contains a 'name=xxx' parameter. If so, I strip out everything but that 'name' parameter value. I want to put the first 'name' parameter value that I find in a variable called $Book1, the second one $Book2, etc. Is there a way to increment a variable name with every iteration? I know you can increment a variable *value*, but what its *name*?

Thx
Ron

Top
#120170 - 2004-05-26 03:33 PM Re: Variable name incrementing in iterations
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
You can do this, but then you need to be able to refer to the new variables later which just makes things damned awkward.

You would be much better off using arrays.

Top
#120171 - 2004-05-26 03:42 PM Re: Variable name incrementing in iterations
Scripter2004 Offline
Fresh Scripter

Registered: 2004-03-01
Posts: 31
Loc: Minneapolis, MN (USA)
Oh right! So set up a Books array, and then put the first name in Books[0], the second one in Books[1], etc., right?
Top
#120172 - 2004-05-26 04:01 PM Re: Variable name incrementing in iterations
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Thats it exactly.

If you don't know how big the array will be, you can create it dynamically.

There are a lot of ways of doing it, but here's one.

Take a file "bookfile.txt" with the following contents:
Code:
name=Withering Bites
name=Hairy Otter and the Fragrant rice of Doom
name=20 Leeks under the Pea



This code will load the values into an array, then display them:
Code:
If Not Open(1,"bookfile.txt")
$s=ReadLine(1)
While Not @ERROR
If InStr($s,"name=")=1
If IsDeclared($aBooks)
$aBooks=Split(Join($aBooks,"|")+"|"+SubStr($s,6),"|")
Else
$aBooks=Split(SubStr($s,6),"|")
EndIf
EndIf
$s=ReadLine(1)
Loop
$=Close(1)
EndIf

For $i = 0 to Ubound($aBooks)
$i+1 " " $aBooks[$i] ?
Next


Top
#120173 - 2004-05-26 04:26 PM Re: Variable name incrementing in iterations
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
you can use Readfile() to read a textfile into an array
then use filterarray() to filter out every line without 'name=' on it
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#120174 - 2004-05-27 06:40 AM Re: Variable name incrementing in iterations
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
This smells like a .INI file. If the file looks like
Code:

[section]
key=value


then you should use READPROFILESTRING() to read sections, keys, or values.
_________________________
There are two types of vessels, submarines and targets.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1247 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.054 seconds in which 0.024 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org