Good day.
I am new to Perl and I am writing some code just to get an idea of how Perl works. i am faced with the following problem:
I am using a foreach loop and within that loop, popping and printing the contents of an array. However, the loop does not continue to the end of the array and some stuff is still left on the array.
Could someone please tell me why this is possibly happening? Code snippet and output below.
Thanks alot. -MisterT
#arrays as stacks
my @stack1 = qw/this is a good feature/;
print "initial array contains : @stack1\n";
push(@stack1,"in");
print "@stack1\n";
push(@stack1,"perl");
print "@stack1\n";
my $var = pop(@stack1);
print "$var popped of arraystack which now contains : @stack1\n";
push(@stack1, 7);
push(@stack1, 8);
$var = pop(@stack1) + pop(@stack1);
print"$var\n";
#arrays as queues
print "@stack1\n";
unshift(@stack1,"thing");
print "@stack1\n";
foreach(@stack1){
my $qq = pop(@stack1);
print "$qq popped from end of queue\n";
print "left on stack : @stack1 \n";
print "stack size is $#stack1 \n";
}
##output
initial array contains : this is a good feature
this is a good feature in
this is a good feature in perl
perl popped of arraystack which now contains : this is a good feature
+in
15
this is a good feature in
thing this is a good feature in
in popped from end of queue
left on stack : thing this is a good feature
stack size is 5
feature popped from end of queue
left on stack : thing this is a good
stack size is 4
good popped from end of queue
left on stack : thing this is a
stack size is 3
a popped from end of queue
left on stack : thing this is
stack size is 2
C:\Documents and Settings>
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.