Jump to content


Photo

Using Arrays In Actionscript


  • Please log in to reply
4 replies to this topic

#1 Kthulu

Kthulu

    X-S Freak

  • XS-BANNED
  • PipPipPipPipPip
  • 1,287 posts
  • Xbox Version:v1.0
  • 360 version:unknown

Posted 13 November 2003 - 06:16 AM

;here's a 2x2 array of 4-digit numbers...
SET myArray "1025367852148526"

you might be wondering how much crack i've been smoking cause that just looks like a string of numbers. well, it is. but check this out...

;get value stored in myArray(0,0) and store it in myValue1...
SETFUNC myValue1 MID 0 4 %myArray%

;get value stored in myArray(0,1) and store in myValue2
SETFUNC myValue2 MID 4 4 %myArray%

;get value stored in myArray(1,0)...
SETFUNC myValue3 MID 8 4 %myArray%
^
;notice how these nums^ are progressing...

this is basically what a high-level language does for you when you say 'myValue = myArray(1,1). when computers store arrays in memory, it's a linear and contiguous set of values. what makes it an array is how you access it. most high-level languages encapsulate this process.

so, if you find yourself wishing you could create an array, you can! smile.gif

Edited by Kthulu, 13 November 2003 - 06:57 AM.


#2 Kthulu

Kthulu

    X-S Freak

  • XS-BANNED
  • PipPipPipPipPip
  • 1,287 posts
  • Xbox Version:v1.0
  • 360 version:unknown

Posted 13 November 2003 - 06:55 AM

here's another tip for making arrays in actionscript...

let's say you want to create an array that contains strings created by the user (i.e. you don't know what's gonna be in there) and you want to be able to pull string1, string2, etc. out of it when it's finished. you can easily use MID to pull a substring from a string, but you don't how long each string is. well, the solution is to keep track of how long each string is when it goes into the array.

let's say the user enters the following strings: "hulk", "jedi starfighter", "hunter", "obi-wan"
as long as the strings themselves don't start with numbers, you can store the string lengths in front of the strings as they are entered and then you know how much to get each time you pull from the array.
for example, when the above strings were finally stored in an array (one big string) it would look like this...

myArrayOfStrings contains "04hulk16jedi starfighter06hunter07obi-wan" (note you'd have to 'pad' for strings whose length is less than 10)

then you can do this...
SET string1len 0
SETFUNC string1len MID 0 2 %myArrayOfStrings%
SETFUNC string1 MID 3 %string1len%
ADD string1len 2
SETFUNC string2len MID %string1len% 2 %myArrayOfStrings%
ADD string2len %string1len%
SETFUNC string2 MID %string1len% %string2len% %myArrayOfStrings%
ADD string2len 2
SETFUNC string3len MID %string1len% 2 %myArrayOfStrings%
ADD string3len %string2len%
SETFUNC string3 MID %string1len% %string2len% %myArrayOfStrings%
ADD string3len 2
...etc...

Edited by Kthulu, 13 November 2003 - 06:58 AM.


#3 geniusalz

geniusalz

    Team MXM

  • Head Moderators
  • PipPipPipPipPip
  • 1,827 posts
  • Gender:Male
  • Xbox Version:v1.1
  • 360 version:unknown

Posted 16 November 2003 - 01:01 AM

QUOTE (Kthulu @ Nov 13 2003, 03:16 AM)
;here's a 2x2 array of 4-digit numbers...
SET myArray "1025367852148526"

you might be wondering how much crack i've been smoking cause that just looks like a string of numbers. well, it is. but check this out...

;get value stored in myArray(0,0) and store it in myValue1...
SETFUNC myValue1 MID 0 4 %myArray%

;get value stored in myArray(0,1) and store in myValue2
SETFUNC myValue2 MID 4 4 %myArray%

;get value stored in myArray(1,0)...
SETFUNC myValue3 MID 8 4 %myArray%
^
;notice how these nums^ are progressing...

this is basically what a high-level language does for you when you say 'myValue = myArray(1,1). when computers store arrays in memory, it's a linear and contiguous set of values. what makes it an array is how you access it. most high-level languages encapsulate this process.

so, if you find yourself wishing you could create an array, you can! smile.gif

How would you go about changing the value of any item?


btw:
I had a somewhat simpler idea of using a dummy xml in memory.

XMLOPEN testXML c:test.xml
XMLCREATE testXML dummy

XMLSetValue testXML !.Array:%INDEX% %blah%
XMLGetValue testXML blah !.Array:%INDEX%

XMLClose testXML

#4 Kthulu

Kthulu

    X-S Freak

  • XS-BANNED
  • PipPipPipPipPip
  • 1,287 posts
  • Xbox Version:v1.0
  • 360 version:unknown

Posted 16 November 2003 - 02:19 AM

the xml looks/sounds alot easier. does it create a file on the hard-drive?

to change a particular value, you break the string into a leftHalf and rightHalf then glue your new value in between them...
%leftHalf%%newValue%%rightHalf%

yes, alot of work! my point of this tho was to get people to (forgive the cliche) think outside the box. have you tried that xml thing? as long as it doesn't leave file-droppings all over the place, i think that's the ticket for complex storage structures.


#5 geniusalz

geniusalz

    Team MXM

  • Head Moderators
  • PipPipPipPipPip
  • 1,827 posts
  • Gender:Male
  • Xbox Version:v1.1
  • 360 version:unknown

Posted 16 November 2003 - 02:25 AM

Hmm...

XMLCreate might work without XMLOpen

So yeah, as long as you don't use XMLSave, it shouldn't write anything to file.

But never really tried it.

Edited by geniusalz, 16 November 2003 - 02:25 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users