Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Specifiying a Range for Variables?

by dingus (Friar)
on Nov 27, 2002 at 17:22 UTC ( [id://216121]=note: print w/replies, xml ) Need Help??


in reply to Specifiying a Range for Variables?

Try
$cmd1 = 'This'; $cmd2 = 'Is'; $cmd3 = 'a'; $cmd4 = 'test'; my @cmds = map{eval '$cmd'.$_} (1..4);
There are also ways to do one eval of '($cmd1, $cmd2, $cmd3, $cmd4)', but I can't think of a way that does not require using a temporary variable like this:
my $t='('; $t.= "\$cmd$_, " for (1..4); @cmds = eval $t.')';
Update Just realised from reading Mr Muskrat's post below that you can interpolate and then use that to get another variable. Hence this may be your best version
$cmd1 = 'This'; $cmd2 = 'Is'; $cmd3 = 'a'; $cmd4 = 'test'; my @cmds = map{ ${"cmd$_"} }(1..4);

Dingus


Enter any 47-digit prime number to continue.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://216121]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-04-19 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found