Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, How do I append the results from bsd_glob to an existing array? Here's what I thought I should be able to do ...
my @list = bsd_glob ("$DIR/*txt", GLOB_QUOTE ); @list .= bsd_glob ("$DIR/*XYZ", GLOB_QUOTE );
... but that does work. What should I have done? Thanks

Replies are listed 'Best First'.
Re: Can't modify private array in concatenation?
by jettero (Monsignor) on Dec 22, 2006 at 22:11 UTC

    Here are a couple of ways. (There are more.)

    1. @list = (@list, bsd_glob ("$DIR/*XYZ", GLOB_QUOTE )); ...
    2. push @list, bsd_glob ("$DIR/*XYZ", GLOB_QUOTE ));

    The . operator is strictly for string concatenation. And in your code it attempts to "coerce" the list into a string.

    -Paul

Re: Can't modify private array in concatenation?
by liverpole (Monsignor) on Dec 22, 2006 at 22:45 UTC
    You can do it either way, as jettero showed:
    @list = (@list, bsd_glob ("$DIR/*XYZ", GLOB_QUOTE )); ... push @list, bsd_glob ("$DIR/*XYZ", GLOB_QUOTE ));

    (Update:  Replaced the code sample I inadvertently copied from the OP with the one I meant to copy, from jettero).

    But there's an interesting difference which becomes very clear for very large lists, which is that using push is more efficient.

    For example, running the following takes about 1 second for the subroutine list_push to run, but 3 to 5 seconds for the subroutine list_combine:

    use strict; use warnings; my $size = 1_000_000; list_push(); #list_combine(); sub list_combine { print "Creating list1 ...\n"; my @list1 = ( "a" ) x $size; print "Creating list2 ...\n"; my @list2 = ( "b" ) x $size; print "Concatenating lists...\n"; my $start = time; @list1 = (@list1, @list2); my $elapsed = time - $start; my $size = @list1; print "List size = $size, elapsed = $elapsed second(s)\n"; } sub list_push { print "Creating list1 ...\n"; my @list1 = ( "a" ) x $size; print "Creating list2 ...\n"; my @list2 = ( "b" ) x $size; print "Pushing lists...\n"; my $start = time; push @list1, @list2; printf "Elapsed = %d\n", time - $start; my $elapsed = time - $start; my $size = @list1; print "List size = $size, elapsed = $elapsed second(s)\n"; }

    It gets much worse for $size = 2_000_000, where list_combine causes my computer thrash as it runs out of available memory.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

      I can only imagine how interesting the IO problem would be with a directory of a million items.

        Well, with a million or two you'd almost have to do something like this instead...

        opendir my $dir, "." or die $!; while( my $name = readdir $dir ) { next unless $name =~ m/(?:XYZ|\.txt|\w)/; print "doing something on $name\n"; # more code here } closedir $dir;

        -Paul

Re: Can't modify private array in concatenation?
by gaal (Parson) on Dec 22, 2006 at 22:12 UTC
    Concatenation is an operator on scalar strings. You want a push:

    push @list, bsd_glob ("$DIR/*XYZ", GLOB_QUOTE );