Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Best way to match items in an array

by stefp (Vicar)
on Dec 07, 2002 at 06:29 UTC ( #218227=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://218227]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2021-03-04 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (107 votes). Check out past polls.

    Notices?