http://www.perlmonks.org?node_id=626728


in reply to Counting the Times Two Unknown Values Appear Together

If you know the position of the fields in the line there's no reason you can't use a regex to do the job. Capture the 10th field then use a back-reference to the 10th in the 13th position.

use strict; use warnings; my @strings = qw{ 0;1;2;3;4;5;6;7;8;9;10;11;12;13 0;1;2;3;4;5;6;7;8;9;10;11;9;8 0;1;2;3;4;5;6;7;8;9;10;11;99 0;1;2;3;4;5;6;7;8;9;10;11;9 0;1;2;3;4;5;6;7;8;;10;11;;13 0;1;2;3;4;5;6;7;8;;10;11;12;13 }; my $rxByPosn = qr {(?x) \A # Start of string (?:[^;]*;){9} # Nine fields/delimiters ([^;]*) # Capture 10th field, which # could be blank ; # Delimiter (?:[^;]*;){2} # Two more fields/delimiters \1 # 13th field must match 10th (?:;|\z) # Delimiter or end of string }; foreach my $string ( @strings ) { print qq{String - $string\n}, $string =~ $rxByPosn ? qq{ Col. 10 matches col. 13\n} : qq{ Cols 10 and 13 differ\n} ; }

The output.

String - 0;1;2;3;4;5;6;7;8;9;10;11;12;13 Cols 10 and 13 differ String - 0;1;2;3;4;5;6;7;8;9;10;11;9;8 Col. 10 matches col. 13 String - 0;1;2;3;4;5;6;7;8;9;10;11;99 Cols 10 and 13 differ String - 0;1;2;3;4;5;6;7;8;9;10;11;9 Col. 10 matches col. 13 String - 0;1;2;3;4;5;6;7;8;;10;11;;13 Col. 10 matches col. 13 String - 0;1;2;3;4;5;6;7;8;;10;11;12;13 Cols 10 and 13 differ

I hope this is of use.

Cheers,

JohnGG