drose2211 has asked for the wisdom of the Perl Monks concerning the following question:
I am attempting to print out a stack. The method I am using works, but it looks like there is probably a better way to go about it. Is it possible to make a loop that pops each value off the end of the stack and prints it rather than the (probably) unnecessarily long way I am doing it here:
#!/usr/bin/perl use strict; use warnings; my $a, $b, $c, $d, $e; my $file = "data.txt"; open(my $fh,'<', $file) or die "can't open $file $!"; my @stack = <$fh>; chomp @stack; $a = pop @stack; print $a . "\n"; $b = pop @stack; print $b . "\n"; $c = pop @stack; print $c . "\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Printing Stacks
by BrowserUk (Patriarch) on Mar 26, 2018 at 20:09 UTC | |
|
Re: Printing Stacks
by Perlbotics (Archbishop) on Mar 26, 2018 at 20:08 UTC | |
by drose2211 (Sexton) on Mar 27, 2018 at 02:38 UTC | |
by hippo (Archbishop) on Mar 27, 2018 at 07:56 UTC | |
by drose2211 (Sexton) on Mar 27, 2018 at 14:33 UTC | |
|
Re: Printing Stacks
by AnomalousMonk (Archbishop) on Mar 26, 2018 at 20:29 UTC | |
|
Re: Printing Stacks
by BillKSmith (Monsignor) on Mar 26, 2018 at 22:18 UTC |