Page 1 of 1 1
Topic Options
#25092 - 2002-07-15 10:28 PM Arrays
Wayne Miller Offline
Fresh Scripter

Registered: 2002-05-14
Posts: 30
Loc: Dallas, TX
Forgive my ignorance but I need to use an array and have a couple of questions. I have a file that contains a list of files that I need to read into an array. The script is located below. The number of lines in the file will vary. I know I can count the lines close the file and then reopen it so I can start at the beggining but is there a way to tell kixtart to begin reading at the begining of the file.

$filelist=e:\files.txt
$lines=0
$num=0

Shell "%comspec% /c dir e:\fanout\apps /s /b > $filelist"

open (1,$filelist,2)

;Counts the number of lines in the $filelist file

;readline (1)
;while @error="0"
; $lines=$lines + 1
; readline (1)
;loop

;Reads the $filelist file into an array called file

dim $file[7]

while @error = "0"
$file[$num]=readline (1)
$num=$num+1
loop

CLOSE (1)

EXIT

Top
#25093 - 2002-07-15 10:39 PM Re: Arrays
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Look into the use of REDIM, e.g.
code:
REDIM preserve $array[ubound($array)+1]

will resize the array by one element and retain already assigned values.

[ 15 July 2002, 22:40: Message edited by: sealeopard ]
_________________________
There are two types of vessels, submarines and targets.

Top
#25094 - 2002-07-15 10:44 PM Re: Arrays
Anonymous
Unregistered


It sounds like you should use the UDF PIPE() (see the UDF forum)
You can execute a shell command and the output
will be returned in an array for you.

Top
#25095 - 2002-07-15 11:15 PM Re: Arrays
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
here is an example....

code:
break on
$file = "files.txt"

;open the file in question.
$nul = open(1,$file)

;lets create an array just as a starting point
$arraysize = 10
DIM $array[$arraysize]

;we will use $count to keep track of the size of the array
$count = 0

;prime the pump so to speak.....
$LineOfData = readline(1)
while NOT @error

;lets check $count VS the current size of the $array
if $count > ubound($array)

;looks like $array needs to be resized. lets add 10 more elements to the array.
$arraysize = $arraysize + 10
redim preserve $array[$arraysize]
endif

;add the data to the $array
$Array[$count] = $LineOfData

;increase $count to point to the next $array element
$count = $count+1

;get the next line of data from out file
$LineOfData = readline(1)
loop

;we have gone through all the lines in the file... lets
;trim back our array to only what we need, the value of $count-1
redim preserve $array[$count-1]


;a quick walk through of all the array elements...
for each $file in $array
? $file
next

hope this helps

Top
#25096 - 2002-07-16 06:23 PM Re: Arrays
Wayne Miller Offline
Fresh Scripter

Registered: 2002-05-14
Posts: 30
Loc: Dallas, TX
Thanks Bryce.

The only problem with your code is it did not allow the redim to have a variable so I add the line
$count=$count -1
and replaced the redim preserve $array[$count - 1] with
redim preserve $array[$count]

Top
#25097 - 2002-07-17 01:34 AM Re: Arrays
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
you are saying that

redim preserve $array[$count-1]

does not work for you?

That is very weird... I use this syntax all the time with out a problem.

Bryce

Top
#25098 - 2002-07-17 03:34 PM Re: Arrays
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Hmm, it won't work for $count=0 because you're trying to REDIM with a negative number in the end. Other than that, I've never had a problem with REDIM either.
_________________________
There are two types of vessels, submarines and targets.

Top
#25099 - 2002-07-17 07:59 PM Re: Arrays
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
well yea that is true... [Big Grin]

I will normally include

code:
if $count > 0
redim preserve $array[$count-1]
else
redim preserve $array[$count]
endif

or something along those lines... depends on what i am wanting to do.

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
1 registered (Allen) and 781 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.08 seconds in which 0.061 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