LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   perl array last 2 elements will not pop (https://www.linuxquestions.org/questions/programming-9/perl-array-last-2-elements-will-not-pop-804597/)

casperdaghost 04-27-2010 04:37 PM

perl array last 2 elements will not pop
 
created this array and the last two elements will not pop - phil and sylvia never get up to bat.

1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 my @players = qw{bob wayne ellen sylvia phil};
5 my @batters = (reverse @players);
6 my $p;
7 my $player;
8 my $batters;
9 my $next;
10 foreach $p (@players) { print "$p just arrived, adding $p to the batting roster\n"};
11 foreach $player (@batters) {
12 print "$player \n";
13 }
14 foreach $batters (@batters) {
15 $next = (pop @batters);
16 print "It is time for $next to be up\n";
17 sleep 5;
18 }
19 foreach $batters (@batters) {
20 print "$batters\n";
21 }


bob just arrived, adding bob to the batting roster
wayne just arrived, adding wayne to the batting roster
ellen just arrived, adding ellen to the batting roster
sylvia just arrived, adding sylvia to the batting roster
phil just arrived, adding phil to the batting roster
phil
sylvia
ellen
wayne
bob
It is time for bob to be up
It is time for wayne to be up
It is time for ellen to be up
phil
sylvia

smoker 04-27-2010 05:33 PM

the correct syntax is
$variable = pop(@array)
Line 20 should not print anything, as popping an array shortens it each time.
I would check your reverse statement too.

I would use a while loop instead of foreach, and getting rid of reverse and using shift instead of pop works fine. (so does a proper reverse statement).

Don't you think that reducing an array while using it as an index might cause problems ?

Code:

#!/usr/bin/perl -w
use strict;

 my @players = qw{bob wayne ellen sylvia phil};
 my @batters = reverse @players;
 my $p;
 my $player;
 my $batters;
 my $next;

 foreach $p (@players) { print "$p just arrived, adding $p to the batting roster\n"};
 foreach $player (@batters) {
 print "$player \n";
 }
 while(@batters) {
 $next = pop(@batters);
 print "It is time for $next to be up\n";
 sleep 5;
 }
 foreach $batters (@batters) {
 print "$batters\n";
 }


casperdaghost 04-27-2010 09:08 PM

thanks - found a good site on the difference between foreach and while loops(it has to do with windows powershell, but still ok.

http://www.winserverhelp.com/2010/04...hile-do-until/

ForEach Loop
The foreach statement is very useful in PowerShell when you need *to loop through an array or loop through items*in an object.
ForEach allows the loop to evaluate the number a certain type of objects in an array or parent object and loop through those objects. One thing to bear in mind is that a ForEach loop is that there is a performance penalty for this convenience.

While Loop
The only *difference between a For*loop and a While loop is that in the While loop the value is initialized before the loop. The While loop is a very simple construct it*doesn’t initialize or increment any fields automatically, it simply tests a condition and then executes the loop for as long as the condition remains true.**In the While loop,*the evaluation and the incremental part of the code are inside the loop as an expression:

also there is an article on perlmonks called while vs foreach:

http://www.perlmonks.org/?node_id=23019

smoker 04-27-2010 10:45 PM

You could use a foreach loop if you didn't pop the array. That's the problem, the loop index moves up as the end of the array moves down. They meet up and the loop finishes while there are still elements left.


All times are GMT -5. The time now is 03:01 AM.