#127140 - 2004-09-28 01:37 PM
Info about declaring arrays and how to use them
|
Anonymous
Anonymous
Unregistered
|
Hi! I'm a (almost) complete newbie to kix-scripting, but it struck me that using array's would provide me with a powerful tool if I only found out how to do it properly. Does anyone now of some good examples? I've searched somewhat on the web, but I'm at loss here, friends) I would be very grateful ... Tom
|
Top
|
|
|
|
#127143 - 2004-09-28 05:05 PM
Re: Info about declaring arrays and how to use them
|
Bryce
KiX Supporter
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
some simple examples....
Code:
;this creates an array containing 10 variables of both numbers and strings.
$array = 1,2,3,4,5,"a","b","c","d","e"
;or...use the split command to split a string on a given delimiter, in this case using a single space.
$array = split("1 2 3 4 5 a b c d e"," ")
for each $item in $array
? $item
next
use the DIM or GLOBAL command to declair a blank/empty array.
Code:
;this creates a 10 variable array the array points are 0 to 9
dim $array[9]
$array[0] = 1
$array[1] = 2
$array[2] = 3
$array[3] = 4
$array[4] = 5
$array[5] = "a"
$array[6] = "b"
$array[7] = "c"
$array[8] = "d"
$array[9] = "e"
use the ubound() command to return the uper limit of an array...
Code:
$array = 1,2,3,4,5,"a","b","c","d","e"
$uperlimit = ubound($array)
for $i = 0 to $uperlimit
? $array[$i]
next
use redim to adjust the size of the array, you can preserve the contents as well.
Code:
$array = 1,2,3,4,5,"a","b","c","d","e"
;increase the sice of the array by 1 and asign the last point a value of Z
redim preserve $array[ubound($array)+1]
$array[ubound($array)] = "z"
Fancy nested arrays.....
to use something like Richard's excell analogy, you can stuff an array into an array...
a simple example for a 2 dimension 4x4 array
Code:
0 1 2 3
0 f g h j
1 k l q w
2 e r t y
3 u i o p
using the above example, 02 = "h", 21 = "r"... so on and so forth....
now some code...
Code:
;first DIM the array...
DIM $array[3]
;this will expand the array from a single dimension To into a 2 dimension 4x4 array
For $i = 0 To UBound($array)
$array[$i] = $array
Next
;lets add our data...
$array[0][0] = f
$array[0][1] = g
$array[0][2] = h
$array[0][3] = j
$array[1][0] = k
$array[1][1] = l
$array[1][2] = q
$array[1][3] = w
$array[2][0] = e
$array[2][1] = r
$array[2][2] = t
$array[2][3] = y
$array[3][0] = u
$array[3][1] = i
$array[3][2] = o
$array[3][3] = p
;now print out some of the array cells..
? $array[0][2]
? $array[2][1]
? $array[3][3]
Bryce
Edited by Bryce (2004-09-29 09:26 PM)
|
Top
|
|
|
|
#127146 - 2004-09-29 09:19 PM
Re: Info about declaring arrays and how to use them
|
Anonymous
Anonymous
Unregistered
|
1st of all! Many thanks to you all for (I think) valuable information. What I had in mind was to use another program (XXCOPY) to dump directory information of all registered profiles under "documents and settings", and put it in a text file. Then I would (if I could ) use KIX to read that file and store the (different kind of) information into an array for further processing (backup, for example). Note: The only information I'd like to gain is the name of the profile folders. CYA Tom
|
Top
|
|
|
|
#127147 - 2004-09-29 09:38 PM
Re: Info about declaring arrays and how to use them
|
Bryce
KiX Supporter
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
you can skip a step by using WSHPipe(). This will take the output from XXCOPY, and pipe it straight into an array with out having to mess with a temp file.
Or if you are wanting to read a temp fine into an array, their are a few UDF's that will do that as well.
here is a sample of one that i use that uses the FSO.
Code:
;this Function will load the contents of a text file into an array Function LoadFile($file) DIM $fso,$f,$fs $fso = CreateObject("Scripting.FileSystemObject") $f = $fso.GetFile($file) If @ERROR Exit(2) EndIf $fs = $f.OpenAsTextStream(1) $loadfile = Split($fs.Read($f.size),@CRLF) Exit(@ERROR) EndFunction
so...how you would use it is like this.
Code:
for each $line in LoadFile("c:\output.txt") ? $line next
Working with arrays are easy, and once you grasp them, almost become second nature.
|
Top
|
|
|
|
#127148 - 2004-09-29 10:07 PM
Re: Info about declaring arrays and how to use them
|
Bryce
KiX Supporter
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
Quote:
Note: The only information I'd like to gain is the name of the profile folders.
you are just after the Foldernames in the "Documents and Settings" Folder?
while i would be remis to not pimp my own DIRPlus() UDF
here is an example on how to pull this information, and build an array on the fly using Kix's own DIR() command.
(slow day at work for me )
Code:
Break on
;the path To the folder you want To look AT. $path = "c:\Documents and Settings"
DIM $array[0] ;create our array with a size of 1 DIM $i ;this is Used as a counter To control array size. DIM $Dir ;this Returns a Value from the Dir() command DIM $profile ;this is Used To show the contents of $array
;first you need To prime the $Dir variable/command by giving the Dir() comamnd a path $Dir = Dir($path)
;now enter INTo a Loop, make sure that the $Dir=Dir() ;is always the last command In the Loop since you are ;relying on the @ERROR from the Dir() To Break the Loop While not @ERROR ;check To see If the Returnes file/folder is a "Folder" If GetFileAttr($path + "\" + $Dir) & 16
;a quick check To make sure that $Dir Does not equal "." or ".." If $Dir = "." or $Dir = ".." ;Do not add "." or ".." To $array Else ;$Dir is a "folder" so lets add it To our array $array[$i] = $Dir
;Increase our counter by 1 $i = $i + 1
;Increase the size out our array To be able To handle new data ReDIM preserve $array[$i] EndIf EndIf
$Dir=Dir() Loop
;the Loop is finished, but our $array has 1 To many pointers In it, so we need To Trim it Down. ;also, If no folder was ever found $i is equal To 0, so In that Case Return nothing If $i ReDIM preserve $array[$i-1] Else ;looks like no folders were found, so erasing the $array array $array=0 EndIf
;now we have an array that contains all of the folder names In the given path. For Each $profile In $Array ? $profile Next
|
Top
|
|
|
|
#127149 - 2004-10-04 12:45 AM
Re: Info about declaring arrays and how to use them
|
Anonymous
Anonymous
Unregistered
|
THX! To me, it's an impressing piece of work you got there! Ok, here's another problem. I have an empty file created (c:\backup.cmd) and I want to write to it, but I got an error code from WRITELINE. This is what I did: OPEN(1,"E:\BKP\BATCH\BACKUP.CMD") WRITELINE(1, "*duh*") CLOSE(1) DISPLAY "E:\BKP\BATCH\BACKUP.CMD" ___ I got this from KIX: Error (317 / 13D) while retrieving error information for FFFFFFFC0 Needless to say, my file 'backup.cmd' was empty.. I'm running Windows XP/SP2 + latest KIX (4.22) *sigh*
|
Top
|
|
|
|
#127150 - 2004-10-04 12:47 AM
Re: Info about declaring arrays and how to use them
|
Anonymous
Anonymous
Unregistered
|
Thank's! Lot of good stuff in there!
|
Top
|
|
|
|
#127151 - 2004-11-02 12:42 AM
Re: Info about declaring arrays and how to use the
|
redniels
Fresh Scripter
Registered: 2002-10-21
Posts: 19
Loc: Netherlands
|
Finally... I get it, thanks guys..
searched a lot, maybe this thread needs a more "visible" spot.. anyways:
call me stupid, but i didn't "got" the array stuff. now I do. (and my script works!)
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 259 anonymous users online.
|
|
|