Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Perl already knows how to do all the work for you. You can just use a hash in almost the same way you would assure uniqueness in a list.:

sub isequal { return 0 if ( grep{ !defined($_) } @_ ) != scalar @_; no warnings qw/uninitialized/; my %gadget; @gadget{ @_ } = (); return scalar keys %gadget == 1 ? 1 : 0; }

This works by creating a hash whos keys are the items being compared. If after creating the keys you have one element, you've got equality. If you have more than one element, you have inequality. The only tricky part is to be sure to silence the warning you get for making a hash key out of 'undef', and to deal with the fact that undef stringifies to ''.

Here is a test example:

use strict; use warnings; use diagnostics; sub isequal { return 0 if ( grep { ! defined( $_ ) } @_ ) != scalar @_; no warnings qw/uninitialized/; my %gadget; @gadget{ @_ } = (); return scalar keys %gadget == 1 ? 1 : 0; } my @test = ( [ 0, 1 ], [ "1", "one" ], [ 0, "0" ], [ 0, undef ], [ 1, undef ], [ "hello", undef ], [ undef, undef ], [ 1, 1 ], [ "1", 1 ], [ "hello", "hello" ] ); foreach my $items ( @test ) { my( $left, $right ) = @{ $items }; my $comp = isequal( $left, $right ); foreach( $left, $right ) { ! defined $_ and $_ = "undef"; } print "Testing $left, $right == ", $comp ? "equal\n" : "not equal\ +n"; }

Update: Another advantage to this method that may not be immediately apparent is that it can be used to test a list of any number of items.

Enjoy!


Dave


In reply to Re: check if 2 values are equal by davido
in thread check if 2 values are equal by eXile

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found