dcsimg
A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Is there no function for scrolling thru an array?

  1. #1
    Special Member Tea_J's Avatar
    Join Date
    Dec 2000
    Posts
    991

    Is there no function for scrolling thru an array?

    Hi Guys

    I deal w/ arrays a lot in flash and i usually find myself doing:

    f1 = row[6];
    f2 = row[7];
    f3 = row[8];
    f4 = row[9];
    f5 = row[10]


    and imagine sometimes i have 50 array elements.. and i have to declare them to a variable 1 by 1.. i wish there was a function to enumerate through an array 1 by 1, that moves the pointer next or back.. like in PHP, to make our lives easier.. is there?

    something like:
    f1 = NextArrayElement(row);
    f2 = NextArrayElement(row);
    f3 = NextArrayElement(row);
    f4 = NextArrayElement(row);
    f5 = NextArrayElement(row);


    looked thru the docs and i cant find anything.. i tried doing:


    a_i=0; //array index

    cid2 = row[0];
    sid_pri = row[a_i++];
    sid_sec = row[a_i++];

    bid = row[a_i++];
    model = row[a_i++];

    but that didnt quite work lolz..



    ofcourse i can make a function for this, but i wish there was a native function.....

    or is there?

  2. #2
    Junior Member
    Join Date
    Jun 2011
    Posts
    10
    for(var i in row){
    var something = row[i];
    }

    var a_i=0;
    var something = row[a_i];
    a_i++;
    var something2 = row[a_i];
    a_i++;

    Only way I can think to do what you need... You should consider restructuring your code if the for-loop won't do it... There should be a much simpler way to do what you need, perhaps if you explained what you're trying to do further.

  3. #3
    Senior Member
    Join Date
    Jun 2003
    Location
    Kent, WA
    Posts
    536
    Like Virual said, I'm not quite sure what you're doing, but I'm pretty sure there's a better way to do it.

    Why you are taking the elements of an array and assigning them to variables? What is the advantage of using an array at that point? Why not just use an object instead?

    Code:
    var myObj:Object = {f1:data1, f2:data2, f3:data3, f4:data4};
    Then all the data is available directly from the object.

    If you're going to use an array, the most common usage is to step through it one element at a time, because typically you don't assume to know what is in each element of the array. Only that it is a certain datatype. (Again; generalizing. You can obviously do whatever you want with arrays.)

    Code:
    for (var i=0; i<row.length(); i++)
    {
       trace(row[i]);
    }
    If you ARE assuming to know what is in each element (as you seem to be from your code above), again... maybe objects, or even separate class entirely, seem to be better fitted to your needs, as you can then explicitly define member data.
    Last edited by marshdabeachy; 06-28-2011 at 06:24 PM.

  4. #4
    Special Member Tea_J's Avatar
    Join Date
    Dec 2000
    Posts
    991
    thanks guys but these arent what im looking for

    I can easily loop through arrays using the above loops but that's not possible since i need to assign these array elements into specific variables, not f1 f2 f3 only.. as per my example above..

    Why you are taking the elements of an array and assigning them to variables? What is the advantage of using an array at that point? Why not just use an object instead?
    well i need to assign them to variables as i would eventually need to assign them to data fields or whatever use i need.. and i do this for clarity also so i know what data im dealing w/ and i can assign these clearly, w/ less coding and less error..

    Sureless it's easier to do
    tf_name.text = customer_name;

    vs

    tf_name.text = customer[1];

    specially if i plan to use this value again and again.. and it makes more sense , easier to read my code.

    same thing when i deal w/ XML , if i plan to , say fo example, vie and then edit customer information coming from XML, i always assign them to variables w/ more meaningful names than dealing /accessing nodes all the time.. and it makes it so much easier to send these values to a script for processing.. since these are already VARIABLE=VALUE pairs i played with..

    so yeah i need a next() function like in php to make my life of assigning arrays to "meaningful" variables easier..


    PS, i already made a function for this and been using it

    arrayNext Function v1
    Function for enumerating through an array
    1) call the function w/o targetArray first = arrayNext();
    - this is to reset the indexer/counter
    2) feed the function the array as you call it it will move the pointer forward
    */
    _global.arrayNext = function (targetArray){

    if(targetArray == undefined){ // if undefined, consider as reset/initiate
    arrayPointer = 0;
    }else{
    curArray = targetArray;
    curArrayData = curArray[arrayPointer];
    arrayPointer++;

    return curArrayData;

    }

    }
    just wish there was a native fucntion for this..
    Last edited by Tea_J; 06-29-2011 at 03:14 AM.

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    10
    Just use an object then... Instead of assigning your data to an array...

    customer.name = somedata

    or use another type of array:

    customer["name"] = somedata
    This way you can still loop through the data with a for-loop if you need.

  6. #6
    Special Member Tea_J's Avatar
    Join Date
    Dec 2000
    Posts
    991
    @virual

    ... nope no can do...

    here's another reason why..

    my phpscript outputs say a comma separated value

    name,age,sex

    in Flash, you'll want to break that up into usuable variables.. so ofcourse you split() it right? and splitting it makes arrays out of the CSV string ... hence i need to convert that array into usuable var=value pairs..


  7. #7
    Junior Member
    Join Date
    Jun 2011
    Posts
    10
    Use LoadVars to get your data from php into an object...
    PHP output should look like this:

    &var1=value1&var2=value2&var3=value3

    Using loadvars you will get an object like this...
    obj.var1 == value1
    obj.var2 == value2
    etc

    Code:
        var result_lv:LoadVars = new LoadVars();
            result_lv.onLoad = function(success:Boolean) {
            if (success) {
                //Run your function to handle the data here
                trace(result_lv.name)
            } else {
                //failed to load the data
            }
            };
    
            var send_lv:LoadVars = new LoadVars();
            send_lv.name = somevalue //data to POST to php script.. use $_POST['name'] to get somevalue in your php script
            send_lv.sendAndLoad("http://www.yoursite.com/yourscript.php", result_lv, "POST");
    Now if you set up your php script to output the data in the format like so:
    &name=customer_name&age=23&location=vancouver

    Your LoadVar object will contain:
    result_lv.name == customer_name
    result_lv.age == 23
    result_lv.location == vancouver


  8. #8
    Special Member Tea_J's Avatar
    Join Date
    Dec 2000
    Posts
    991
    thanks for the inputs.. but hehe no.. i used to do that.. it aint worth it.. too much more code, from both php and As2 side.. when i can simply output comma delimitted values or values in similar fashion.. just the outputting of var=value pairs from php is already too tedious and prone to typos (specially when im speed coding).. sometimes instead of

    name=$name pairs i type
    $name=$name etc.. and then takes me long time to figure wtf is wrong w/ my script hehe

    and not to mention the need to handle special characters when dealing w/ var=val pairs..... you should try just throwing xml or csv values into your flash soo much easier.. less code less mistakes.. just gota get the hang of it.. hehe

  9. #9
    Senior Member
    Join Date
    Jun 2003
    Location
    Kent, WA
    Posts
    536
    Quote Originally Posted by Tea_J View Post
    less code less mistakes..
    I'd argue against that. Clearer code, less mistakes. You can cram it as small as you want, but if you can't understand it, it won't make any difference. Don't get me wrong, I don't write any more code than I need to. But I'd probably be using a class or object for what you're attempting.

    I've dealt with the assumption that certain array elements contain certain values, and it's much more prone to errors than directly assigning those values into an object. Coding is generally 10% typing and 90% debugging. Being able to understand what's going on is much more important than doing it as quickly as possible.

    To each their own, I guess.

  10. #10
    Special Member Tea_J's Avatar
    Join Date
    Dec 2000
    Posts
    991
    hi,

    well i do appreciate the inputs and ideas so thanks much..

    yeah i guess it is where one is more comfortable with, and after almost a decade of coding php and actionscript , and jumping onto the OOP boat , i've always been able to finish my projects quicker w/ being less of a "code critic" and more of a practical approach.. i do admit sometime's debuggin can be a b*tch haha.. but i've learned to deal w/ them and actually have functions and other special functions and elements in my projects that help me debug easier..


    one thing i liked w/ my style, of var=value etc, is that it's just soo much easier..

    each textfield in my script is attached w/ code such as:

    onClipEvent (load) {
    myVar="summary";
    if(eval("_parent." + myVar) == undefined){

    }else{
    this.text = eval("_parent." + myVar);
    }

    }

    on (change) {
    _parent[myVar]=this.text;

    }
    this way, i can make a bunch of copies of this textfield and just change the myVar value on each TF mc, and viola, my form is done, and all user inputs is ready to send via old school loadVariables or even the load(link); function.. i need not declare each user input into an object's variable for loadVars and all that..


    what's best about this is, when loading variables into these fields when one wants to view the data submitted...

    no painstaking assigning to each text field like:

    tf_name.text = this.name;
    tf_age.text = this.age;


    etc..

    here's an example of one of my routines to load variables outputted from a phpscript,

    http://d.pr/zxKP
    as you can guess i have my own DELIMITATION for values i send from my script.. these come into my script bare and w only numerical indices, so the guess work is there, but nothing a little declaration ORDERING from both PHP and AS2 side can handle hehe. hence i needed that function to move the array index step forward like pHP's NEXT() function.. and that's where my own arrayNext() function comes in for now..

    arrayNext(); // initiate function
    - basically resets the global array indexer back to zero first..


    anyway this is so much simplier than dealing w/ objects and object based handling/declarations etc..

    So once data is loaded from PHP output , the above routine sets the variables and its values into the mc/target timeline.. and the timeline moves 1 frame or more forward where the actual textfields , checkboxes, combo boxes and all, are and as soon as these instances instances come into the time line, the code attached on each of these instances are set to pickup those variables.. no need to assign them 1by1 to each instance..


    Anyway i'm just tryin to show off a little this unique method that i have come to rely on and perhaps it can help others

    i know some would find this crude, but this data handling strategy has made my life soo much easier all these years.. i was able to finish a full blown inventory system (w/c im now updating w/ more features) in a month or 2, from Flash GUI to PHP backend everything..

    I work alone usually, one man coding army.. so im really a self made expert in making things easy and practical for me haha..


    btw the inventory system i mentioned here have now been in service for 4 years, and w/o bias, i've compared it to lots of other custom developed inventory system (VB, c+, etc) here locally.. and it was and still way ahead in features, gui, and efficiency than any other option i've seen from other programmers.. and i did compare my product w/ other options to make sure i had the edge, hehe

    so i guess, still boils down to whatever gets the job done hehe

  11. #11
    Junior Member
    Join Date
    Jun 2011
    Posts
    10
    I don't mean to be a dick but if you were really coding for 10 years you would not have such bad practices as this. There is a much cleaner and easier way to code. Your argument against using LoadVars makes no sense, because you're actually writing MORE code using your method. Plus you have code in movieclips instead of on the timeline which is just going to complicate things more. Inventory system in a month or 2? I've made one in about 2-3 days for my game. Clearly your methods of doing things are only slowing you down.

    Do whatever you like. But your coding practices are horrible and why even bother asking for help if you are just going to do things your way regardless.

  12. #12
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    Tea, if you're not going to accept help, stop asking for it. If you want spend all your time figuring out how to make bad code work with more bad code, it's your right. But stop wasting the time and effort of people on this board.

    You keep posting for help and when you get it, instead of using the help to solve your problem, you spend all your effort trying to explain why you won't improve your code and solve your problem.

    It's not all about "whatever gets the job done." It's all about minimizing time and effort.

    The way you handle data and code in general is a waste of time and effort. If you refuse to improve your code, stop wasting our time and effort.

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