Loading ...
Sorry, an error occurred while loading the content.

[PBML] Re: List comparison

Expand Messages
  • Jeff Boes
    ... Not really, but it s pretty easy to make one: sub compare_lists { my($rA, $rB) = @_; # Note that you have to call this with *references* to the actual
    Message 1 of 2 , Mar 14, 2000
    • 0 Attachment
      > -----Original Message-----
      > From: root [mailto:root]On Behalf Of Sundar Dinakaran
      > Sent: Tuesday, March 14, 2000 12:22 PM
      > To: List: Perl Beginners
      > Subject: [PBML] List comparison
      >
      >
      > Hi,
      > Is there any function or operators to compare two lists ?

      Not really, but it's pretty easy to make one:

      sub compare_lists {
      my($rA, $rB) = @_;
      # Note that you have to call this with *references* to the actual lists,
      # because Perl's syntax collapses argument lists into one list.
      # E.g., "&mysub( (1,2), (3,4) )" is the same as "&mysub( (1,2,3,4) )".

      # Lists are not equal unless same size
      return 0 unless scalar(@$rA) == scalar(@$rB);

      # Now compare item by item.
      my $i;
      for ($i = 0; $i < scalar(@$rA); $i++) {
      return 0 unless $rA->[$i] eq $rB->[$i];
      }
      return 1;
      }

      print &compare_lists( [1,2], [1,2,3] ), "\n";
      print &compare_lists( [1,2,3], [1,2,3] ), "\n";
      $a = 1; $b = 2;
      print &compare_lists( [$a,$b], ['1','2'] ), "\n";
      @a = (1,2,4);
      @b = qw(1 2 4);
      print &compare_lists( \@a, \@b ), "\n";

      ----
      "To paraphrase Mark Twain, the difference between the right program and
      almost the right program is like the difference between lightning and a
      lightning bug. The difference is just a bug."
      --Danny Hillis, "The Pattern on the Stone" (1998)
      ___________
      Jeff Boes <>< jboes@...
      Mur Consulting http://www.qtm.net/~jboes/
    Your message has been successfully submitted and would be delivered to recipients shortly.