diera
diera

Reputation: 1

how to make pair string comparision from csv file in perl

i have a problem with the code to make string pair comparison. my data in csv file as below

WorkerId,query,sysa,sysb,Answer.selectedsys

A2ZBU2WW9WHXB7,male,lucene-std-rel,random,lucene-std-rel
A2ZH0XH25HXD1Y,male,lucene-std-rel,random,lucene-std-rel

A1V89RF7V0DCF5,male,lucene-noLen-rel,lucene-noLen-rr,lucene-noLen-rel
AZ92SE5L1P238,male,lucene-noLen-rel,lucene-noLen-rr,lucene-noLen-rr 

A2ZBU2WW9WHXB7,male,lucene-std-rel,lucene-noLen-rr,lucene-std-rel
A1DGNCZZWELLVX,male,lucene-std-rel,lucene-noLen-rr,lucene-noLen-rr

the output results should be as below:

compare: lucene-std-rel      random     
result: lucene-std-rel-->2           random--> 0        draw (choose both)--> 0 

compare: lucene-noLen-rel    lucene-noLen-rr    
result: lucene-noLen-rel=1   lucene-noLen-rr =1     draw=0

compare: lucene-std-rel lucene-noLen-rel    
result: lucene-std-rel= 1    lucene-noLen-rel=1     draw=0.

my code: it seems just count if user choose either sysA, sysB or both without considering the pair of string.

for($i=0;$i<@query;++$i) {
if ( $field[1] eq $query[$i] ) {
    if ( $field[4] eq $field[2]) {
    print "$query[$i]: $field[4]\n";
    $counta++;
    } 
    if ( $field[4] eq $field[3]) {
    print "$query[$i]: $field[4]\n";
    $countb++;
    }
    if ( $field[4] eq ($field[2] && $field[3])) {
    print "$query[$i]: $field[4]$field[3]\n";
    $countc++;

}

any help is very much appreciated.

Upvotes: 0

Views: 288

Answers (2)

Shalini
Shalini

Reputation: 455

The '&&' operator returns the last value evaluated. The '&&" operator first evaluates the left hand operand followed by the right hand operand and returns the right hand operand. So in the expression - ( $field[4] eq ($field[2] && $field[3])), it essentially means you are doing ( $field[4] eq $field[3] ).

Of course assuming that both the left hand and the right hand operand are true.

As suggested by @mob, you should probably do - ( $field[4] eq $field[2] && $field[4] eq $field[3] )

Upvotes: 0

mob
mob

Reputation: 118605

This line

if ( $field[4] eq ($field[2] && $field[3])) {

is probably not doing what you expect. It is not equivalent to

if ($field[4] eq $field[2] && $field[4] eq $field[3]) {

Upvotes: 3

Related Questions