#!/usr/bin/perl -- use strict; use warnings; use diagnostics; use Text::CSV; use Text::CSV_XS qw( ); #main { use autodie 2.06; open( my $fh_in, '<', 'data.txt' ); my $col_rules = [ [ [ 1 => eq => 'Apple'], [ 0 => eq => 'CX89'], [ "%s %s tree\n" => [ 0, 1 ] ], ], [ [ 1 => eq => 'Sailing'], [ 0 => eq => 'CX89'], [ "%s %s boat\n" => [ 0, 1 ] ], ], [ [ 0 => ne => 'CX89'], [ 1 => eq => 'Apple'], [ 1 => ne => 'Sailing'], [ "%s %s candy\n" => [ 0, 1 ] ], ], ]; process_rules( $col_rules, $fh_in, \*STDOUT ); } sub process_rules { my ( $rules, $in, $out ) = @_; my $csv_in = Text::CSV->new( { sep_char => ',', eol => $/ } ) or die Text::CSV->error_diag(); my $csv_out = Text::CSV->new( { eol => $/ } ) or die Text::CSV->error_diag(); while ( my $row = $csv_in->getline($in) ) { my $rulematch = 0; for my $ruleset (@$rules) { last if $rulematch; my $ruleset_match = 0; for my $rule ( @$ruleset[0 .. $#$ruleset ] ) { my ( $ix, $op, $val ) = @$rule; $ruleset_match++ if $op eq 'eq' and $val eq $row->[$ix]; $ruleset_match++ if $op eq 'ne' and $val ne $row->[$ix]; } #~ warn '$rulematch ',$rulematch; #~ warn '$ruleset_match ', $ruleset_match ; #~ warn '$#$ruleset ', $#$ruleset ; if ( $ruleset_match == $#$ruleset ) { $rulematch++; my ( $printf, $indices ) = @{ $ruleset->[-1] }; $out->printf( $printf, (@$row)[@$indices] ); } } } } __DATA__ "CX89",Apple "CX89",Sailing "CX89",Apple "AC75",Apple "CX89",Sailing