Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Counting the Times Two Unknown Values Appear Together

by johngg (Canon)
on Jul 15, 2007 at 16:32 UTC ( #626728=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2019-02-15 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I use postfix dereferencing ...









    Results (95 votes). Check out past polls.

    Notices?