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

I have a function that has several variables called $val1, $val2, ... , $val10. In the function, I call another function that does work on each of the variables i.e. printOp ($val1) printOp ($val2) ... printOp ($val10) ------------------------------------- Is there any way to do this using a loop? i.e. for ( $i = 0 to 10 ) printOp ( $val$i ) }

Replies are listed 'Best First'.
Re: variable interpolation
by merlyn (Sage) on Feb 21, 2001 at 23:58 UTC
    I have a function that has several variables called $val1, $val2, ... , $val10.

    There's your problem. You want $val[1] through $val[10] instead. And maybe even $val[0].

    -- Randal L. Schwartz, Perl hacker

(ichimunki) re: variable interpolation
by ichimunki (Priest) on Feb 21, 2001 at 23:59 UTC
    read perldata. you want to use a "list" (aka "array") instead of enumerated scalar names.
Re: variable interpolation
by yakko (Friar) on Feb 22, 2001 at 00:35 UTC
    As has been said above, you most likely want a list (bka: array). Let's say you choose @val... Here's an example of what you can then do:
    foreach my $value (@val) { printOp($value); }

    --
    Me spell chucker work grate. Need grandma chicken.