rhxk has asked for the wisdom of the Perl Monks concerning the following question:

say I have

$a1 = 0; $a2 = 1; $a3 = 4; $b1 = 8; $b2 = 2; $b3 = 9; $c1 = 1; $c2 = 4; $c3 = 7;

and I want to print the values of a1,a2,...,b1,b2,...,c1,c2,..., and so on in this manner:

for ($i=1; $i<=3; $i++) { print ${"a$i"}.${"c$i"}.${"c$i"}; }

is there something wrong with this?
I know I know this, but somehow a moth has gotten in my head. :(

thanx...

Code tags added by GrandFather

Replies are listed 'Best First'.
Re: for loop help
by jwkrahn (Abbot) on May 15, 2006 at 20:33 UTC
    It looks like you need a Hash of Arrays, something like:
    my %hash = ( a => [ 0, 1, 4 ], b => [ 8, 2, 9 ], c => [ 1, 4, 7 ], ); for my $i ( 0 .. 2 ) { print "$hash{a}[$i]$hash{b}[$i]$hash{c}[$i]"; }

Re: for loop help
by moklevat (Priest) on May 15, 2006 at 20:34 UTC
    Well, you could certainly do what you want to do using eval, but fundamentally it looks like you have the wrong data structure for the job.

    Rather than having a bunch of scalars you might consider using an array, or a hash, or most likely a hash of arrays. Since I'm not certain of the relative significance of the scalar letters (a,b,c) and the apparent index numbers (1,2,3) it is difficult to make a specific recommendation.

    I'm sure a fast-coder will follow-up with some magical sample code shortly.

      don't do that ;-)
      use strict; my $a1 = 0; my $a2 = 1; my $a3 = 4; my $b1 = 8; my $b2 = 2; my $b3 = 9; my $c1 = 1; my $c2 = 4; my $c3 = 7; for (my $i=1; $i<=3; $i++) { print eval "\${a$i} . \${c$i}. \${c$i}" }
Re: for loop help
by samtregar (Abbot) on May 15, 2006 at 20:34 UTC
    Yes - you should be using arrays for this:

    my @a = (0, 1, 4); my @b = (8, 2, 9); my @c = (1, 4, 7); for my $i (0 .. 2) { print $a[$i], $b[$i], $c[$i]; }

    -sam

Re: for loop help
by dsheroh (Monsignor) on May 15, 2006 at 20:43 UTC
    is there something wrong with this?

    Yes, according to use strict:
    Can't use string ("a1") as a SCALAR ref while "strict refs" in use at ./foo line 10.

    That's not to say that you can't make it work that way, but it's really not a very good idea. As others have mentioned, arrays or a hash of arrays would be a much, much cleaner (and safer!) way to accomplish what you appear to be doing.

Re: for loop help
by rhxk (Beadle) on May 15, 2006 at 22:28 UTC
    Well first, thanx GrandFather for adding the code segments. I truly truly appreciate it.
    I just wanted to write something fast.

    Thanx for the info guys, after reading your suggestions and thinking about it for some time, the better way was to change my data structure...thanx!

    --Rob
Re: for loop help
by swampyankee (Parson) on May 15, 2006 at 20:48 UTC

    I tried it and it ran, so it seems to be syntactically correct. I suspect that you knew that, and were wondering about a slightly deeper problem.

    Since I've no clue as to what level of wrongness you're concerned with, I am left to wonder why why would you want to do it that way, vs using array syntax?. This would save having to have code such as

    $a494141 = exp(( 4.0*atan(1.0) * sqrt(-1)));

    emc

    "Being forced to write comments actually improves code, because it is easier to fix a crock than to explain it. "
    —G. Steele