The 16 line script below will take an arbitrary amount of data and run an arbitrary number of match criteria against it, only cycling over the data once. It uses a DATA section, but just replace the second argument to adTie() if the data and patterns are stored in files.
The script outputs:
record #2 contains pattern #1
record #3 contains pattern #2
record #4 contains pattern #1
record #4 contains pattern #2
#!perl -w
use strict;
use AnyData;
my($data,$patterns) = split /\n~\n/, join'',<DATA>;
my $dataHash = adTie( 'CSV',[$data],'r',{cols=>'id,d1,d2,d3,d4,d5'}
+ );
my $patternHash = adTie( 'CSV',[$patterns],'r',{cols=>'id,criteria'} )
+;
while (my $row = each %$dataHash) {
while (my $pattern = each %$patternHash) {
my $criteria = $pattern->{criteria};
for my $datum(qw(d1 d2 d3 d4 d5)) {
$criteria =~ s/$datum/$row->{$datum}/g;
}
next unless eval $criteria;
printf "record #%s contains pattern #%s\n",$row->{id},$pattern
+->{id};
}
}
__DATA__
1,9,9,9,9,9
2,3,5,9,9,9
3,9,9,7,8,4
4,3,5,7,8,4
~
1,d1==3 and d2==5
2,d3==7 and d4==8 and d5==4
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.