PDA

View Full Version : Hopefully simple string manipulation query.


Cringer
08-12-2003, 10:29 AM
I'm using WebSpeed (Progress 4GL) to pass database data through hidden fields into a Javascript function. Some of the data I need to pass through is essentially a 2-dimensional array. The only way I can think of doing this right now is using a comma delimited list of data of the form

system1,env1,system1,env2,system1,env3,system2,env1,system2env2.....

Hope that makes sense! Anyway - I'm not a javascript programmer. I'm learning slowly, but would really value some help in taking these strings and passing them into 2-dimensional Javascript arrays for later processing.

Alternatively if you have any bright ideas for a better way of doing this, please let me know. Thanks a lot in advance.

glenngv
08-12-2003, 10:55 AM
var arr = new Array();

arr[0] = new Array();
arr[0][0] = "env1";
arr[0][1] = "env2";
arr[0][2] = "env3";

arr[1] = new Array();
arr[1][0] = "env1";
arr[1][1] = "env2";
arr[1][2] = "env3";

arr[2] = new Array();
arr[2][0] = "env1";
arr[2][1] = "env2";
arr[2][2] = "env3";

//...and so on


//display content for testing
var str='';
for (var i=0;i<arr.length;i++){
str+="\nitem ["+i+"][0]:"+arr[i][0] + '\n';
str+="item ["+i+"][1]:"+arr[i][1] + '\n';
str+="item ["+i+"][2]:"+arr[i][2] + '\n';
}
alert(str);

The code in blue must be generated by your server-side code

Cringer
08-12-2003, 10:58 AM
Actually that's a good idea. Don't know why I didn't think of that as I already generate HTML that way. So why not generate JavaScript. You're a genius! ;)

Cringer
08-12-2003, 03:46 PM
ok... following on from this. Has anyone got any help for me?

I've got a selection box:

<select name="JSSystem" size="1">
<option Selected>Select System</option>
</select>

and I've got the array:

<script language="javascript">
var SystemArr = new Array();
SystemArr[6] = "sys1";
SystemArr[7] = "sys2";
SystemArr[8] = "sys3";
SystemArr[9] = "sys4";
SystemArr[10] = "sys5";
</script>

(array intentionally doesn't start at 0 so I can dynamically reference things later on)

Is there a way I can use this array to populate the selection list?

Cheers again for your help!

beetle
08-12-2003, 03:55 PM
FYI, you can create two-dimensional arrays with literal notation as well. Below is the same array as in Glenn's examplevar arr = [
['env1','env2','env3'],
['env1','env2','env3'],
['env1','env2','env3']
];
Shouldn't take long to see how it's put together :D

Cringer
08-13-2003, 01:23 PM
Thanks for the help on the earlier issue.

I'm assuming from the lack of responses to the second question that you're all secretly laughing at my stupidity... :p :o :) :)

I've sorted that bit out.... using the server side processing to pass through the dropdown box. It works ok. I hope.. ;)

whammy
08-14-2003, 12:37 AM
That literal notation is actually really cool because it resembles the way I see the array in my head.