part of variable name is variable, how?

Discussion in 'Perl Misc' started by bing, Aug 1, 2005.

  1. bing

    bing Guest

    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
    bing, Aug 1, 2005
    #1
    1. Advertising

  2. bing

    surfer dude Guest

    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"...
    surfer dude, Aug 1, 2005
    #2
    1. Advertising

  3. bing

    Greg Bacon Guest

    In article <>,
    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
    Greg Bacon, Aug 1, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bobby Chamness
    Replies:
    2
    Views:
    2,376
    Joe Smith
    Apr 22, 2007
  2. mfglinux
    Replies:
    11
    Views:
    678
    Roberto Bonvallet
    Sep 12, 2007
  3. Jack
    Replies:
    8
    Views:
    261
  4. cayenne
    Replies:
    19
    Views:
    647
    John W. Krahn
    May 20, 2004
  5. David Filmer
    Replies:
    19
    Views:
    221
    Kevin Collins
    May 21, 2004
Loading...

Share This Page