www.ClassicTW.com
https://mail.black-squirrel.com/

multi dimensional arrays
https://mail.black-squirrel.com/viewtopic.php?f=15&t=33218
Page 1 of 1

Author:  SteveH_66 [ Thu May 17, 2012 4:01 am ]
Post subject:  multi dimensional arrays

Still having problems with multidimensional arrays. Mostly syntax problems I believe. For example, I have a list of sectors in a bubble, and I read them to an array.

Code:
setVar $dataDir "gameData\"
setVar $bubbleFile $dataDir & GAMENAME & "-bubble.txt"
readToArray $bubbleFile $bubbleArray


Now, I have been trying to use the array, which is from an include script, as a multidimensional array. Here is how I am trying to add a variable to the 2nd dimension with this line of code :

Code:
        setVar $_bubbleArray~bubbleArray[$b][$v] 1


So, what mistake(s) am I making?
Does a multidimensional array need to be handled differently when it is from an include?
Is my syntax wrong?
Do I have to declare the multidimensional array first in the script somehow?

For the syntax :
I have tried [[$b][$v]]
I have tried [$b[$v]]
I have tried [$b][$v]
I have tried [$b[$v]

Appreciate any help anyone can give, and maybe some pointers for using multidimensional arrays. :)

Edit : The single dimension array I am using for this is working fine, shows up right in echo's as well as doing a $sv to check the script variables.

Author:  Decker [ Thu May 17, 2012 5:47 am ]
Post subject:  Re: multi dimensional arrays

ReadToArray creates a single dimensional Array. You cannot initialize other dimensions once an array has been delcared.

If you really, really, need the additional dimensions, then you will have to populate another multidimensional array with the values found in the Array created by ReadToArray:

Code:
readtoarray $File $Temp
setarray $BUBBLES[$TEMP][1]
setvar $i 1
while ($i <= $TEMP)
  setvar $BUBBLES[$I] $TEMP[$i]
  add $i 1
end

Author:  SteveH_66 [ Thu May 17, 2012 10:37 pm ]
Post subject:  Re: multi dimensional arrays

Thanks Decker, that cleared up some of it. :) Would be nice to see some scripts that use multidimensional arrays so i could get a look at how they work. Hopefully someone will see this and know of a public script that makes use of them so I can study them. I'll go through the ones I have looking for a script that uses one.

Author:  Decker [ Fri May 18, 2012 6:03 am ]
Post subject:  Re: multi dimensional arrays

Read this: http://helixtw.podzone.org/ls_dny_help/ ... _101p1.htm

Check out Helixs site for more stuff: http://helixtw.podzone.org/

As for scripts that use arrays, all you need to do is collect as many source scripts as you can, and study them. Generally, the larger the file-size, the more likely you'll find arrays being used.

If you have an idea for data structures write it out on paper first and see how feasible your idea is. Sometimes, multidimensional arrays are counter productive.

Author:  ElderProphet [ Sun May 20, 2012 12:51 pm ]
Post subject:  Re: multi dimensional arrays

Decker wrote:
ReadToArray creates a single dimensional Array. You cannot initialize other dimensions once an array has been delcared.

Sure you can, you just have to initialize them one at a time. So let's say you want to read a file into an array, and then create a new 5-element array for each element in the ReadToArray array:
Code:
ReadToArray $file $array
setVar $i 1
while ($i < $array)
  setArray $file[$i] 5
  add $i 1
end


Here is a brief write up I made on arrays: http://twxscripts.com/twxarrays1

Additional questions?

Author:  SteveH_66 [ Mon May 21, 2012 12:50 am ]
Post subject:  Re: multi dimensional arrays

Thanks for the reply EP, I kinda got what you're saying about the arrays and I took a look at your tutorial which was interesting. I have to study the while loop code a bit more to figure out exactly what you're doing in it, although I think I have a grasp on it basically.

But I'm having trouble putting the multidimensional array to work, I did a simple test script.

OK, I wrote a simple script trying to figure out multidimensional arrays. I made 2 files, one with 30 lines to read to an array called bubbleArray, and one to read to an array called figArray which is 15 lines. The first 10 elements in figArray will match elements in bubbleArray, the last 5 will not. I converted the bubbleArray to a multidimensional array called sectorArray, and it seems to populate correctly.

Then, I wrote some IF conditionals in :part1 that compare the figArray to sectorArray. I put in some echo's for debugging and I have been using $sv to list out the variables after the script runs.

Code:
setVar $dataDir "gameData\"
setVar $figsFile $dataDir & GAMENAME & "-mdaFigsFile.txt"
setVar $bubbleFile $dataDir & GAMENAME & "-mdaBubbleFile.txt"

readToArray $figsFile $figArray
readToArray $bubbleFile $bubbleArray
setArray $sectorArray[$bubbleArray][1] $bubbleArray

setVar $b 1
setVar $f 1
setVar $s 1
setVar $ss 1
setVar $i 1

  WHILE ($b <= $bubbleArray) AND ($f <= $figArray)
    setVar $sectorArray[$s] $bubbleArray[$b]
    setVar $sectorArray[$s][$ss] 1
    add $b 1
    add $s 1
    add $ss 1
  END

setVar $b 1
setVar $s 1
setVar $ss 1
goto :part1
PAUSE

:part1
echo "*" "At part1"
echo "*" & $s
echo "*" & $sectorArray[$s] & " " & $figArray[$f] & " " & $sectorArray[$s][$ss]
  WHILE ($s <= $sectorArray)
         IF ($sectorArray[$s] = $figArray[$f]) AND ($sectorArray[$s][$ss] = 1)
           setVar $sectorArray[$s][$ss] 2
           echo "*" $figArray[$f] & " has been added to the Array"
           add $f 1
           IF ($f = $figArray)
             setVar $f 1
             add $s 1
             add $ss 1
           END
           pause
         END
         IF ($sectorArray[$s] = $figArray[$f]) AND ($sectorArray[$s][1] = 2)
           echo "*" $figArray[$f] & " is in the Array"
           add $f 1
           IF ($f = $figArray)
             setVar $f 1
             add $s 1
             add $ss 1
           END
           pause
         END
         IF ($sectorArray[$s] <> $figArray[$f])
           echo "*" $figArray[$f] & " is not in the Array"
           add $f 1
           IF ($f = $figArray)
             setVar $f 1
             add $s 1
             add $ss 1
           END
           pause
         END
  END

goto :part2
PAUSE

:part2

  WHILE ($s <= $sectorArray)
    IF ($sectorArray[$s][$ss] = 0)
      echo "*" & "no fig in sector " & $sectorArray[$s]
    END
  END
PAUSE


As you can see, I also put in pauses, so that I could have the script make one run through and stop, so that I could check out what it is doing. As I said before, it seems that it is setting the $sectorArray as I want, with the sector number and the 2nd dimension of the array all set to 1.

But, it seems that I am not doing something right in the IF conditionals. As you can see from the echo's I put in, it is showing that the first elements of $figArray and $sectorArray match and that the first element of the 2nd dimension of $sectorArray is set at 1.

But, after running the script, it seems that the conditionals are not working right. I don't know if my logic flow (is that the right programming term?) in the conditionals is wrong, or if the syntax I am using as I set them up is wrong.

Or, am I missing an IF conditional that is needed to make the script work right? What puzzles me is that even if I am missing a needed conditional routine, it looks to me like the condition of the first IF routine IS met right away, $sectorArray[$s] and $figArray[$f] do match, and the 2nd dimension of $sectorArray[$s][$ss] is showing as 1. So, I don't understand why it is not resetting the 2nd dimension element to 2.

Thanks for any help and insight someone can give me here, sorry about the long post, but I wanted to show all the pertinent information about the script code and the results I am getting.

Author:  ElderProphet [ Mon May 21, 2012 12:23 pm ]
Post subject:  Re: multi dimensional arrays

I'm willing to help you, but there's a few things wrong in your code, and your post is too long... it would take me an hour to understand all that you're trying to do. Keep code samples to just a few lines... less than 10 if possible, and you'll receive much better help, and from more people.

So, here's the real question... why are you trying to use a multidimensional array? Can you accomplish what you are attempting using several single dimension arrays? If so, do that, at least until you understand arrays better. If you haven't mastered single dimension arrays and while loops (iteration), then you are just muddying the water further trying to understand multidimensional arrays. Throw an include into the mix, and you've set yourself up for failure. It would be wise to first ask for clarification on iteration, then arrays, then includes before tackling a script that has all of these elements in it. So where would you like to begin?

Author:  SteveH_66 [ Mon May 21, 2012 4:03 pm ]
Post subject:  Re: multi dimensional arrays

OK, thanks EP, I'll try to keep it to shorter amounts of code. This was basically an attempt to understand multidimensional arrays better. It could probably be done with something like 3 arrays and nested WHILE loops. I kinda got side tracked off on MD arrays once I thought I knew how they worked.

Here's what I'm working on. First, I wrote a script that plotted courses to every sector in a bubble, writing them out to a file. Then I edited the output file manually, cutting out the duplication in the lines, like

19463
19463 5483
19463 5483 10202
etc.

until I got to where I was actually getting a route to a deadend or sector not covered in a route to a dead end for some reason. The next script I am working on is basically for double checking the courses, one after another, to make sure that every sector in the bubble is still covered in one of the routes - to make sure I didn't accidentally cut out a line with a route to a side deadend or something you know.

There are a couple of ways I could go about doing it I suppose. I could use 2 single dimension arrays, one with the lines of courses and the other a list of sectors in the bubble, and use a couple of nested WHILE loops to step through it, creating a 3rd array to hold the sector numbers as they are 'checked off', with code to make sure that a 'checked off' sector is never repeated in that array.

My idea with the multidimensional array was to use it for the bubble sectors, and then the other one with the courses array. Step through the course array one sector at a time on each line, using the 2nd dimension of the bubble array to hold a value depending on if it had already been seen before, or hadn't been seen.

It seems to me that it is going to be a fairly complicated script no matter which way I do it, but as you said maybe I'm better off sticking to like 3 arrays and doing it that way. Anyway, I'll try it that way. Thanks

Author:  ElderProphet [ Tue May 22, 2012 9:01 pm ]
Post subject:  Re: multi dimensional arrays

I've done something similar in an eprobe script. And I see a use for 2 dimension array, but not for what you had in mind. I'll often use a 2 dimension array for courses. So in your scenario, $course[25] might be "1 2 9 430 25", right? I would instead build mine so that $course[25] = "5", the number of hops in the course, which will equal the number of elements in the 2nd dimension., then that dimension would be as follows:
$course[25][1] = 1
$course[25][2] = 2
$course[25][3] = 9
$course[25][4] = 430
$course[25][5] = 25

This makes it easier for me to step through the warps, vs. using getWord on a string like "1 2 9 430 25". But again, this is non-trivial if you're not really comfortable with arrays and iterators, so feel free to seek further clarification or examples.

P.S. If we're going to keep this thread going, please edit that super long post and nix the variable dumps.

Author:  SteveH_66 [ Wed May 23, 2012 12:37 am ]
Post subject:  Re: multi dimensional arrays

OK, post edit is done EP, sorry I didn't think to go back and do that on my own. Anyway, I just looked at my posts here, I didn't really lay out my reasoning for wanting to use the 2 dimensional array to check courses, it's all a part of a collection of scripts I have written trying to do one thing, which I talked about in another thread. And I didn't explain my reasoning too well in that post either, judging from some of the replies I got in that post as well.

The scripts are all an attempt on my part for the most turn efficient bubble management. That post is here http://classictw.com/viewtopic.php?f=15&t=33176 and I should probably go back in there and make another post, further explaining my reasoning and what I am trying to do.

Anyway, this latest script is to try to make sure that every sector in the bubble is visited in at least one course, so that I don't miss a sector during bubble maintenance. I edited the data file that my course plotting script output, trying to eliminate redundancy of lines, and I just want to write this script to ensure that while cutting lines out, I didn't accidentally cut a line that has a sector not visited in any of the other lines (courses) in the data file.

In my above reply, I suppose I didn't clearly illustrate what I was talking about, my reasoning for why I was trying to use the multidimensional array in the way I did. But I didn't want to make too long a post with pasted in stuff, after having made that long post I just edited with the variable dumps in it that you asked me to edit because it was too long.

Perhaps I should go back to that thread, further explain in that post what I am trying to do overall here, and then we can pick up this discussion after I have better explained the concept over there, if you think I am going about this the right way. I might be going about it all wrong and you or someone can tell me so in that post, and we can pick up the discussion of multidimensional arrays here if you think one would be useful in what I am trying to do here overall, but by doing it in a different way.

If you would like me to show you what I meant by editing the data file, I can make another post here, showing a before and after edit of a short section of the data file, 5 or 6 lines illustrating what I'm talking about.

Thanks for your help on the multidimensional arrays, I AM learning a lot from what we have discussed in here so far. :D

Author:  SteveH_66 [ Thu May 24, 2012 11:41 pm ]
Post subject:  Re: multi dimensional arrays

In the interests of keeping my post short, instead of putting the code here in the post, I am attaching a copy of my script that plots the courses in a bubble from the gate to every sector in the bubble.

The zip contains 2 versions of the script, one uncommented and the other pretty heavily commented, the commented one might be useful to beginners to see what I am doing at each step of the code. It might also help to show newbies like me what Singularity and other scripters who recommend heavily commenting your code mean about why you should do that - so that when you come back weeks or months later and look at the script, you may be able to figure out what the heck you were doing with it. :D If you can see a use for a script like this, feel free to download it and use it.

A few changes to the script and running it will show you quite obviously what I mean by redundant lines in the output file, lines that I manually edited out.

It should also be quite obvious that while manually editing an output file like this, one could accidentally cut out a route to a dead end or other sector not visited in any of the other lines (courses) of the output data file.

Once you understand what I am saying here, you can understand why I would want to do a script to double check all the lines (courses) to make sure no sector is left unvisited. Maybe I could have coded the course plotter script better, to keep from having these redundant lines, but this was the best code I could think of for doing what I wanted to do.

NOTE : I decided to do it this way because it also makes a file that, after you delete out the redundant lines (courses) some further editing allows you to have a file that gives you a pretty good 'text map' of your bubble. :D

I decided to try to use a 2 dimensional array in the double check script, and that led to this discussion.

Attachments:
bubbleCoursePlotter.zip [2.94 KiB]
Downloaded 570 times

Author:  SteveH_66 [ Fri May 25, 2012 12:02 am ]
Post subject:  Re: multi dimensional arrays

OK EP, I added another post in that other thread which I link to above, trying to clarify the general idea behind what I am trying to do here overall. Now about the double check script and why I was trying to use a multidimensional array in it.

For the script, I am starting out with 2 data files, one is bubbleCourses.txt, and the other is bubble.txt. bubbleCourses.txt has lines of courses in it, bubble.txt has a list of every sector in the bubble. I read both of them into arrays, one called coursesArray and the other called bubbleArray.

My idea was to use a new array called sectorsArray, populating it in the 1st dimension with the bubble sectors from bubbleArray, and making sectorsArray a 2 dimension multidimensional array. My idea was to use the 2nd dimension as the double check, setting all the values in the 2nd dimension to 1.

I was going to step through the coursesArray one element at a time. I would check each sector in the course against the sectorsArray, against the bubble sector in the first dimension. If it had not yet been seen in this routine, the value would be 1 in the 2nd dimension, and I would reset it to 2. If it had already been seen, it would have already been reset to 2 in the 2nd dimension, so I would skip over it.

In the next routine, I would check sectorsArray one element at a time. If the 2nd dimension element is 2, I know that it was found in the double-check routine above. If it is a 1 then I know it wasn't found and I can echo it out and/or write it to a file of not-found sectors.

Does it sound logical to do it this way, or do you think there is a better way to do it? A better way to use the multidimensional array? Or for simplicity would it be better to just use a 3rd array as the double check? Thanks for your help and insight on this matter. :D

Author:  ElderProphet [ Tue May 29, 2012 9:55 am ]
Post subject:  Re: multi dimensional arrays

I've responded with a concise, nearly complete example in the other thread here. Have you had a chance to look that over?

Author:  SteveH_66 [ Thu May 31, 2012 2:55 am ]
Post subject:  Re: multi dimensional arrays

Yes I have EP, thanks for your help and the code, now I'm studying the code and doing some testing. As you advised, trying to grasp the concepts and what the code is doing at each point. As always, your help is much appreciated :D

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/