in reply to Printing Variable Variables

Whilst not the best answer, this works:
my $string0 = "Jessy"; my $string1 = "Bob"; my $string2 = "Nancy"; my $num = 0; while( $num < 3 ) { my $stringnum = "\$string" . $num; my $result = eval $stringnum; print $result . "\n"; $num++; }

This produces the following output:

Jessy Bob Nancy

Replies are listed 'Best First'.
Re^2: Printing Variable Variables
by merlyn (Sage) on Sep 13, 2005 at 13:45 UTC
    my $stringnum = "\$string" . $num; my $result = eval $stringnum;
    (Note: I gave monarch a chance to fix his own article in /msg, but he blew me off in /msg and is now ignoring me, so I have to make this a public comment instead. I think that qualifies as "trying to do the right thing first".)

    Please don't use eval for this without adding a big "don't do this, though". It invokes the compiler needlessly, and it also encourages variable variables, which is a broken form of programming.

    I'd elaborate, but other answers in this thread already have better solutions. I just needed to flag monarch's solution as bad since he was unwilling to do that himself.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.