in reply to iterating over array to create smaller arrays based on pattern match

use strict; use warnings; my @files = qw(001.file.a 001.file.b 002.file.a 002.file.b); my @sorted_files; for (@files){ if (m/^(\d+)\./){ push @{$sorted_files[$1]}, $_; } else { die "File name with unknown format: '$_'\n"; } } for my $bucket (0 .. $#sorted_files){ print "Processing bucket $bucket\n"; for (@{$sorted_files[$bucket]}){ print "\tprocessing file $_\n"; } } __END__ Processing bucket 0 Processing bucket 1 processing file 001.file.a processing file 001.file.b Processing bucket 2 processing file 002.file.a processing file 002.file.b
  • Comment on Re: iterating over array to create smaller arrays based on pattern match
  • Download Code

Replies are listed 'Best First'.
Re^2: iterating over array to create smaller arrays based on pattern match
by samtregar (Abbot) on Apr 17, 2008 at 22:11 UTC
    Danger! If you ever encounter a file called "10000000000000000.file.a" your program will run out of memory and crash. Perl's arrays are not sparse so when you ask for $array[10000000000000000] you're going to allocate a great hunk of memory.

    -sam