Problem with arrays (I think)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cainnech
    New Member
    • Nov 2007
    • 132

    Problem with arrays (I think)

    Hi guys,

    I'm a bit puzzled by a script I'm trying to write. I'm doing a script which wil automatically generates a series of commands which can then be executed by a third party application. Very simply explained, I'm generating a series of commands and I export that to a txt-like file.

    I've done this before without any problems but now I've got something strange going on. I implemented a sort function which seems to work fine when I use it stand-alone. But when I copy the code into my big script, it doesn't work anymore. I checked the script for duplicate names but there aren't any. The only thing what I'm doing different then in the stand-alone script, is that I create new items in an array in different functions. However in the stand-alone script that is also in a function and there it works perfectly.

    Could it be because I'm not supposed to split arrays in pieces? Although when I look at the code, I don't think I'm doing anything wrong with it.

    This is the stand-alone version which works fine:
    Code:
    <html>
    <head>
    <script language="JavaScript" type="text/javascript">
    <!--
    var seq = "";
    
    function test() {
    people[people.length++] = new item("test.dko", "2");
    people[people.length++] = new item("test1.dko", "1");
    }
    
    var people = new Array();
    function item(file, nr) {
    	this.file = file;
    	this.nr = parseInt(parseFloat(nr));
    }
    
    
    function sortNr(a, b) {
    	var x = a.nr;
    	var y = b.nr;
    	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    }
    
    function sortArray() {
    	people.sort(sortNr);
    
    for (var i=0; i<people.length; i++) {
    seq += people[i].file + (i+1)+" \n";
    }
    document.Demo.Results.value = seq;
    }
    // -->
    </script>
    </head>
    <body>
    <form name="Demo" method="post" action="/" onsubmit="return false;">
    <input type="button" value="Sort By Age" onclick="sortArray()" /> &nbsp; 
    <br />
    <input type="button" value="Stest" onclick="test()" />
    <textarea name="Results" onfocus="this.blur();" rows="20" cols="60"></textarea>
    </form>
    </body>
    </html>
    A quick explain to what the function is doing: It puts two values in a textarea (test.dko & test1.dko) and then sorts on the value from low to high. So in the list test1.dko should be first and test.dko should be second.

    The code for my big script cannot be pasted here since it's over 1200 lines big. But I can send it to you upon request and e-mailaddress.
    The only thing I changed there is that I'm putting
    Code:
     people[people.length++] = new item("file.dko", "2");
    in a few different functions but all with different values. So I'm actually trying to add new items to the array depending on which functions are being called.

    When I put the
    Code:
     people[people.length++] = new item("file.dko", "2");
    at the top where I define the array then it works without a problem but that's not the effect I'm going for.

    Does anyone have a clue why this won't work ?
    Thanks for the help!
  • Cainnech
    New Member
    • Nov 2007
    • 132

    #2
    UPDATE

    Hi guys,

    After a few hours of further testing and gazing at code I can only conclude that this is something very strange...

    Adding stuff to the array has now been moved inside a function.

    I've put a test button in place which calls for this function. And this way it seems to work. However, when I call this function from inside another function, it doesn't work. Although it's the same function.

    So I thought, maybe there's an error in the main-function from where I'm calling this other function. So I created a second function which just shows a popup just for debugging reasons. So when I tested it, I get the popup but the data isn't passed to the array. So I'm sure he's executing it but it diesn't work.

    So my question is: Why does it work when using a button to call the function but not when I just call the function from inside another function ?

    Any genius know the answer to that one?

    Off to bed for me. Thanks guys.

    Comment

    • Cainnech
      New Member
      • Nov 2007
      • 132

      #3
      UPDATE 2

      I did just one last quick test.

      Since my test2 (the one with the popup) was working, I copied the contents of test1 into test2. I let the function run and I get the popup but no data is being passed to the array.

      For the first time in my life, Javascript has outsmarted me ...

      Now really off to bed.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        hi ...

        you should use:

        [CODE=javascript]people[people.length] = 'newvalue';
        [/CODE]
        since length is incremented automatically when adding elements to an array, but you may also use the push-method

        kind regards

        Comment

        • Cainnech
          New Member
          • Nov 2007
          • 132

          #5
          HI Gits,

          Thanks for the tip but it still doesn't solve my problem with that function. Any ideas on that ?

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            hi ...

            i'm not quite sure that i got your real problem ... could you explain it again in other words please? i tested the code you posted that seems to work ... or isn't it working as you intend? the error i mentioned could have made problems in case you iterate through the array later on ... because of undefined array-elements ...

            kind regards

            Comment

            • Cainnech
              New Member
              • Nov 2007
              • 132

              #7
              Hi Gits,

              The code I pasted above also works with me. But when I try to include it in another (much larger) script then it fails on me.

              It's the same script but there's only one small difference. In the script above I call the function "test" upon a click of the button.
              In my other script I'm calling the function "test" from within another function; so test();

              And that way he won't execute the script.

              But for testing purposes, I added a button to my page which calls for the function "test". And when I push the button, then it does work.

              So when I call a function using a button, it works and when I call it from within a function it doesn't work.

              Greets

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                it seems like a scope-issue ... could you access the people-array before the call of test? or is it undefined?

                Comment

                • Cainnech
                  New Member
                  • Nov 2007
                  • 132

                  #9
                  Hi Gits,

                  Sorry for my late response but I've been out of time.
                  First of all best wishes for 2008!

                  I've been looking into the code but I'm afraid I can't see the the scope-issue if there's one. Probably my lack of knowledge of the scope.

                  Would you mind if I sent you the code so you could have a look ?

                  Thanks,

                  Cainnech

                  Comment

                  • Cainnech
                    New Member
                    • Nov 2007
                    • 132

                    #10
                    Thanks to the infinite wisdom of Gits my problem has been solved.

                    In fact my script was in an external .js. And my page was constructed with an Iframe. In both pages I referenced the external .js.

                    So the same array was created on two different pages, my parent-window and my iframe.

                    All my values that I wanted to add to the array were added inside the Iframe except when I had to generate the output which was generated from the parent-window.

                    So when I tried to generate the output, I didn't get anything because my array on the Parent-window was empty. They were all in the array in the Iframe.

                    So I removed all the scripts from the Iframe and only kept the script in the parent. And if I had to call a function from inside the Iframe I called it from the parent-window by using:
                    Code:
                    <input type="button" value="TEST" onclick="parent.functionname()" />
                    This way my array is created only once and I'm sure all the values go in the right array.

                    Comment

                    • gits
                      Recognized Expert Moderator Expert
                      • May 2007
                      • 5390

                      #11
                      hi ...

                      thanks for sharing the solution :) ... post back to the forum anytime you have more questions ...

                      kind regards

                      Comment

                      Working...