all groups > flash actionscript > april 2004 >
You're in the

flash actionscript

group:

fun with arrays, how do I do this???



fun with arrays, how do I do this??? xxx
4/26/2004 11:32:31 PM
flash actionscript: I am using flash 5 (Can't get the money approved for MX sux to be me)
I have a loadvariablenum call thru php that sends flash info like so:

line=thomas,smith

Then I am trying to assign the first string in the array to a
variable.
The array comes in fine and I can even use the array in whole as a
variable.
But I can't seem to make a call to just the first string.

function info () {
theVars = line;
theArray = new Array();
theArray = theVars.split(",");
ArrayName[0] = "first";
ArrayName[1] = "last";
}

output for variable "line" = thomas,smith
output for variable "first" = nothing

What is it that I am missing? How do I either assign the first split
of the array to a variable name, or set it as a variable?

Thank you.
Re: fun with arrays, how do I do this??? mandingo
4/27/2004 5:36:54 AM
Hmmm,

I am using MX but something like this may help you...

line = "Thomas,Smith";

function info() {
theVars = line;
theArray = new Array();
theArray = theVars.split(",");

namesArray = new Array();

createName = new Object();
switch(theArray.length){
case 2:
createName.surName = theArray[theArray.length -1];
createName.firstName = theArray[0];
break;
default:
createName.surName = theArray[theArray.length -1];
createName.firstName = theArray[0];
createName.secondName = theArray[1];
}
namesArray.push(createName);
}
info();

trace("The person in position 0 has a surname of : " + namesArray[0].surName +
" and a first name of " + namesArray[0].firstName);

output should be : "The person in position 0 has a surname of : Smith and a
first name of Thomas"

This way would also allow for a first second and surname...

never having migrated from 5 to MX, I don't know what is acceptable in that
envrionrment

but maybe it will help
cheers,
Re: fun with arrays, how do I do this??? mandingo
4/27/2004 5:40:36 AM
actually, namesArray should be instantiated outside the 'info' function call
.... otherwise it will be reset each time the function is run...

namesArray = new Array();
// then
function info(){
theVars = line;
// etc....
Re: fun with arrays, how do I do this??? xxx
4/27/2004 7:55:53 AM
Thank you, I see I was trying to go about things the wrong way.

I appreciate all you guys help

Re: fun with arrays, how do I do this??? Peter Weidauer
4/27/2004 2:38:44 PM
[quoted text, click to view]


Thomas, I'm not quite sure what you are trying to accomplish but the
above function creates three new variables:

1) theVar, which holds the same string as line
2) theArray, which is the array [ "thomas", "smith" ]
3) ArrayName, which is the array [ "first", "last" ]

A variable "first" is never created. If you want to access the parts of
the name as "last" and "first" you should write:

first = theArray[0]
last = theArray[1]


You could also do it without a special function like this:

var firstName = line.split(",").shift();
var lastName = line.split(",").pop();

or

var firstName = line.split(",")[0];
var lastName = line.split(",")[1];


HTH,
Peter
AddThis Social Bookmark Button