in reply to iterating over array to create smaller arrays based on pattern match
Seems like you need to "sub-select" your list. Given that it's not in order, I'd build a hash of arrays (HoA) with the keys corresponding to your selections, then process them in whatever order you wanted.
#!/usr/bin/perl -w use strict; my @array = qw(001.file.a 001.file.b 002.file.a 002.file.b); my %hash; for my $filename (@array){ $filename =~ /^(\d+)/; push @{$hash{$1}}, $filename; } for my $group (sort { $a <=> $b } keys %hash){ print "Processing group '$group':\n"; for my $file (@{$hash{$group}}){ print "\tProcessing $file:\n"; ### Do stuff } }
Update: Wow, you guys are quick. I hit the 'Comment' link, typed out the code, previewed it, and posted it - and suddenly there were three posts ahead of me where there had been zero. I guess I'd better learn to type faster. :) It's also amusing (but unsurprising) that all of us gave essentially the same answer, so I'm going to '++' everyone 'cause they're so brilliant. :)
-- Human history becomes more and more a race between education and catastrophe. -- HG Wells
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: iterating over array to create smaller arrays based on pattern match
by phippsy (Initiate) on Apr 18, 2008 at 15:35 UTC |