in reply to Comparing sorted arrays

If all you need to know is that the two arrays are the same or not, you can use the fairly quick and dirty trick of stringifying them.

my @a1 = (...); my @a2 = (...); if ( @a1 == @a2 and do{local $"="\cA"; "@a1"} eq do{local $"="\cA"; "@ +a2"} ) { # They're the same }

The caveat is that you must choose a delimiter ("\cA" above) that does not appear in your data. Cntrl-A is a fairly safe bet for either text or numeric data, but if your data could include binary data, there is a small chance of a false positive. </code>


Examine what is said, not who speaks.

The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Replies are listed 'Best First'.
Re: Re: Comparing sorted arrays
by rir (Vicar) on Jan 16, 2003 at 20:43 UTC
    I like your stringification idea. The value of $" doesn't matter though.

    Update: Struck out stupid comment, unfortunately that is not the same as striking out stupidity. I was wrong.

        You are quite right.

        There are also the usual quibles to be made with regard to the definition of equality. (Refs, Strings, Numbers).

        I'll usually pass on worrying that stringification is overloaded to return pseudo-random strings.

        The "" or undef is a bad choice for a separator as it is always included in your data.