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) | [reply] [d/l] [select] |
|
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
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
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 | [reply] [d/l] |
|
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;
| [reply] [d/l] |
Re: Reading a Line into an Array
by zeidrik (Scribe) on May 31, 2001 at 11:54 UTC
|
#!/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 | [reply] [d/l] |
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
| [reply] [d/l] [select] |
|
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!"
| [reply] [d/l] [select] |
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. | [reply] [d/l] |
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 | [reply] [d/l] |
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 | [reply] [d/l] [select] |