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

Does anyone know if it's possible to do something like this:
#!/usr/local/bin/perl -w $cmd1 = 'This'; $cmd2 = 'Is'; $cmd3 = 'a'; $cmd4 = 'test'; my @cmds = ($cmd1..$cmd4); for (@cmds){ print "$_\n"; }
I tried it a few differnt ways, but I'm not having much luck.

Thanks,
Dru
Another satisfied monk.

Replies are listed 'Best First'.
Re: Specifiying a Range for Variables?
by tachyon (Chancellor) on Nov 27, 2002 at 17:19 UTC
    # perhaps you want this my $cmd1 = 'This'; my $cmd2 = 'Is'; my $cmd3 = 'a'; my $cmd4 = 'test'; push @cmds, ( $cmd1, $cmd2, $cmd3, $cmd4 ); print "$_\n" for @cmds; # or perhaps you want this @cmds = qw( This is a test ); print "$_\n" for @cmds; # or perhaps if you want commands that do something and can only be x +y z my %command = ( this => sub { print "Doing this!\n" }, that => sub { print "Doint that!\n" }, other=> sub { print "Doin' t' other brother!\n" }, exit => sub { print "Goodbye!\n"; exit }, ); my $cmd_list = join ', ', keys %command; while (1) { print "Command ($cmd_list): "; chomp (my $do_it = <> ); if ( $command{$do_it} ) { &{$command{$do_it}}; } else { print "Don't know how to $do_it!\n"; } } __DATA__ C:\>perl test.pl Command (other, this, that, exit): help Don't know how to help! Command (other, this, that, exit): other Doin' t' other brother! Command (other, this, that, exit): exit Goodbye! C:\>

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Specifiying a Range for Variables?
by joealba (Hermit) on Nov 27, 2002 at 17:20 UTC
    Here's one way.
    $cmd1 = 'This'; $cmd2 = 'Is'; $cmd3 = 'a'; $cmd4 = 'test'; my @cmds = map {${"cmd$_"}} (1..4); for (@cmds){ print "$_\n"; }
    I'll refrain from the obligatory "You should use a hash instead..." statement on the assumption that this is simply a learning exercise. :)

      Why? Well if you won't, then I shall. :)

      - That should be a hash.

      Further materials: Dominus' archived newsposts, part one, two and three on Why it's stupid to 'use a variable as a variable name'.

      Makeshifts last the longest.

Re: Specifiying a Range for Variables?
by thelenm (Vicar) on Nov 27, 2002 at 17:24 UTC
    You'll have to list them all out individually if you want to do it that way, i.e.
    my @cmds = ($cmd1, $cmd2, $cmd3, $cmd4);
    Then again, why not put them all in an array in the first place? Then you won't have to worry about creating an array separately. You can create the array all at once, or pieces at a time, depending on what makes sense with what the rest of your code is doing.
    # Create all at once my @cmds = qw(This Is a test); # -OR- Create one at a time my @cmds; push @cmds, 'This'; push @cmds, 'Is'; push @cmds, 'a'; push @cmds, 'test'; # Then use the array of cmds for (@cmds) { print "$_\n"; }

    -- Mike

    --
    just,my${.02}

Re: Specifiying a Range for Variables?
by Mr. Muskrat (Canon) on Nov 27, 2002 at 17:25 UTC

    This is probably not the best way to do it nor the best thing to do...

    #!/usr/bin/perl $cmd1 = 'This'; $cmd2 = 'Is'; $cmd3 = 'a'; $cmd4 = 'test'; my @cmds = (1..4); for (@cmds){ print ${"cmd$_"},$/; }

Re: Specifiying a Range for Variables?
by dingus (Friar) on Nov 27, 2002 at 17:22 UTC
    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.