A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: split() command :: arrays?

  1. #1
    Senior Member 7year's Avatar
    Join Date
    Oct 2003
    Posts
    408

    split() command :: arrays?

    I cannot figure out how to get an indexed array from a delimited string for the life of me. Someone told me to do this

    Code:
    array=delimitedString.split("|");
    since i used the '|' as my delimiter when sending the variable to flash. but when i run this, all it does is give me this:

    say string = "value1|value2|value3|value4"

    i do trace(array[0]) i get "undefined". same for other index numbers. but if i do trace(array) i get value1,value2,value3,value4

    what the Heck!!! i am so fed up with this. this is all that has been holding me up for hours!

    TIA for any help you can offer
    FASCISM DOESN'T ALWAYS LOOK LIKE ADOLF HITLER.

  2. #2
    Senior Member
    Join Date
    Apr 2004
    Location
    Missouri
    Posts
    384
    This works for me.

    code:

    delimitedString = "234|24|532|7|45|235";
    thearray=delimitedString.split("|");
    trace( thearray[0] );


    (traces 234)

    If it doesn't work try using the Debugger and looking at the variables themselves.

  3. #3
    Senior Member 7year's Avatar
    Join Date
    Oct 2003
    Posts
    408
    Does that work in flash MX? i am doing exactly that. and it doesn't work for me. exact same code. I made a test movie and all it does is make a delimited stringd then assign an array with delimitedString.split("|") and i can't use the array indexes. I don't understand. I did it at work and at home. two separate computers, two separate copies of Flash MX and neither worked.
    FASCISM DOESN'T ALWAYS LOOK LIKE ADOLF HITLER.

  4. #4
    Senior Member
    Join Date
    Feb 2002
    Posts
    536
    Originally posted by 7year
    Does that work in flash MX? i am doing exactly that. and it doesn't work for me. exact same code. I made a test movie and all it does is make a delimited stringd then assign an array with delimitedString.split("|") and i can't use the array indexes. I don't understand. I did it at work and at home. two separate computers, two separate copies of Flash MX and neither worked.
    just 1 question... are u loading an external data file?

  5. #5
    Senior Member 7year's Avatar
    Join Date
    Oct 2003
    Posts
    408
    i am actually using data returned from php which is sent in the format &arrayName=value1|value2|value3|value4, but that shouldn't matter because all the data taken in from LoadVariables is taken as strings. I also tried making an empty flash file that only had the following code:

    stringVar="value1|value2|value3|value4";
    arrayVar=stringVar.split("|");
    trace(arrayVar[0]);

    i get undefined for the trace value

    if i do trace(arrayVar) i get this value1,value2,value3,value4
    all it does is replace the pipe symbols '|' to commas.
    FASCISM DOESN'T ALWAYS LOOK LIKE ADOLF HITLER.

  6. #6
    Senior Member 7year's Avatar
    Join Date
    Oct 2003
    Posts
    408
    ok, heres a new update. I got it to return the values in a trace, if i assign them manually. Now, i have a new problem, though the values are available through trace, they don't come up in a dynamic text field. what the hell!!!! Is it making an array of strings, or not?
    FASCISM DOESN'T ALWAYS LOOK LIKE ADOLF HITLER.

  7. #7
    Senior Member
    Join Date
    Feb 2002
    Posts
    536
    here's what i would do...

    code:


    var _f = new LoadVars();

    _f.load ("yourScript.php");
    _f.onLoad = function () {

    myArray = this.arrayName;
    arrayVar = myArray.split("|");

    for (i in arrayVar) trace (arrayVar[i]);

    }





    And to display it...

    yourTextField.text = arrayVar[index]

  8. #8
    Senior Member 7year's Avatar
    Join Date
    Oct 2003
    Posts
    408
    ok, some progress!!! i got the array[index] to display in a textfield, but it only works when the text for the textfield is assigned(fieldName.test=arrayName[0]) inside of the onLoad function. This is so hard! AS sucks compared to other languages
    FASCISM DOESN'T ALWAYS LOOK LIKE ADOLF HITLER.

  9. #9
    Senior Member 7year's Avatar
    Join Date
    Oct 2003
    Posts
    408
    i also tried assigning a new _global array to the same variables as the one inside the onLoad function, but i cant access those either
    FASCISM DOESN'T ALWAYS LOOK LIKE ADOLF HITLER.

  10. #10
    Senior Member
    Join Date
    Feb 2002
    Posts
    536
    Originally posted by 7year
    ok, some progress!!! i got the array[index] to display in a textfield, but it only works when the text for the textfield is assigned(fieldName.test=arrayName[0]) inside of the onLoad function. This is so hard! AS sucks compared to other languages
    u can only display your data when it was fully loaded... thats why u can have it displayed inside the onLoad event.

    code:

    /* ******
    file.txt

    varSome=1|2|3|4

    */

    var a = new LoadVars();
    a.load ("file.txt");
    a.onLoad = function () { trace (this.varSome) }
    // returns 1|2|3|4

    trace (varSome);
    // returns undefined coz it will be executed b4 u load your txt file (or php script).





    Following that clue u need to trigger your event inside the onLoad function...

  11. #11
    Senior Member 7year's Avatar
    Join Date
    Oct 2003
    Posts
    408
    i did fix it in fact, and that was the problem. I did this.

    the php script returns it like this now:
    &arrayName=value1|value2|value3|value4&completeHom eLoad=yes

    then i did this on frame 3:
    if(completeHomeLoad!="yes") {
    gotoAndPlay(2)
    }

    when on frame four i set the text field values. works like a charm. Thank you everyone for helping me reach a conclusion.
    FASCISM DOESN'T ALWAYS LOOK LIKE ADOLF HITLER.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center