LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-27-2010, 04:37 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
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

Last edited by casperdaghost; 04-27-2010 at 04:41 PM.
 
Old 04-27-2010, 05:33 PM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
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";
 }

Last edited by smoker; 04-27-2010 at 06:12 PM.
 
Old 04-27-2010, 09:08 PM   #3
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
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

Last edited by casperdaghost; 04-27-2010 at 09:19 PM.
 
Old 04-27-2010, 10:45 PM   #4
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with writing array elements into a file jayran Linux - Newbie 22 12-16-2009 07:21 AM
printing array contents after "push" of elements gives a different output in PERL gaynut Programming 1 08-20-2008 04:04 AM
trimming perl array elements homey Programming 7 02-17-2008 03:48 PM
Deleting elements from array in perl with splice signalno9 Programming 2 08-16-2005 10:57 PM
perl - get number of elements in an array AM1SHFURN1TURE Programming 3 03-07-2005 03:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration