JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 4th, 2006, 10:00 AM
roamer's Avatar
roamer roamer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 285 roamer User rank is Corporal (100 - 500 Reputation Level)roamer User rank is Corporal (100 - 500 Reputation Level)roamer User rank is Corporal (100 - 500 Reputation Level)roamer User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 19 h 59 m 41 sec
Reputation Power: 11
Send a message via ICQ to roamer Send a message via MSN to roamer
How to use string as Array name?

a = new Array('a1','a2','a3');

function myfun1(haha){
tmp2 = haha;

for(i=0;i<tmp2.length;i++){
document.form1.mysel2.options[i]= new Option(tmp2[i],tmp2[i]);

}
}

...............
myfun1('a');
-------------------------------------------
When I alert(tmp2), a is displayed. But I found that tmp2[i] won't evaluate as a[i]. Because it just treat tmp2 as an array.
-------------------------------------------

Can anyone help? Thx.

Reply With Quote
  #2  
Old April 4th, 2006, 01:12 PM
jsKid's Avatar
jsKid jsKid is offline
Application is what Divides Us
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2002
Location: Titusville, FL
Posts: 2,179 jsKid User rank is Sergeant Major (2000 - 5000 Reputation Level)jsKid User rank is Sergeant Major (2000 - 5000 Reputation Level)jsKid User rank is Sergeant Major (2000 - 5000 Reputation Level)jsKid User rank is Sergeant Major (2000 - 5000 Reputation Level)jsKid User rank is Sergeant Major (2000 - 5000 Reputation Level)jsKid User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 15 h 42 m 16 sec
Reputation Power: 57
Send a message via AIM to jsKid Send a message via MSN to jsKid
MySpace
At first glance I would say that the problem is that you are
passing the array name as a string, instead of the object itself.
Code:
myfun1('a') --> myfun1(a)


But then it comes down to, you want to explicity access
the array via it's string name? Could you explain why you'd
want to do it this way?
__________________
Download [ Fx | Op ] Validate [ Markup | Css ]

Last edited by jsKid : April 4th, 2006 at 01:14 PM.

Reply With Quote
  #3  
Old April 4th, 2006, 03:22 PM
derelict's Avatar
derelict derelict is offline
garish grotesque gargoyle
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Mar 2006
Location: gracing gargantuan gothic gateways
Posts: 1,337 derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 6 Days 8 h 35 m 8 sec
Reputation Power: 1036
you can get tricky...

I agree with jsKid, there doesn't seem to be any need to reference the array by a string containing its name. if you wanted to do this, it would be better to create a container with a string reference for the array, then use the string reference elsewhere... there's a couple ways to do this:

Code:
<html>
<head>
<title>JS wierdness</title>
<script type="text/javascript">
	var arrCont = new Array();
	arrCont['foo'] = new Array();
	arrCont['foo'][0] = 'arrrr foooooooohh onnnneee';
	arrCont['foo'][1] = 'arrrr foooooooohh twwwoooo';
	arrCont['bar'] = new Array();
	arrCont['bar'][0] = 'arrrr baaaaahhhrr onnnneee';
	arrCont['bar'][1] = 'arrrr baaaaahhhrr twwwoooo';
	var objCont = new Object();
	objCont['foo'] = new Array();
	objCont['foo'][0] = 'obbbby foooooooohh onnnneee';
	objCont['foo'][1] = 'obbbby foooooooohh twwwoooo';
	objCont['bar'] = new Array();
	objCont['bar'][0] = 'obbbby baaaaahhhrr onnnneee';
	objCont['bar'][1] = 'obbbby baaaaahhhrr twwwoooo';

	warr = 'foo';
</script>
</head>
<body>
given two arrays, 'foo' and 'bar', access them via string names by reference in either an array or object container:
<a id='linker' href="javascript:warr=='foo'?warr='bar':warr='foo';void(document.getElementById('linkor').firstChild.data=  'currently '+warr+', click to change');"><span id="linkor">currently foo, click to change</span></a><br><br>
<a href="javascript:alert(arrCont[warr][0])">alert from array container, part one</a><br>
<a href="javascript:alert(arrCont[warr][1])">alert from array container, part two</a><br><br>
<a href="javascript:alert(objCont[warr][0])">alert from object container, part one</a><br>
<a href="javascript:alert(objCont[warr][1])">alert from object container, part two</a><br>
</body>
</html>


but in both cases you're really using a string key in a container object (I think this works because arrays are objects and that object notation is allowed in JS). If you *must* access arrays by string reference, just build them as members of a collection (even if there's only one member) so that you can use this notation to find them.

Is this what you needed?

HTH - derelict

Reply With Quote
  #4  
Old April 5th, 2006, 12:19 PM
roamer's Avatar
roamer roamer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 285 roamer User rank is Corporal (100 - 500 Reputation Level)roamer User rank is Corporal (100 - 500 Reputation Level)roamer User rank is Corporal (100 - 500 Reputation Level)roamer User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 19 h 59 m 41 sec
Reputation Power: 11
Send a message via ICQ to roamer Send a message via MSN to roamer
Yes, thank you, this is what I want. Though I am not fully know all the syntax (90% acheived), I think i can finish the rest of my code.

Actually, I would like to create 3 arrays, and by passing one argument to a function, it will extract the suitable array and return its values.

------------------
a = new Array('a1','a2','a3');
b = new Array('b1','b2','b3');
c = new Array('c1','c2','c3');

function myfun1(haha){
tmp2 = haha;

for(i=0;i<tmp2.length;i++){
document.form1.mysel2.options[i]= new Option(tmp2[i],tmp2[i]);

}
}
myfun1('b');

Reply With Quote
  #5  
Old April 5th, 2006, 12:54 PM
derelict's Avatar
derelict derelict is offline
garish grotesque gargoyle
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Mar 2006
Location: gracing gargantuan gothic gateways
Posts: 1,337 derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 6 Days 8 h 35 m 8 sec
Reputation Power: 1036
then this should work:

Javascript Code:
Original - Javascript Code
  1. var arrCont = new Object;
  2. // create an object to contain your arrays by reference
  3. arrCont['a'] = new Array('a1','a2','a3');
  4. arrCont['b'] = new Array('b1','b2','b3');
  5. arrCont['c'] = new Array('c1','c2','c3');
  6. // notice use of quotes around identifiers 'a','b', and 'c'
  7.  
  8. function myfun1(haha){
  9. // input string 'b' comes in as var haha
  10.  
  11. tmp2 = arrCont[haha];
  12. // notice *NO* quotes to deref input string from var context
  13. // and that it's not a direct assign... we're grabbing from the array container arrCont
  14.  
  15. for(i=0;i<tmp2.length;i++){
  16. document.form1.mysel2.options[i]= new Option(tmp2[i],tmp2[i]);
  17.  
  18. }
  19. }
  20. myfun1('b');
  21. // notice quotes becuase we're passing the string identifier, not a variable
  22.  


this should make your script work the desired way. If you have more questions about syntax or principles, please just ask

HTH - derelict
Comments on this post
roamer agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > How to use string as Array name?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap