...

View Full Version : Need help on array script



ShadowIce
05-16-2009, 02:08 PM
Hi all :) I have been doing some research on arrays and how to create multiple, comma seperated strings.

Now I have a small problem that I cant seem to fix. The problem I have is that I can't get rid of the comma after the last array value has been read. and I can't get the prompt to create more than 1 variable name at once..

if there's a way to retrieve the comma seperated list along with the double quotes using only 1 document.write, please help. i need the variables to look like this:

var variable = new Array("comma","seperated","list");

here is the script i created from various resources:


<script>

function name(num){

var vnum;

for(vnum = 1;vnum<=num;vnum++){

var vname = prompt('please enter the name of variable '+vnum+'\: ','');

}

var a = new Array("a","b","c","d");
var b = new Array("e","f","g");
var c = new Array("g","h","i","j");

if(vname != null || vname != ""){

document.write('var '+vname+' = new Array\(');

}

for(var x=0;x<=a.length-1;x++){

if(x.length != a.length-1){

delimiter = ",";

}else{

delimiter = "";

}

document.write('\"'+a[x]+'\"\,');

}

document.write('\)\;');

}

name(4);

</script>

Thank you! :)

~ShadowIce~

bdl
05-16-2009, 03:47 PM
Most of that makes little, if any, sense. This is not meant to be a criticism, I'm literally telling you it makes no sense. If you can explain why you're trying to create an array like that it would be helpful. I find it odd that you're using document.write to attempt to output an array structure. Seems a bit fishy, maybe the beginnings of some malicious script.



...and I can't get the prompt to create more than 1 variable name at once..


for(vnum = 1;vnum<=num;vnum++){
var vname = prompt('please enter the name of variable '+vnum+'\: ','');
}



The problem there is a no-brainer. You create a new instance of the 'vname' variable each iteration through the loop. So when you're done enteriing N values, the last value is the one that actually is assigned and retained. What you want to do is this:


function enterNameList(listLen) {
var vname= []; // new array named 'vname'
for ( var i=0; i<listLen; i+= 1 ) {
// each name entered is pushed into the array vname
vname.push( prompt("Enter a name") );
}

return vname;
}

var nameList= enterNameList(5);

alert( nameList );

There. Now you have an array structure already in place, and all the values entered are pushed into it.

jmrker
05-16-2009, 04:06 PM
This will display what you want, but I don't think it will do anything.
Just displays contents of 'a', 'b', and 'c' arrays delimited as you desire.


<html>
<head>
<title>Array Display</title>
<script type="text/javascript">
// From: http://www.codingforums.com/showthread.php?t=166473

var vname = new Array();
function Aname(num) {
for(var vnum = 0; vnum<=num; vnum++) {
vname[vnum] = prompt('please enter the name of variable '+(vnum+1)+'\: ','');
}
}
Aname(2);

var a = new Array("a","b","c","d");
var b = new Array("e","f","g");
var c = new Array("g","h","i","j");

var str = '';
for (var i=0; i<vname.length; i++) {
str += 'var '+vname[i]+' = new Array("';
if (i == 0) { str += a.join('","'); }
if (i == 1) { str += b.join('","'); }
if (i == 2) { str += c.join('","'); }
str += '");<br>';
}
document.write(str);

</script>
</head>
<body>
Array Display<p>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum