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

comparsion of 2 arrays

by Madam (Sexton)
on Jun 23, 2005 at 09:03 UTC ( [id://469324]=perlquestion: print w/replies, xml ) Need Help??

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

how to compare 2 arrays at one shot? For e.g, i have arrays
@arr1 = ("book","pen","pencil"); @arr2 = ("pen","pencil",book",eraser"); @arr3 = ("pen","pencil","book");
i want to compare the 2 arrays without using hashes at one shot. desired output : Compare(@arr1,@arr2) = not matching. Compare(@arr1,@arr3) = matching

Replies are listed 'Best First'.
Re: comparsion of 2 arrays
by marto (Cardinal) on Jun 23, 2005 at 09:07 UTC
    Hi,

    You may want to take a look at List::Compare.

    Hope this helps

    Cheers

    Martin
Re: comparsion of 2 arrays
by davidrw (Prior) on Jun 23, 2005 at 11:23 UTC
Re: comparsion of 2 arrays
by lupey (Monk) on Jun 23, 2005 at 11:48 UTC
Re: comparsion of 2 arrays
by blazar (Canon) on Jun 23, 2005 at 09:52 UTC
    You may sort and then check if "@arr1" eq "@arr2", which is not bullet-proof, but could be suitable for your needs, especially if you set $" accordingly. This still involves more than one statement, well, unless you like a horrible hack like
    "@{[sort @arr1]}" eq "@{[sort @arr2]}";
    Otherwise, whatever method you really use behind the scenes, who prevents you from wrapping it in a Compare() sub?
Re: comparsion of 2 arrays
by rev_1318 (Chaplain) on Jun 23, 2005 at 09:53 UTC
    If your arrays contain text, you could use sort/join:
    print "MATCH" if join("",sort(@arr1)) eq join("",sort(@arr3))

    Paul

Re: comparsion of 2 arrays
by tphyahoo (Vicar) on Jun 23, 2005 at 10:02 UTC
    You might want to check out String Comparison by Madam, a recent discussion on basically the same theme. In resolving all these types of problems, the best approaches seem to involve morphing from string to array and back with sort/join. You asked a question about arrays, Madam asked a question about strings, but it's the same problem.

    This also mentions some issues you need to think about, eg, how do you treat duplicate elements in the list; how do you treat whitespace; etc.

    Reasonablekeith's solution, towards the bottom, seemed the best to me. Short and to the point.

    You could also have a look at Array::Compare from CPAN, which I fished using a search engine. Good luck.

      "You asked a question about arrays, Madam asked a question about strings, but it's the same problem."

      Check out the name of the user who posted this question :)

      Cheers

      Martin
        Hehhh? Pardons to madam but... well, it is sort of the same problem, isn't it? Well, since my gut told me these issues were linked, I'm going back to that earlier thread and linking back here, at least then everything's tied together for future monks stolpering around here.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-25 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found