Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > part of variable name is variable, how?

Reply
Thread Tools

part of variable name is variable, how?

 
 
bing
Guest
Posts: n/a
 
      08-01-2005
The expected output should be:

=====
zero, 0
one, 1
two, 2
=====

This is the code snippet. I know it's not correct. Hence the question
in the subject.

============
$name0 = 0;
$name1 = 1;
$name2 = 2;

%map = (
name0 => 'zero',
name1 => 'one',
name2 => 'two'
);

for ($j=0;$j<=2;$j++)
{
print "$map{name{$j}}, $name{$j}\n";
}
=========

Appreciate any help.

Bing

 
Reply With Quote
 
 
 
 
surfer dude
Guest
Posts: n/a
 
      08-01-2005
While wandering through cyberspace on 1 Aug 2005 13:31:34 -0700, bing
said ...
> The expected output should be:
>
> =====
> zero, 0
> one, 1
> two, 2
> =====
>
> This is the code snippet. I know it's not correct. Hence the question
> in the subject.
>
> ============
> $name0 = 0;
> $name1 = 1;
> $name2 = 2;
>
> %map = (
> name0 => 'zero',
> name1 => 'one',
> name2 => 'two'
> );
>
> for ($j=0;$j<=2;$j++)
> {
> print "$map{name{$j}}, $name{$j}\n";
> }
> =========
>
> Appreciate any help.
>
> Bing
>
>


This sounds like a job for "eval"...
 
Reply With Quote
 
 
 
 
Greg Bacon
Guest
Posts: n/a
 
      08-01-2005
In article <. com>,
bing <> wrote:

: $name0 = 0;
: $name1 = 1;
: $name2 = 2;
:
: %map = (
: name0 => 'zero',
: name1 => 'one',
: name2 => 'two'
: );
:
: for ($j=0;$j<=2;$j++)
: {
: print "$map{name{$j}}, $name{$j}\n";
: }

Change what you're printing, e.g.,

print qq[$map{"name$j"}, name$j\n];

If you're trying to grab the values of $name[0-2], read mjd's series
on why using a variable as a variable name is a bad idea.

http://perl.plover.com/varvarname.html
http://perl.plover.com/varvarname2.html
http://perl.plover.com/varvarname3.html

Consider, for example, grabbing the name from reverse(%map).

Hope this helps,
Greg
--
Isn't it curious how "page" has become a colloquial term for the word
"document" in hypertext, in a medium that does not have pages anymore?
Do we reuse words as soon as they are freed up by technology? Do we reuse
those of whose destruction we can't bear to be reminded? -- Jutta Degener
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
adding a variable name to a hash to name is part of the variable name Bobby Chamness Perl 2 04-22-2007 09:54 PM
Variable displays at one part while does not in another part in a Jack ASP General 8 05-10-2005 07:26 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM
noob question: Trying to extract part of a string in a variable to another variable cayenne Perl Misc 19 05-19-2004 11:22 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57