Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Reading a Line into an Array

by scrubber (Sexton)
on May 31, 2001 at 10:26 UTC ( #84474=perlquestion: print w/ replies, xml ) Need Help??
scrubber has asked for the wisdom of the Perl Monks concerning the following question:

Hi.
I am reading a file and entering each line into an array. For some reason the last new line is also entered into it. I have tried using chomp to remove it, but this doesn't work -
chomp ( $array[ scalar(@array) -1 ] );
Any ideas?

Comment on Reading a Line into an Array
Download Code
Re: Reading a Line into an Array
by jeroenes (Priest) on May 31, 2001 at 10:56 UTC
    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)

      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
      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/
Re: Reading a Line into an Array
by yakko (Friar) on May 31, 2001 at 11:02 UTC
    Assuming you're splitting on space-like chars, this ought to get you started:
    while(<FD>) { chomp; my @array=split(/\s/); # or /\s+/ if you want to collapse whitespa +ce ... }

    --
    Me spell chucker work grate. Knead grandma chicken.

Re: Reading a Line into an Array
by zeidrik (Scribe) on May 31, 2001 at 11:54 UTC
    This works:
    #!/usr/bin/perl -w use strict; open(R,"textfile"); my @F=<R>; close(R); pop @F if (@F[scalar(@F)-1] eq "\n"); print @F;
    Regards
Re: Reading a Line into an Array
by iakobski (Pilgrim) on May 31, 2001 at 11:56 UTC
    By the way, in Perl you don't often have to find out how many elements there are in an array. If you want the last element use: my $last = $array[ -1 ];

    -- iakobski

      Iakobski is right. This is smarter :)
      #!/usr/bin/perl -w use strict; open(R,"textfile"); my @F=<R>; close(R); pop @F if $F[-1] eq "\n"; print @F;
Re: Reading a Line into an Array
by scrubber (Sexton) on May 31, 2001 at 12:22 UTC
    thanks, this is all very helpful. I would like to point out that I already knew about $array[-1] for the last element, if you check out the middle of the code. But thanks again, I've got lots of stuff to work with!
    -scrubber
      if you undef $/; then you can say my @array = split /\n+/, <DATA>; and this will ignore all the empty lines.

      Update
      Sorry about the minimalist post - didn't have any time earlier. The reason this works is that $/ (newline by default) is used as the end-of-line character when reading from a filehandle. If you undefine it then no end of line is seen and the whole file is "slurped" in, in one go. "split"ting this on one (or more) newlines does the "chomp"ing for you while also returning a list. hope this makes it a bit clearer.

      "Argument is futile - you will be ignorralated!"

Re: Reading a Line into an Array
by MrCromeDome (Deacon) on May 31, 2001 at 19:08 UTC
    I'm not saying that this is any better or worse than what has already been posted, but you might wish to check out the File::Slurp, which already does what you are trying to do. It's as simple as:
    use File::Slurp; @array = read_file($filename);
    HTH! MrCromeDome
Re: Reading a Line into an Array
by John M. Dlugosz (Monsignor) on May 31, 2001 at 23:13 UTC
    I read your question a little differently, so maybe this is what you are thinking: You read in the whole file with something like @array=<FILE>; but any blank lines at the end of the file gave you empty array elements. Removing the trailing newline from the elements still leaves you with empty elements. Clean this up with: pop @array  while (@array && $array[-1] eq '');

    —John

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://84474]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-09-21 20:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (175 votes), past polls