#23470 - 2002-06-18 10:49 PM
Arrays 101 (Multi-Dimensional Arrays)
|
Tony72
Getting the hang of it
Registered: 2002-06-18
Posts: 53
|
I have never been good with arrays, not even way back in BASIC on a trs-80 anyway, can someone give me a brief rundown on how to get this to work? here is the setup-
i wanted to create an array with three items that will be used at the same time, and have multiple groups of these three. For example=
code:
$array="a1","a2",a3","b1","b2","b3"...
now, how would I go about USING these (or is there a better way to set this up)? there are 24 of these. I am using code:
Case $comparison = "$a1" $misc1="a2" $misc2="a3"
but as I said, there are 24 lines, potentially more in the future, and I would like to keep my script as un-bloated as possible. I have RTFM, but my mind just isnt getting around it; as I said, I have never been good with arrays, but now I definitely want to address this shortcoming. [ 19 June 2002, 05:25: Message edited by: Howard Bullock ]
_________________________
Be kind; each person is fighting his own private war.
|
Top
|
|
|
|
#23471 - 2002-06-18 11:14 PM
Re: Arrays 101 (Multi-Dimensional Arrays)
|
Lonkero
KiX Master Guru
Registered: 2001-06-05
Posts: 22346
Loc: OK
|
well, firstly, if you have of everyone 3 items, easiest is to create 3 arrays. so you would have: $a=1,2,3,4,5,6,7,8 $b=1,2,3,4,5,6,7,8 $c=1,2,3,4,5,6,7,8
to access arrays one can use many ways, but easiest is to use the array index. in this case: $a[0] means 1 entry which is 1 and the other arrays have the same. to get all the elements of the array one could do: $a[0] $a[1] $a[2] and so on... but if you know how many elements array contains, in this case 8 we can make a loop of it: for $counter=0 to 7 ; arrays start indexing from 0 not 1. ? "$counter has " $a[$counter] " in it" next this loop gets every element of the array and outputs it to new line with same fancy bancy...
so, if they are all three alike you can query them together: for $counter=0 to 7 ? "array a $counter has " $a[$counter] " in it" ? "array b $counter has " $b[$counter] " in it" ? "array c $counter has " $c[$counter] " in it" next
which outputs all arrays to separate lines.
and if something has left open, just don't hesitate to ask.
cheers,
_________________________
!download KiXnet
|
Top
|
|
|
|
#23473 - 2002-06-18 11:44 PM
Re: Arrays 101 (Multi-Dimensional Arrays)
|
BrianTX
Korg Regular
Registered: 2002-04-01
Posts: 895
|
I wonder if in this case you would be better off using the UDF Marray() from the UDF forum. It creates multidimensional arrays. (although I admit if you're just starting out with arrays, it might be a little hard to grasp.)
Brian [ 18 June 2002, 23:52: Message edited by: BrianTX ]
|
Top
|
|
|
|
#23475 - 2002-06-19 12:18 AM
Re: Arrays 101 (Multi-Dimensional Arrays)
|
Tony72
Getting the hang of it
Registered: 2002-06-18
Posts: 53
|
actually, what i need is a 3x24 array. I looked at MArray, and was... ehem... confused. Im sure in a few years I'll wonder how I didnt get it, but for now... Anyway, thanks for the array explaination. I was actaully wondering if I would be better off having three arrays, like the example, or if I could just use one large list like my example, and have it look for
x=0 :loop $item1=$array[x+1] $item2=$array[x+1] $item3=$array[x+1] if $variable = "$item1" $blah="$item2" $yech="item3" endif
also, how does the script know when the end of the array is reached? Also, how does it know that once a match is made it doesnt have to execute thru the rest of the array? [ 19 June 2002, 00:22: Message edited by: Tony72 ]
_________________________
Be kind; each person is fighting his own private war.
|
Top
|
|
|
|
#23477 - 2002-06-19 12:45 AM
Re: Arrays 101 (Multi-Dimensional Arrays)
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
If you just need a 3x24 array you can use something like this. I find this rather easy to comprehend. code:
Dim $A[24], $B[24], $C[24], $D[3]
For $i=0 to 23 $A[$i] = chr($i+65) + 1 $B[$i] = chr($i+65) + 2 $C[$i] = chr($i+65) + 3 Next
$D[0] = $A $D[1] = $B $D[2] = $C
for $h=0 to 23 for $i=0 to 2 ? $D[$i][$h] next ? next
code:
;Building a three dimensional array (2,3,24) ;Define data buckets arrays (2*3)=6 arrays of 24 elements each Dim $A[24], $B[24], $C[24], $E[24], $F[24], $G[24]
; Define the middle tier arrays (2 arrays of 3 elements each) Dim $D[3] $D[0] = $A $D[1] = $B $D[2] = $C
Dim $J[3] $J[0] = $E $J[1] = $F $J[2] = $G
; Define the top level array Dim $K[2] $K[0] = $D $K[1] = $J
for $m=0 to 1 for $i=0 to 2 for $h=0 to 23 $K[$m][$i][$h] = "$m,$i,$h" ? $K[$m][$i][$h] next ? next ? next
? "Element $K[1][2][20] equals: " + $K[1][2][20] ;Assign a new value to the element $K[1][2][20] $K[1][2][20] = "Hi There" ? "Element $K[1][2][20] now equals: " + $K[1][2][20]
[ 19 June 2002, 04:52: Message edited by: Howard Bullock ]
|
Top
|
|
|
|
#23478 - 2002-06-19 10:15 AM
Re: Arrays 101 (Multi-Dimensional Arrays)
|
cj
MM club member
Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
|
I couldn't pass a thread on arrays without sticking in my 2c
My favourite way to use 2D (and higher) arrays is to make them "CSV's":
Pick a delimiter for each dimension, I often use CHR(255), CHR(254) and down and don't normally use more than three dimensions. Let's use comma (,) and semi colon (
Now make one long string var with these delimiters:
So, a 3x24 array containing this info:
code:
Member Posts Rank LLigetfa 2076 3 MCA 2698 2 HowardBullock 618 12 Shawn 2803 1 sealeopard 848 11 kdyer 1083 7 BrianTX 354 18 NTDOC 1205 6 Radimus 948 9 jpols 1512 5 Lonkero 957 8 Bryce 1808 4 RichardHowarth 425 15 DrillSergeant 588 13 AlexH 385 16 bleonard 527 14 cj 923 10 awinkel 270 21 WillHetrick 211 25 JackLothian 338 19 kholm 306 20 masken 216 23 DDavidson 378 17
would look like this:
Member,Posts,Rank;LLigetfa,2076,3;MCA,2698,2;HowardBullock,618,12;Shawn,2803,1;sealeopard,848,11;kdyer,1083,7;BrianTX,354,18;NTDOC,1205,6;Radimus,948,9;jpols,1512,5;Lonkero,957,8;B ryce,1808,4;RichardHowarth,425,15;DrillSergeant,588,13;AlexH,385,16;bleonard,527,14;cj,923,10;awinkel,270,21;WillHetrick,211,25;JackLothian,338,19;kholm,306,20;masken,216,23;DDavid son,378,17
To extract info from this mess, we use the SPLIT() function:
$record=SPLIT(mess, ";")[0] returns the first RECORD which is Member,Posts,Rank
SPLIT($record, ",")[0] returns the first FIELD of the specified record, which is Member
We can combine these two functions like this:
$result=SPLIT(SPLIT(string, ";")[record number], ",")[field number]
You can wrap that in a UDF if you want to. I normally only use this method for READ ONLY arrays, like lookup tables etc. To write a change to the array is a little more complex:
To change cj's "rank" to 1 ( ) you want to change field 2 in record 19 (both are zero based) you just SPLIT the string into an array, change the particular element and re-join the array. This is easier in VBS with it's JOIN function, but can be done in KiX very easily. I have a UDF in my scripting page called vbJOIN.UDF that does this
code:
$aRecords=SPLIT($sString, ";") ; split the string into records $aFields=SPLIT($aRecords[19], ",") ; split required record(s) into fields $aFields[2]=1 ; make change(s) $sString="" ; clear string for each $zField in $aRecords $sString=$sString+$zField+";" ; join next $sString=substr($sString, 1, len($sString)-1) ; string that last record marker
again, you can put all that in a UDF.
This is not the best, fastest or cheapest way to work with arrays, but it will help you get a better understanding of them
It is also handy for passing info between places that does not support arrays, like the window.showModalDialog method in DHTML etc...
cj
|
Top
|
|
|
|
#23479 - 2002-06-19 10:37 PM
Re: Arrays 101 (Multi-Dimensional Arrays)
|
Tony72
Getting the hang of it
Registered: 2002-06-18
Posts: 53
|
thanks, I think I have a handle on the arrays now. Lots of good info; tomorrow I will have time to work on my script and test it out (spent all day working on a terminal server problem. I hate legacy applications!)
If NWN came in at CompUSA, today will be a perfect day.
_________________________
Be kind; each person is fighting his own private war.
|
Top
|
|
|
|
#23481 - 2002-06-24 09:03 PM
Re: Arrays 101 (Multi-Dimensional Arrays)
|
Tony72
Getting the hang of it
Registered: 2002-06-18
Posts: 53
|
ya, Neverwinter Nights. I ended up getting it last Friday during my lunch break. That was much better than actually getting food (ya right).
Right now I am modifying my logon script to use the array values. I am actually using it to find out the site location: where I work supports a lot of different sites via WAN links, and one thing it needs to do is figure out which one the user is at (which it does by looking at the subnet that user is on).
The array, then, holds the subnet list, the site name, and what type of site it is (there are 5 or 6 types).
Need to test out the different kinds of working with the array; I want to go with the fastest method, since this is a logon script.
One question, tho- would it be faster to have this routine in the logon script, or call another script that does the detection and names the location?
_________________________
Be kind; each person is fighting his own private war.
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 507 anonymous users online.
|
|
|