Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't this simple Perl push/pop program working?

Tags:

perl

I'm reading the Llama (Learning Perl) book, and working on the exercises. And for this exercise:

Write a program that reads a list of strings on separate lines until end-of-input and prints out the list in reverse order. [. . .]

Well, I already figured out a simpler approach (I remembered you could use reverse on arrays... Perl is so... awesome so far), but I am wondering why this one isn't working.

#!/usr/bin/perl

use 5.010;

chomp(@strings = <STDIN>);

foreach (@strings){
    push @revstrings, $_;
}

while($i++ <= scalar @revstrings){
    say pop @revstrings;
}

It goes like this:

$ ./first
one
two
three

[^D]
three
two
$

the output is the same if I change the <= in the while loop to just <.

like image 292
Carson Myers Avatar asked Jul 04 '09 04:07

Carson Myers


1 Answers

You'll never get past halfway... Each time through that last iteration, you'd get:

  • $i++ means that $i will increase by one;
  • pop @revstrings will mean that scalar @revstrings will decrease by one.

They'll meet in the middle, when $i++ just exceeds half the original @revstrings length.

Actually, the $i++ is unnecessary, since scalar @revstrings will be zero when the array is empty, so you just need:

while(scalar @revstrings){
    say pop @revstrings;
}
like image 157
Stobor Avatar answered Nov 08 '22 22:11

Stobor