Home > Archive > PERL Miscellaneous > May 2006 > better idiom for appending to list from file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
better idiom for appending to list from file
|
|
| Mirco Wahab 2006-05-30, 7:04 pm |
| Hi all,
is there a typical Perl-spell when
having a list:
@Array = qw( a b c d );
that needs to be appended from a file:
[somefile.txt]
ee
ff
gg
hh
[EOF]
I'd use
splice @Array, @Array, 0, map{ chomp; $_ } <FH>;
for that, but it may look ugly to others.
Any ideas?
Regards
Mirco
| |
| Paul Lalli 2006-05-30, 7:04 pm |
| Mirco Wahab wrote:
> is there a typical Perl-spell when having a list:
>
> @Array = qw( a b c d );
>
> that needs to be appended from a file:
>
> [somefile.txt]
> ee
> ff
> gg
> hh
> [EOF]
>
> I'd use
>
> splice @Array, @Array, 0, map{ chomp; $_ } <FH>;
>
> for that, but it may look ugly to others.
>
> Any ideas?
Well, first of all,
splice @array, @array, 0, LIST
is exactly equivalent to:
push @array, LIST
so that's one optimization you could make. As for reading the file and
getting rid of the newlines, I might try to take advantage of a module
like Tie::File or IO::All:
tie my @file, 'Tie::File', 'file.txt' or die "Can't tie: $!";
push @array, @file;
(Tie::File auto-chomps the lines for you. Read its docs for more
information...)
Paul Lalli
| |
| Ted Zlatanov 2006-05-30, 7:04 pm |
| On 30 May 2006, wahab@chemie.uni-halle.de wrote:
> is there a typical Perl-spell when
> having a list:
>
>
> that needs to be appended from a file:
>
> [somefile.txt]
> ee
> ff
> gg
> hh
> [EOF]
>
> I'd use
>
> splice @Array, @Array, 0, map{ chomp; $_ } <FH>;
>
> for that, but it may look ugly to others.
If your goal is to write nice, portable, maintainable code, you want
to do this:
while (<FH> )
{
chomp;
push @Array, $_;
}
It's longer (4 lines vs. 1), but when you revisit it 3 years later,
you won't spend precious time wondering what it does. You might make
a small argument for efficiency, but I would guess the performance
difference is really small and depends on many other factors. For
large files, in particular, creating the temporary list of all lines
can be deadly for your memory usage, whereas line-by-line processing
will not use much memory at all.
The one-line version would be
push @Array, map { chomp; $_ } <FH>;
but I would hate to see that in code I had to maintain. The
documentation needed and possible confusion are just not worth the
cleverness.
Ted
| |
| Brian McCauley 2006-05-31, 7:06 pm |
|
Mirco Wahab wrote:
> Hi all,
>
> is there a typical Perl-spell when
> having a list:
>
> @Array = qw( a b c d );
>
> that needs to be appended from a file:
>
> [somefile.txt]
> ee
> ff
> gg
> hh
> [EOF]
>
> I'd use
>
> splice @Array, @Array, 0, map{ chomp; $_ } <FH>;
>
> for that, but it may look ugly to others.
>
> Any ideas?
push @Array, <FH>;
chomp @Array;
This harmlessly chomps the elements that were already in @Array.
|
|
|
|
|