Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Reading a Line into an Array

by jeroenes (Priest)
on May 31, 2001 at 10:56 UTC ( [id://84479]=note: print w/replies, xml ) Need Help??


in reply to Reading a Line into an Array

There are several ways to go. If you are really only concerned with the last empty line, just remove it with:
pop @array; #pop removes the last item of an array
But maybe you want to remove each newline at the end of the individual items?
chomp foreach( @array ); # the foreach loop 'remembers' the value
Or do you want remove all empty items as well?
chomp foreach( @array ); @array = grep { length } @array; #grep leaves out all items that have +a 'false' length
If you want, take a look at grep, foreach and length.

Hope this helps,

Jeroen
"We are not alone"(FZ)

Replies are listed 'Best First'.
Re: Re: Reading a Line into an Array
by grinder (Bishop) on May 31, 2001 at 11:43 UTC

    Actually, chomp will operate on an array if you ask it nicely:

    chomp(@a);

    Try out this one-liner if you like:

    perl -e '@a=("a","b","c\n");chomp @a;$"="-";print "[@a]\n"'

    --
    g r i n d e r
Re: Re: Reading a Line into an Array
by Chady (Priest) on May 31, 2001 at 11:46 UTC
    chomp foreach( @array );

    Didn't you just mean  chomp (@array); ?


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-23 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found