As always, there is more than one way to do it. Please see code below for some illustrations. Some comments:

Hope this helps.

use strict; use warnings; use Math::Round qw/round/; use Data::Dumper; # function returns 1 if all arguments are integer # zero otherwise sub is_approximately_an_integer { my $eps = 0.0001; while( my $x = shift ) { # need to use "round", "int" does not work! return 0 if abs( $x-round($x) ) > $eps; } return 1 } # some test cases print "Testing...\n"; print is_approximately_an_integer( 1, 3.99999999, 5 ), "\n"; print is_approximately_an_integer( 1.2, 3.99999999, 5 ), "\n"; print is_approximately_an_integer( 1, 3.99999999, 5.0000001 ), "\n"; print is_approximately_an_integer( 1, 3.99999999, -5 ), "\n"; print is_approximately_an_integer( 1, 3.99999999, -5.1 ), "\n"; print is_approximately_an_integer( 1, 3.99999999, -5.000000001 ), "\n" +; print is_approximately_an_integer( 1, -3.99999999, -5.000000001 ), "\n +"; print is_approximately_an_integer( 1 ), "\n"; print is_approximately_an_integer( 1.5 ), "\n"; # what if we have hash of arrays ? my %hash = ( "1,2,3" => [ 1, 3.99999999, 5 ], "1,2,4" => [ 1.2, 3.99999999, 5 ], "1,2,5" => [ 1, 3.99999999, 5.0000001 ], # etc ); # this returns only the arrays but lose keys print "\nSelected arrays\n"; my @wants = grep { is_approximately_an_integer( @$_ ) } values %hash; print Dumper( @wants ); # this returns the keys print "\nSelected keys...\n"; my @wants = grep { is_approximately_an_integer( @{$hash{$_}} ) } keys +%hash; print Dumper( @wants ); print "...and the associated array.\n"; # and you can get the arrays as well print Dumper( @hash{@wants} );

In reply to Re^5: question about what to grep(?) by hdb
in thread question about what to grep(?) by crunch_this!

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.