September 23, 2003

easy array output

Here's a handy way I found to output the contents of an array in an easily readable way, with a single line of code.

trace(myArray.toString().split(',').join('\n'));

It takes the toString output of the array, which is comma-delimited, then does a find and replace, inserting linebreaks where the commas were. This keeps you from having to write a "for" loop just to output the contents of one array.

You could also use the array.toString.split.join trick to populate a textbox with the contents of an array, in order to display a list of items.


-- correction --

I was quickly corrected in the comments: skip the toString & split and just do trace(myArray.join("\n"));

"It looked like a good idea at the time..." :)

Posted by andy at September 23, 2003 04:24 PM
Comments

You convert the array to a string.
Then split the string (convert to an array)
then join the array..

Too much steps

A quicker way

trace(myArray.join("\n"));

cheers

Posted by: sangles at September 23, 2003 05:21 PM

I guess I've just had a long day...

openDoor().closeDoor().openDoor().fetchBeer()

:D

Sometime you look at so much code during the day that you miss the obvious.

thanks

Posted by: andy makely at September 23, 2003 05:29 PM
Post a comment