in reply to Best way to find patterns in csv file?

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