Same problem with your example and with uniq() - string vs numeric comparison. Can be worked around by numifying data:
use strict;
use warnings;
use Test::More;
sub all_different {
my %seen;
for ( @_ ) { return 0 if $seen{$_}++; } #-- duplicate found
return 1;
}
# e.g. if ( all_different($RB1, $RB2, $WR1, $WR2, $TE1) ) { ...
is( all_different( 1,2,3,4,5 ), 1 , "all different");
is( all_different( 1,2,3,"05",5 ), 0 , "some duplicates with different
+ string");
is( all_different( map { $_+0 } 1,2,3,"05",5 ), 0 , "some duplicates w
+ith different strings numified");
is( all_different( 1,1,3,4,5 ), 0 , "some duplicates");
is( all_different(), 1 , "empty paramlist");
done_testing;
result:
ok 1 - all different
not ok 2 - some duplicates with different string
# Failed test 'some duplicates with different string'
# at 3.pl line 17.
# got: '1'
# expected: '0'
ok 3 - some duplicates with different strings numified
ok 4 - some duplicates
ok 5 - empty paramlist
1..5
-
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.