Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Printing Stacks

by Perlbotics (Archbishop)
on Mar 26, 2018 at 20:08 UTC ( [id://1211781]=note: print w/replies, xml ) Need Help??


in reply to Printing Stacks

A little rewrite using DATA instead of an external file and a loop.

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>; my @stack = <DATA>; #chomp @stack; #$a = pop @stack; #print $a . "\n"; #$b = pop @stack; #print $b . "\n"; #$c = pop @stack; #print $c . "\n"; print while defined ($_ = pop @stack); __DATA__ line 1 line 2 line 3 line 4

Result:

line 4 line 3 line 2 line 1

See also: reverse and File::ReadBackwards.

Replies are listed 'Best First'.
Re^2: Printing Stacks
by drose2211 (Sexton) on Mar 27, 2018 at 02:38 UTC

    I did it this way and it worked, but it all comes out on one line and I have tried adding a new line in several areas, but I keep getting errors. Where would I add a new line character to make sure they all come out on separate lines?

      Either refrain from chomping @stack or else replace print with say.

      I have tried adding a new line in several areas, but I keep getting errors.

      If you want help fixing errors you have to say what those errors were (precisely) and how you generated them (with an SSCCE). Just saying "I keep getting errors" is not informative enough.

        Sorry, didn't mean to be vague. Getting rid of the chomp worked.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1211781]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-29 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found