ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
Last edited by casperdaghost; 04-27-2010 at 04:41 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";
}
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.