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

Hello PerlMonks I am using a foreach loop to send commands to a command line of a device, I would then like to push that command into an array for storage at which the next command would be issued and then stored to the end of the array etc etc....so that I can use these values later. any idea how to do this when the commands are defined as a local my variable within the foreach loop? hope this makes sense?
foreach my $command1 (@commands){ 48 chomp $command1; 49 50 $command_run = $ssh->exec($command1); 51 52 print $command_run; 53 my @commands_run = (); 54 push (@commands_run, $command_run); -- want to push commands +issued into the @commands_run array. 55 56 } 57 #print @commands_run;
  • Comment on sending commands to CLI and then appending to end of array with push
  • Download Code

Replies are listed 'Best First'.
Re: sending commands to CLI and then appending to end of array with push
by davorg (Chancellor) on Jul 13, 2009 at 14:52 UTC

    You need to move the declaration of @commands_run out of the foreach loop - otherwise it gets created afresh on each iteration of the loop. And in your code there seems to be some confusion about the name of the variable that contains the command. Is it $command1 or $command_run? Looks like $command_run contains the result of running $command1.

    But once those little issues are sorted out, something very similar to your code will work just fine.

    my @commands_run; foreach my $command1 (@commands) { chomp $command1; my $command_run = $ssh->exec($command1); print $command_run; push @commands_run, $command1; } print @commands_run;

    Update: Actually, once that code has been run there will be no difference between @commands and commands_run. What were you expecting?

    --

    See the Copyright notice on my home node.

    Perl training courses

Re: sending commands to CLI and then appending to end of array with push
by jethro (Monsignor) on Jul 13, 2009 at 14:53 UTC

    If you want to collect something into an array it is not beneficial to clear that array in every loop iteration. Put my @commands_run = (); before the loop instead of inside the loop and it should work

      Thanks that sorted the problem perfectly !!!!
Re: sending commands to CLI and then appending to end of array with push
by kennethk (Abbot) on Jul 13, 2009 at 14:52 UTC
    This sounds like you are having some confusion with how scoping operates in Perl. If you have a value stored in a lexically scoped variable, that variable will fall out of scope at the end of your block, and hence you can no longer access the variable by that name. The solution, of course, is to scope your variables correctly. Since the scope of you @commands_run array is one level up, you need to declare that variable at the block level. Something like:

    my @commands_run = (); #<-- moved foreach my $command1 (@commands){ 48 chomp $command1; 49 50 $command_run = $ssh->exec($command1); 51 52 print $command_run; 54 push (@commands_run, $command_run); -- want to push commands +issued into the @commands_run array. 55 56 } 57 #print @commands_run;