Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Comparing a value to a list of numbers

by tybalt89 (Prior)
on Feb 01, 2021 at 02:32 UTC ( #11127725=note: print w/replies, xml ) Need Help??


in reply to Comparing a value to a list of numbers

And now for something completely different...

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11127703 use warnings; no warnings qw( qw ); use Search::Dict; @ARGV = qw( 1,2,5,6,9,10,41-56 1 4 42 17 ); my $file = join '', map "$_\n", split /,/, shift; open my $fh, '<', \$file or die; for my $n ( @ARGV ) { look $fh, $n, { comp => sub { $_[0] <=> $_[1] }, xfrm => sub { $_[0] =~ s/\d+-//r }, }; my $have = <$fh>; my $not = (defined $have and $have =~ /(\d+)-(\d+)/ ? $1 <= $n <= $2 : $have == $n) ? '' : ' no +t'; print "$n is$not in set\n"; }

Outputs:

1 is in set 4 is not in set 42 is in set 17 is not in set

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2021-06-13 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (54 votes). Check out past polls.

    Notices?