Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Multiple Conditional Statements

by Perlbotics (Archbishop)
on Sep 11, 2013 at 11:19 UTC ( [id://1053466]=note: print w/replies, xml ) Need Help??


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
    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
Re^2: Multiple Conditional Statements
by vsespb (Chaplain) on Sep 11, 2013 at 11:55 UTC
    the OP didn't specify if e.g. 05.0 is identical to 5 or not - probably yes
    Well, OP specified "!=" operator in original post.
    However, as shown below, sanitizing input
    In general case, programmer cannot control if his data is numified or no. So I don't think it's really about "sanitizing".
    floats might need normalisation using sprintf...
    In general case, normalization for floats won't work well, example:
    perl -e 'my ($a, $b) = (0.45000001, 0.4499999); print sprintf ("%0.1f +%0.1f %f", $a, $b, $a-$b )'
    prints 0.5, 0.4 and 0.000000

    My example Re: Multiple Conditional Statements can be fixed to work with floats (but I admit it's slow).
    UPD: fixed float example

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1053466]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 03:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found