473,396 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with arrays (I think)

132 100+
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:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script language="JavaScript" type="text/javascript">
  4. <!--
  5. var seq = "";
  6.  
  7. function test() {
  8. people[people.length++] = new item("test.dko", "2");
  9. people[people.length++] = new item("test1.dko", "1");
  10. }
  11.  
  12. var people = new Array();
  13. function item(file, nr) {
  14.     this.file = file;
  15.     this.nr = parseInt(parseFloat(nr));
  16. }
  17.  
  18.  
  19. function sortNr(a, b) {
  20.     var x = a.nr;
  21.     var y = b.nr;
  22.     return ((x < y) ? -1 : ((x > y) ? 1 : 0));
  23. }
  24.  
  25. function sortArray() {
  26.     people.sort(sortNr);
  27.  
  28. for (var i=0; i<people.length; i++) {
  29. seq += people[i].file + (i+1)+" \n";
  30. }
  31. document.Demo.Results.value = seq;
  32. }
  33. // -->
  34. </script>
  35. </head>
  36. <body>
  37. <form name="Demo" method="post" action="/" onsubmit="return false;">
  38. <input type="button" value="Sort By Age" onclick="sortArray()" /> &nbsp; 
  39. <br />
  40. <input type="button" value="Stest" onclick="test()" />
  41. <textarea name="Results" onfocus="this.blur();" rows="20" cols="60"></textarea>
  42. </form>
  43. </body>
  44. </html>
  45.  
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
Expand|Select|Wrap|Line Numbers
  1.  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
Expand|Select|Wrap|Line Numbers
  1.  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!
Dec 25 '07 #1
10 1140
Cainnech
132 100+
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.
Dec 26 '07 #2
Cainnech
132 100+
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.
Dec 26 '07 #3
gits
5,390 Expert Mod 4TB
hi ...

you should use:

Expand|Select|Wrap|Line Numbers
  1. people[people.length] = 'newvalue';
  2.  
since length is incremented automatically when adding elements to an array, but you may also use the push-method

kind regards
Dec 26 '07 #4
Cainnech
132 100+
HI Gits,

Thanks for the tip but it still doesn't solve my problem with that function. Any ideas on that ?
Dec 26 '07 #5
gits
5,390 Expert Mod 4TB
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
Dec 26 '07 #6
Cainnech
132 100+
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
Dec 27 '07 #7
gits
5,390 Expert Mod 4TB
it seems like a scope-issue ... could you access the people-array before the call of test? or is it undefined?
Dec 27 '07 #8
Cainnech
132 100+
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
Jan 3 '08 #9
Cainnech
132 100+
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:
Expand|Select|Wrap|Line Numbers
  1. <input type="button" value="TEST" onclick="parent.functionname()" />
  2.  
This way my array is created only once and I'm sure all the values go in the right array.
Jan 7 '08 #10
gits
5,390 Expert Mod 4TB
hi ...

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

kind regards
Jan 7 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Kris | last post by:
Hi All, I am having a problem compiling a C++ project that I am working on and I'm hoping that one of you C++ gurus out there can provide me with some guidance.
3
by: Cyron | last post by:
Hello All, I am confused with some of the results I am getting from my C++ program. I want the program to display "This is a test" but instead "This is a" is displayed. From experimentation...
14
by: placid | last post by:
Hi all, i have this struct for a binary tree node typedef struct treenode { char *word; struct treenode *right; struct treenode *left; }TreeNode;
4
by: Victoria Penrice | last post by:
Dear all, I've written a dll (in C#), to be used as a COM object in VB6. Say (for simplicity) I've defined two classes in that dll: ClassFunc and ClassArgs. One of the functions in ClassFunc...
3
by: YYZ | last post by:
I'm new to VB.net, but not new to OO in general, and I'm very strong in VB6. However, I don't know what I did in VB.net to cause this to happen. I've got 2 projects open at the same time, in the...
7
by: Phil | last post by:
Hi all, 1st post here so................:-) I am using VS.Net and coding in VB. I am following some examples from a book that shows a connection string as:- sqlcnn = new sqlconnection("data...
5
by: Chad | last post by:
I want to create a class which contains a date, integer and string property which all allow a NULL value. I don't want to store the values in an Object datatype. I prefer more strongly typed...
2
by: RS200Phil | last post by:
Hi, I'm tearing my hair out on the simplest of queries (SQL Server 2k). I'm using ASP and the code I was using worked fine when attached to an Access MDB, but I can't crack it under SQL Server....
5
by: Troot | last post by:
Hi All, I've attached some code below. My problem is the following, I have the two variables $name and $pword. I'm passing values for them via xml. I have declared them as global and then try to...
3
by: ZMY | last post by:
I am new to Numpy/Pylab, and I am trying to construct a list of dictionaries with arrays as the items, for example: .... ), 2: ''}, {1: array(), 2: ''}, {1: array(), 2: ''}] ), 2: ''}, {1:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.