in reply to Can't modify private array in concatenation?

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$..$/

Replies are listed 'Best First'.
Re^2: Can't modify private array in concatenation?
by chromatic (Archbishop) on Dec 22, 2006 at 23:43 UTC

    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

        or start thinking directory {of directories}+ ;) the directory entry will be about 20-40M, reading it is still OK, the real fun is when you have to rename/move a few files

        cheers --stephan