Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Dynamically replace Array content.

    Hello All,

    I have a code which works fine for what is does but I want to add in dynamically a new list to the Array...... from an input field.

    The relevent part of the code reads as this for example........

    var step_x_list=new Array("ca1sb2en1pg1itm1","ca1sb2en1pg1itm2","ca1sb2en1pg1itm3")

    but I need a way to dynamically replace the existing.... "ca1sb2en1pg1itm1","ca1sb2en1pg1itm2","ca1sb2en1pg1itm3" seen above with a new list from an input field which holds the following say....... ca2sb2en2pg1itm1","ca2sb2en2pg1itm2","ca2sb2en2pg1itm3

    I have tried various ideas but none seem to be able to inject the new values in the space between the brackets.

    Any thoughts.

    Martin.

    For those just wondering what the code is and want to try it, it is a one piece code which can fire many different functions without requiring lots of individual bits of code to do same. The action usually happens in micro-seconds but I slowed it to a one second count between changes so you can see what is actually happening.

    Below is a revised code if you want to see it working and also to give an idea about the Array part in which I want to add a different number set.
    Copy the code and save in notepad as test.html
    It can be opened as a web page. Remember to change the code tags to the correct script tags


    Code:
    var i_x_list=0
    function fireFunctionList_x_list()
    {
    if (document.getElementById("locate_load_x_list").value == "end" || document.getElementById("locate_load_x_list").value == "undefined"){i_x_list=0;document.getElementById("locate_load_x_list").value=""}else{fireFunctionList_x_list_do()}
    }
    function fireFunctionList_x_list_do()
    {
    document.getElementById("addto").value="place_";
    var step_x_list=new Array("ca1sb2en1pg1itm1","ca1sb2en1pg1itm2","ca1sb2en1pg1itm3","ca1sb2en1pg1itm4","ca1sb2en1pg1itm5","ca1sb2en1pg1itm6","ca1sb2en1pg1itm7",
    "ca1sb2en1pg1itm8","ca1sb2en1pg1itm9","ca1sb2en1pg1itm10","ca1sb2en1pg1itm11","ca1sb2en1pg1itm12","ca1sb2en1pg1itm13","ca1sb2en1pg1itm14","end")
    document.getElementById('locate_load_x_list').value=step_x_list[i_x_list]
    i_x_list=i_x_list+1
    document.getElementById("addto").value+=document.getElementById("locate_load_x_list").value;
    document.getElementById("final_locate_load_x_list").value=document.getElementById("addto").value;
    document.getElementById("final_locate_load_x_list").value+=document.getElementById("addto2").value;
    var x_list = document.getElementById("final_locate_load_x_list").value;
    setTimeout("fireFunctionList_x_list()",1000)
    var f_x_list = new Function(x_list); f_x_list();
    }
    <BR><BR>
    <button onclick="fireFunctionList_x_list()">run the function list rotation</button>
    <BR><BR>
    <input id="addto" type="text" size=25 value="place_"><BR>
    <input id="addto2" type="text" size=4 value="()"><BR>
    <input id="locate_load_x_list" type="text" size=20 value=""><BR>
    the code fires this function if there<input id="final_locate_load_x_list" type="text" size=25 value=""><BR>
    Code:
     function place_ca1sb2en1pg1itm4(){alert("ca1 number4 function dummy run as alert");}
    Code:
     function place_ca1sb2en1pg1itm9(){alert("ca1 number9 function dummy run as alert");}
    Code:
     function place_ca2sb2en1pg1itm6(){alert("ca2 number6 function dummy run as alert");}
    Code:
     function place_ca2sb2en1pg1itm12(){alert("ca2 number12 function dummy run as alert");}

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,307
    Thanks
    23
    Thanked 612 Times in 611 Posts
    PHP Code:
    <?php
    $old_stuff 
    = array('first',  'second''third');
    print_r($old_stuff);
    echo 
    '<br />-----------------<br />';
    $love 1;
    $me 'a_new_word';
    $please 'ce3';

    $old_stuff = array(=> $love=> $me=> $please);
    print_r($old_stuff);
    ?>

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hello sunfighter,

    Thank you for your reply, but could you convert that to javascript if possible.

    I have a feeling it may have the same problem I have encountered to date which is that the replacement words or numbers come from an input field as a set like.... 123,456,789 and so on and would be dropped into just one of your string entries say the $love= as the number string cannot be devided up, the result won't insert into the array. Or at least I have failed to make it do so, so far.

    Martin.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    I have tried various ideas but none seem to be able to inject the new values in the space between the brackets.
    ??? What brackets? What "space"?

    But if you are saying that you have an <input> field which contains *EXACTLY*
    Code:
    "ca2sb2en2pg1itm1","ca2sb2en2pg1itm2","ca2sb2en2pg1itm3"
    and you want to put that into an array, it's trivial:

    Code:
    arrayName = form.fieldName.value.split(",");
    If you mean you only want to replace the first three items in the array...leaving the other items intact:
    Code:
    var temp = form.fieldName.value.split(",");
    for ( var i = 0; i < temp.length; ++i )
    {
        arrayName[i] = temp[i];
    }
    or you could do
    Code:
    var temp = form.fieldName.value.split(",");
    arrayName = temp.concat( arrayName.slice(temp.length) );
    Anyway, I suspect the magic bullet you are looking for is the split method.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    Okay, I actually managed to make your code run (after fixing your omission of the "final_locate_load_x_list" element).

    But I don't understand what relevance that code has to your question about replacing all or part of the step_x_list array.

    The code also seems incredibly convoluted for doing something that should be simple, but I guess that's another subject.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New Coder
    Join Date
    Mar 2012
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hello Old Pedant,

    [.....The code also seems incredibly convoluted for doing something that should be simple, but I guess that's another subject.....]
    Yes, it is with all those input fields, but it is an experiment and the fields allow me to see the assembly of the wanted code and various actions. The code I have shown is part of a much larger one. However, please feel free to post a complete working example of a simplifed code which can do same as I am always open to suggestions for ways to code things which can be shown by example to actually work.


    [.....But I don't understand what relevance that code has to your question about replacing all or part of the step_x_list array......]
    I am sorry but I am not good at explaining myself.

    In the bigger picture, there are many functions which could be fired within the main code, I showed only four for example and two of those were fired when the code assembled the function with the correct final number function like the.... place_ca1sb2en1pg1itm9()

    In actual fact the codes array would be empty at first and is populated by numbers from an external input field 'new_selections' which has different numbers added from other sources. It is that group of numbers from the input field I am trying to insert into the step_x_list array. The code I posted previously showed a list of numbers in the array already so the code could work and show by example it can construct the final number and execute the required function.

    On the execution of a main function..... loadthe_FunctionList() the external input field 'new_selections' holding a quantity of numbers needs to populate the step_x_list array so the rest of the array code can loop using the...... fireFunctionList_x_list() to assemble each final number from the array list.

    When the....loadthe_FunctionList() is fired again at a later time, there could be a completely different set of numbers in the input field 'new_selections' for the array to run through, so that new set of numbers needs to replace the previous before the...fireFunctionList_x_list() is started anew once again.

    The code part for end or undefined is there to reset the assembly fields when needed and to stop array code forever looping in a runaway fashion.

    Maybe the code below can be better understood. If what I am asking is possible, can you insert the change into the code as example.

    If you wish for proof the code functions with the input field numbers, simply copy them from the shown field or from here... "ca5sb2en1pg1itm25","ca1sb2en1pg1itm2","ca5sb2en1pg1itm99","ca1sb2en1pg1itm4","ca3sb2en1pg1itm8","ca 1sb2en1pg1itm12","ca1sb2en1pg1itm13","ca1sb2en1pg1itm14","end"
    and insert to replace the step_x_list array wording ....THIS NEEDS TO BE POPULATED AT MAIN FUNCTION START FROM THE EXTERNAL INPUT FIELD new_selections BEFORE ASSEMBLY LOOP STARTS....


    ##### code starts below here #####

    <input id="new_selections" type="text" size=100 value='"ca5sb2en1pg1itm25","ca1sb2en1pg1itm2","ca5sb2en1pg1itm99","ca1sb2en1pg1itm4","ca3sb2en1pg1it m8","ca1sb2en1pg1itm12","ca1sb2en1pg1itm13","ca1sb2en1pg1itm14","end"'>
    <BR>
    Code:
    function loadthe_FunctionList()
    {
    
    // THIS POSITION IS WHERE THE new_selections INPUT FIELD
    // NEEDS TO POPULATE THE var step_x_list=new Array() BELOW
    
    fireFunctionList_x_list()
    }
    var i_x_list=0
    function fireFunctionList_x_list()
    {
    if (document.getElementById("locate_load_x_list").value == "end" || document.getElementById("locate_load_x_list").value == "undefined"){i_x_list=0;document.getElementById("locate_load_x_list").value=""}else{fireFunctionList_x_list_do()}
    }
    function fireFunctionList_x_list_do()
    {
    document.getElementById("addto").value="place_";
    
    var step_x_list=new Array(....THIS NEEDS TO BE POPULATED AT MAIN FUNCTION START FROM THE EXTERNAL INPUT FIELD new_selections BEFORE ASSEMBLY LOOP STARTS....)
    
    document.getElementById('locate_load_x_list').value=step_x_list[i_x_list]
    i_x_list=i_x_list+1
    document.getElementById("addto").value+=document.getElementById("locate_load_x_list").value;
    document.getElementById("final_locate_load_x_list").value=document.getElementById("addto").value;
    document.getElementById("final_locate_load_x_list").value+=document.getElementById("addto2").value;
    var x_list = document.getElementById("final_locate_load_x_list").value;
    setTimeout("fireFunctionList_x_list()",1000)
    var f_x_list = new Function(x_list); f_x_list();
    }
    <BR><BR>
    <button onclick="loadthe_FunctionList()">load and run the function list rotation</button>
    <BR><BR>
    <input id="addto" type="text" size=25 value="place_"><BR>
    <input id="addto2" type="text" size=4 value="()"><BR>
    <input id="locate_load_x_list" type="text" size=20 value=""><BR>

    <input id="final_locate_load_x_list" type="text" size=25 value=""><BR>

    Code:
     function place_ca1sb2en1pg1itm4(){alert("ca1 number4 function dummy run as alert");}
    Code:
     function place_ca1sb2en1pg1itm9(){alert("ca1 number9 function dummy run as alert");}
    Code:
     function place_ca2sb2en1pg1itm6(){alert("ca2 number6 function dummy run as alert");}
    Code:
     function place_ca2sb2en1pg1itm12(){alert("ca2 number12 function dummy run as alert");}
    Code:
     function place_ca3sb2en1pg1itm8(){alert("ca3 group number8 function dummy run as alert");}
    Code:
     function place_ca3sb2en1pg1itm15(){alert("ca3 number15 function dummy run as alert");}
    Code:
     function place_ca3sb2en1pg1itm45(){alert("ca3 number45 function dummy run as alert");}
    Code:
     function place_ca3sb2en1pg1itm189(){alert("ca3 number189 function dummy run as alert");}
    Code:
     function place_ca5sb2en1pg1itm44(){alert("ca5 number44 function dummy run as alert");}
    Code:
     function place_ca5sb2en1pg1itm99(){alert("ca5 number99 function dummy run as alert");}
    Code:
     function place_ca5sb2en1pg1itm61(){alert("ca5 number61 function dummy run as alert");}
    Code:
     function place_ca5sb2en1pg1itm22(){alert("ca5 number22 function dummy run as alert");}
    Last edited by SpidersWebHelp; 03-18-2012 at 03:33 PM.

  • #7
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,307
    Thanks
    23
    Thanked 612 Times in 611 Posts
    Martin,
    I'm sorry about the php thing. I seems I do that a lot. Guess that's how my brain is wired.
    Code:
    <script type="text/javascript">
    
    var old_array = new Array(
    "ca5sb2en1pg1itm25",
    "ca1sb2en1pg1itm2",
    "ca5sb2en1pg1itm99",
    "ca1sb2en1pg1itm4",
    "ca3sb2en1pg1itm8",
    "ca 1sb2en1pg1itm12",
    "ca1sb2en1pg1itm13",
    "ca1sb2en1pg1itm14",
    "end" );
    
    document.write(old_array[4]+'<br />--------------------<br />');
    
    old_array[2] = 'Love is but a movie ploy';
    var x = 0;
    document.write(old_array[0]+'<br />');
    document.write(old_array[1]+'<br />');
    document.write(old_array[2]+'<br />');
    document.write(old_array[3]+'<br />');
    document.write(old_array[4]+'<br />');
    document.write(old_array[5]+'<br />');
    document.write(old_array[6]+'<br />');
    document.write(old_array[7]+'<br />');
    document.write(old_array[8]+'<br />');
    </script>
    Not much difference.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    Okay, I understand what you want to do. And I really did understand that you were only showing a sampling of the functions that would be called.

    But you keep saying "numbers". For example:
    In actual fact the codes array would be empty at first and is populated by numbers from an external input field 'new_selections' which has different numbers added from other sources.
    But the strings you are using are *NOT* numbers. Example: ca2sb2en1pg1itm6. That has a lot more than just "numbers" in it.

    So does the code need to take a set of numbers, such as "2,2,1,1,6" and convert that to "ca2sb2en1pg1itm6"?? Or is it just a single string of numbers? "22116"? Or what?

    And can you *guarantee* that every string so constructed *WILL* have a previously created function of the appropriate name? That is, if I (somehow, from something) created the string "ca9sb1en0pg1itm9" would you guarantee that the function place_ca9sb1en0pg1itm9( ) already exists? Because even if you limit each "piece" of that name to one digit, that is still 9 to the 5th power (59049) different possible functions. (Or 10 to the 5th...100,000...if you allow zeroes.)

    I think I am really really missing something in all this.

    But putting elements into an array is the easy part, in any case. You just need to show us *EXACTLY* what the contents of the new_selections field will look like.
    Last edited by Old Pedant; 03-18-2012 at 08:30 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New Coder
    Join Date
    Mar 2012
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hello Old Pedant,

    Okay I see how I am confusing you with the word numbers, I was just using it as a generalisation to mean the string of numbers and characters that would appear in the input field and then be transferred into the Array field. So no there is no conversion to be done, the 'number' (string of numbers and characters combined) to be used are the actual "ca2sb2en1pg1itm6" or "ca2sb2en1pg1itm80" that would be in the input field as shown in post.


    [.......And can you *guarantee* that every string so constructed *WILL* have a previously created function of the appropriate name? That is, if I (somehow, from something) created the string "ca9sb1en0pg1itm9" would you guarantee that the function place_ca9sb1en0pg1itm9() already exists?.....]

    That would be a yes. If the string "ca9sb1en0pg1itm9" along with a fair few others were in the input field the code would generate a final executable function place_ca9sb1en0pg1itm9() as you assumed and then fire it.

    (Just for the record, not that it would be possible in reality. If you wish to add in one or more completely different string of letters and numbers even a name like "martin" to the input field where there is NO actual function in existence to be fired, the code makes up the fictitious place_whatevernumber() and runs it, although it won't do anything, then moves onto the next string in array sequence without causing any interruption or failing of the code.)

    [......But putting elements into an array is the easy part, in any case. You just need to show us *EXACTLY* what the contents of the new_selections field will look like.....]

    The new_selections input field is shown in my post at top of code, but the input field looks like this (below) when it is written into from other locations. I have made it just three elements below as three or hundreds are possible.

    The contenst are written to the field value as shown....... "ca5sb2en1pg1itm37","ca7sb2en1pg1itm2","ca5sb2en1pg1itm19","end"

    <input id="new_selections" type="text" size=100 value='"ca5sb2en1pg1itm37","ca7sb2en1pg1itm2","ca5sb2en1pg1itm19","end"'>

    Is this what you are asking for?

    Martin.


    Martin.
    Last edited by SpidersWebHelp; 03-18-2012 at 09:10 PM. Reason: crossed posts

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    Yep.

    So, as I said, it's easy:

    Code:
    step_list_x = document.getElementById("new_selection").value.split(",");
    You are done.

    If you need to *APPEND* the contents of new_selection to step_list_x then:

    Code:
    step_list_x = step_list_x.concat( document.getElementById("new_selection").value.split(",") );
    If you need to replace elements of step_list_x, then just tell us WHICH elements. The first N where N is the number in new_selection or the last N or ???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    SpidersWebHelp (03-19-2012)

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    But if you truly can guarantee that the functions will all exist at the time they are added to the step_list_x array, then I would put function references into the array instead of strings.

    Code:
    var temp = document.getElementById("new_selection").value.split(",");
    step_list_x = [ ];
    for ( var t = 0; t < temp.length; ++t )
    {
        var f = new Function("place_" + temp[t] + "()");
        step_list_x[t] = f;
    }
    And then your code is dirt simple:
    Code:
    var i_x_list = 0;
    function fireFunctionList_x_list()
    {
         if ( i_x_list + 1 >= step_list_x.length ) return;
         var f = step_list_x[ i_x_list++ ];
         if ( f == null ) return;
         setTimeout( fireFunctionList_x_list, 1000 ); // don't use the old-fashioned quote version
         f( );
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    Another possibility:

    Stick with strings in the array:
    Code:
    step_list_x = document.getElementById("new_selection").value.split(",");
    And then, similar to what you were doing before:
    Code:
    var i_x_list = 0;
    function fireFunctionList_x_list()
    {
         if ( i_x_list + 1 >= step_list_x.length ) return;
         try {
             var f = new Function( "place_" + step_list_x[ i_x_list++ ] + "()" );
             if ( f == null ) return;  // probably not needed
             setTimeout( fireFunctionList_x_list, 1000 ); // don't use the old-fashioned quote version
             f( );
         } catch ( ignored ) { 
             /* do nothing if function doesn't exist */
         }
    }
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    New Coder
    Join Date
    Mar 2012
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you Old Pedant.

    I can work with those methods.
    Have you ever heard the expression "You cannot see the wood for the trees".

    I have to say I seem to be becoming dumber by the day, I forget many simple things more often. The things seem to be there in my mind but I just cannot grab or arrange them correctly and use. Getting too old I think, brain cell burnout type of thing.

    I had previously use various ideas including the split, but not in the way you coded. However simple the code turned out to be I simply failed to see it. Having it written in front of me it became obvious.

    The overall code works fine now with the change from....
    step_list_x = new Array(.....)
    to...
    step_x_list = document.getElementById("new_selection").value.split(",");
    as a simple one line replacement (with a bit of position moving for the little x to match the existing code and the removal of the quotes around each string in the input field when the input field is written, as it splits at the comma).

    Thanks once again for your help.

    Martin.

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    Oh, I've seen a *LOT* of trees in my lifetime! <grin style="sheepish" />
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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