I was trying to solve an exercise in "Learning Perl" by Schwartz, when I happened across an unexpected output in the code I wrote. I was wondering what I've done wrong.
Qn: Implement a simple tac similiar to the unix utility.
My solution:
#!/usr/bin/perl
use strict;
use warnings;
my @array;
while (<>) {
push @array, $_;
}
foreach ($#array..0) {
print $array[$_];
}
Implementing it with: $ ./tac list
where list contains:
$ cat list
An apple
Blue boys
Coy cows
Dreary ducks!
gives no output.
$ ./tac list
$
Your problem is the foreach
loop counter. You can't go backwards like that.
You could instead do:
while (@array) {
print pop @array;
}
or just:
print pop @array while (@array);
while (@array)
will evaluate @array
in a scalar context, which means that the size of the array will be tested. When the size reaches zero, the loop will terminate.
As this is perl, there are of course a million ways to do this. Another would be:
print for reverse @array
...or you could read in the array backwards by using unshift
:
#!/usr/bin/perl
use strict;
use warnings;
my @array;
while (<>) {
unshift @array, $_;
}
print for @array;
...or you could even just do:
#!/usr/bin/perl
use strict;
use warnings;
print reverse <>;
Enjoy learning Perl!
The range notation doesn't count down:
$ perl -e 'foreach my $i (10..0) { print "$i\n"; }'
$
You can use:
#!/usr/bin/perl
use strict;
use warnings;
my @array = <>;
foreach (0..$#array)
{
print $array[$#array - $_];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With