Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Comparing arrays with text contents

by rsriram (Hermit)
on Jun 21, 2006 at 10:42 UTC ( [id://556624]=perlquestion: print w/replies, xml ) Need Help??

rsriram has asked for the wisdom of the Perl Monks concerning the following question:

I need to compare two arrays:

@anc=("BX1.01.001","BX1.01.002","BX1.01.003","BX1.01.004","BX1.01.005" +,"BX1.01.006"); @ind=("BX1.01.002","BX1.01.002","BX1.01.003","BX1.01.004","BX1.01.005" +,"BX1.01.006");

My function to compare these two array's is

sub comp_arr { @arr1=sort($_[0]); @arr2=sort($_[1]); # To check if the number of elements in both arrays match if ($#arr1 != $#arr2) { print "Indicator and anchor count do not match"; exit; } #Actual compare starts here for ($x=0; $x <= $#arr2; $x++) { if ($arr2[$x] ne $arr1[$x]) { print ("Anchor/indicator mismatch in $arr1[$x]\n"); } }

Even if the arrays are similar or if there are any differences, I get an error message stating there is a mismatch in the first element. Can someone help me out by telling me what's wrong in the above codes?

Sriram

proper code tags by holli

Replies are listed 'Best First'.
Re: Comparing arrays with text contents
by Zaxo (Archbishop) on Jun 21, 2006 at 10:53 UTC

    Your,

    @arr1=sort($_[0]); @arr2=sort($_[1]);
    puts exactly one element in each array, a reference to the corresponding array. They will never match unless you compare an array to itself.

    You want,

    my @arr1 = sort @{$_[0]}; my @arr2 = sort @{$_[1]};

    Your for loop is more perlishly written,

    for (0 .. $#arr1) { if ($arr2[$_] ne $arr1[$_]) { print "Anchor/indicator mismatch in $arr1[$_]\n"; } }
    but what you have will work fine.

    You might be interested in the Algorithm::Diff package.

    After Compline,
    Zaxo

Re: Comparing arrays with text contents
by Samy_rio (Vicar) on Jun 21, 2006 at 11:00 UTC

    Hi, Try this using Array::Compare module.

    TIMTOWTDI

    use strict; use warnings; use Array::Compare; my @anc=("BX1.01.001","BX1.01.002","BX1.01.003","BX1.01.004","BX1.01. +005","BX1.01.006"); my @ind=("BX1.01.002","BX1.01.002","BX1.01.003","BX1.01.004","BX1.01. +005","BX1.01.006"); my $comp = Array::Compare->new(); if (!($comp->compare_len(\@anc, \@ind))){ print "Indicator and anchor count do not match"; exit; } if ($comp->compare(\@anc, \@ind)){ print "Same Array OK"; }else{ print "Different Array OK"; }

    Use $comp->perm(\@anc, \@ind) for compare the array without order.

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: Comparing arrays with text contents
by mickeyn (Priest) on Jun 21, 2006 at 10:55 UTC
Re: Comparing arrays with text contents
by prasadbabu (Prior) on Jun 21, 2006 at 10:56 UTC

    The following code works for me. You are using sort function wrongly as Zaxo indicated. Also i think you are not sending the arrays as reference for subroutine parameters. You have to send the arrays as reference for subroutine parameters. For example,  comp_arr (\@anc, \@ind)

    use strict; use warnings; my @anc=("BX1.01.001","BX1.01.002","BX1.01.003","BX1.01.004","BX1.01.0 +05","BX1.01.006"); my @ind=("BX1.01.002","BX1.01.002","BX1.01.003","BX1.01.004","BX1.01.0 +05","BX1.01.006"); comp_arr (\@anc, \@ind); sub comp_arr { my @arr1= sort (@{$_[0]}); my @arr2= sort (@{$_[1]}); if ($#arr1 != $#arr2) { print "Indicator and anchor count do not match"; exit; } for my $x (0..$#arr2) { print "$arr1[$x]\t$arr2[$x]\n"; print ("Anchor/indicator mismatch in $arr1[$x]\n") if ($arr2[$ +x] ne $arr1[$x]); } } output: ------- BX1.01.001 BX1.01.002 Anchor/indicator mismatch in BX1.01.001 BX1.01.002 BX1.01.002 BX1.01.003 BX1.01.003 BX1.01.004 BX1.01.004 BX1.01.005 BX1.01.005 BX1.01.006 BX1.01.006

    Prasad

Re: Comparing arrays with text contents
by Moron (Curate) on Jun 21, 2006 at 11:38 UTC
    ( $#ar1 == $#ar2 ) and ( $ar1[$_] eq $ar2[$_] ) || 0 || last for ( 0 .. $#ar1 );

    -M

    Free your mind

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://556624]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 03:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found