in reply to Multiple Conditional Statements
You should introduce a test function instead of listing all combinations in the if clause. I read your condition as no duplicates allowed, so this should work:
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,1,3,4,5 ), 0 , "some duplicates"); is( all_different(), 1 , "empty paramlist"); done_testing;
Perhaps you can also use uniq() from List::MoreUtils?
Update: In response to vsespb's comment below - the OP didn't specify if e.g. 05.0 is identical to 5 or not - probably yes, so numification is advised. However, as shown below, sanitizing input or creating a specialised all_different_nums() sub can be done by the OP if necessary. Only the OP knows, what is equal in his/her given context. I.e., floats might need normalisation using sprintf... we don't know (yet).
Maybe this one is more robust?
use strict; use warnings; use Scalar::Util qw(looks_like_number); use Test::More; sub all_different { my %seen; for ( @_ ) { my $norm_val = looks_like_number( $_ ) ? 0+$_ : $_; return 0 if $seen{$norm_val}++; #-- 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 (num)"); is( all_different( 1,1,3,4,5 ), 0 , "some duplicates (num)"); is( all_different( qw(a b 8) ), 1 , "all different (str)"); is( all_different( qw(a a 8) ), 0 , "some duplicates (str)"); is( all_different( qw(5 05.0 8) ), 0 , "some duplicates (mixed)"); is( all_different(), 1 , "empty paramlist"); done_testing;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Multiple Conditional Statements
by vsespb (Chaplain) on Sep 11, 2013 at 11:25 UTC | |
|
Re^2: Multiple Conditional Statements
by vsespb (Chaplain) on Sep 11, 2013 at 11:55 UTC |