Re: better idiom for appending to list from file



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

.



Relevant Pages

  • Re: Picking Element from Array one by one
    ... >> I am getting some values from loop and i store all that value in array ... >> using push function. ... no correllation. ... Please Rita, get it together woman. ...
    (comp.lang.perl.misc)
  • Re: The prodigal son returns...more easy Homework Help...
    ... Looking at your output, you're obviously "running off the end" of the "lower" array, and printing one number from the "higher" array. ... to push and pop 16 bit values as long as the stack remain aligned ... There may be other places where you want to change from 16-bit registers to 32-bit registers, ... It "sounds logical" that 16-bit calculations would be faster and "easier for the CPU". ...
    (alt.lang.asm)
  • Please help with convoluted script
    ... I have a script which is supposed to query a database and compile data for every ... push @months, $month; ... # Build the array which will hold the dates for the previous week ... # so we can output the proper string later. ...
    (perl.beginners)
  • Re: (trivial) unshift and shift vs push and pop
    ... Whenever the allocated memory for the array of SV pointers is full (or ... Perl allocates new memory, and copies the pointers over. ... than push, since grow-for-unshift involves something reallocish, ...
    (comp.lang.perl.misc)
  • Re: why the perl docs suck
    ... Treats ARRAY as a stack, and pushes the values of LIST onto the end of ... trying to program Perl. ... It is explaining exactly what the "push" ...
    (comp.lang.perl.misc)