http://www.perlmonks.org?node_id=989800


in reply to Identical Arrays

Easy!
use Modern::Perl; my @one = ( 1, 2, 3 ); my @two = ( 2, 3, 1 ); my @three = ( 1, 2, 4 ); say identical( \@one, \@two ); # returns 1 say identical( \@one, \@three ); # returns 0 sub identical { my ( $first, $second ) = @_; return ((join chr(0), sort @$first) eq (join chr(0), sort @$second +)) ? 1 : 0; }
A few comments:

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^2: Identical Arrays
by philiprbrenan (Monk) on Aug 26, 2012 at 13:26 UTC

    Not so easy!

    my @one = (chr(1), chr(1), chr(1)); my @two = (chr(1).chr(0).chr(1), chr(1)); say identical(@one, @two); # returns 1
      I know. This is one of the degenerate cases where a specially crafted string will break this simple subroutine.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics