Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Associative array + eval() problem

Status
Not open for further replies.

SofaKingGreat

Technical User
Joined
Oct 11, 2007
Messages
3
Location
US
I need some help.....

I have an associative array (tenmap).

I have a form with numbered hidden fields (num_1,num_2,num_3 etc..)

I'm trying to print the values from "tenmap" based on the values num_X

the while loop checks that ten_x exists, if it does I try to print the values.

I'm trying to assign the num_X value to var tmp.

I also created tmp1 and I assign to it the same value that num_1 has (000_00).

Then I alert(tmp) and alert(tmp1), they both seems to be "000_00".

When I try to alert(tenmap[tmp]), I get "undefined", but alert(tenmap[tmp1]) prints 1 (the value from the associative array).

What am I doing wrong :confused:

Code:
function sssmap()
{
	var x = 1;
	var tenmap = new Array();
	tenmap["000_00"] = 1;
	while (eval("typeof document.pb.ten_" + x + " != 'undefined'"))
	{
			eval("var tmp = document.pb.ten_" + x + ".value");
			var tmp1 = "000_00";
			alert(tmp);
			alert(tmp1);
			alert(tenmap[tmp]);
			alert(tenmap[tmp1]);
		}
		x++;
	}
}

TennisLessons.com - The place to go to find a pro.
MathLessons.com - When the numbers don't add up.
 
Just out of curiosity, why are you using eval? It looks like it's completely unnecessary for what you're doing.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
You have an incorrect closing brace, among other things. Also, eval is ev[red]i[/red]l. Try this:
Code:
var tmp1 = "000_00";
while (document.pb.elements['ten_' + x) + '' != 'undefined')
  {
  var tmp = document.pb.elements['ten_' + x).value;
  alert(tmp);
  alert(tmp1);
  alert(tenmap[tmp]);
  alert(tenmap[tmp1]);
  x++;
  }

Lee
 
If Lee's solution still doesn't work for you then I would guess that your input data doesn't hold what you think it does. This small test shows that what you're attempting is possible:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

window.onload = function () {
   var a = [];
   a["000_00"] = 1;
   var b = "000_00";
   var c = document.forms["frm"].elements["txt"].value;
   alert("b: " + b + "\nc: " + c + "\na[b]: " + a[b] + "\na[c]: " + a[c]);
};

</script>

<style type="text/css">
</style>

</head>
<body>

<form id="frm">
   <input type="text" name="txt" value="000_00" />
</form>

</body>
</html>

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
OK, eval = evil = it's gone.

I changed it to

Code:
var tmp = document.pb.elements['ten_' + x].value;

But when I try to alert(tenmap[tmp]) I still get undefined....

I also tried getting the value directly (var tmp = document.pb.ten_1.value;), but that also return undefined.

When I alert tmp and tmp1, they both seem to have the correct value (000_00).

TennisLessons.com - The place to go to find a pro.
MathLessons.com - When the numbers don't add up.
 
It seems I forgot to take my "Anti Donkey" medication today....

I had a space between the _ and the 00.

Thanks for the help, and for informing me about the evil ways of eval().

Cheers.

TennisLessons.com - The place to go to find a pro.
MathLessons.com - When the numbers don't add up.
 
When I alert tmp and tmp1, they both seem to have the correct value (000_00).

Are you sure?

Try sticking characters around the data, like so:

Code:
alert("*" + tmp + "*");
alert("*" + tmp1 + "*");

That way you'll see if there are any spaces in the string that you're not seeing, which falls back on what I said in my last post:
If Lee's solution still doesn't work for you then I would guess that [!]your input data doesn't hold what you think it does.[/!]

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top