http://qs321.pair.com?node_id=218227


in reply to Best way to match items in an array

The first pass gathers the filenames in %file. The second pass handles the files suffix per suffix if an handler of the form handle_XXX is defined for the suffix XXX. Note that you can add new handlers without touching the logic.
#! /usr/bin/perl use strict; my %file; # hash of filenames arrays keyed by suffix # first pass for (<*>) { push @{$file{$2}}, $_ if /(.*\.(.*))/; } # second pass for my $suffix (sort keys %file) { no strict 'refs'; next unless defined &{"handle_$suffix"}; &{"handle_$suffix"}($_) for sort @{$file{$suffix}} } # process the file with suffix '.cpp' sub handle_cpp { print "$_[0]\n"; }

-- stefp