http://www.perlmonks.org?node_id=812926

BhariD has asked for the wisdom of the Perl Monks concerning the following question:

I have the following code:

while($evalue = <DATA>){ chomp; if($evalue < 0.001){ print OUTPUT $evalue, "\n"; } } close DATA;

DATA ----------- 1e-005 2e-090 0.00 0.67 1.0 4e-065

For some reason the above code does not seem to do what I want. It prints out the input in the DATA as it is without evaluating the if statement above.

OUTPUT (wrong) ----------- 1e-005 2e-090 0.00 0.67 1.0 4e-065

The output should look like this: (the evalue of 0.00 means 1e-180, so should be counted).

OUTPUT (correct) ----------- 1e-005 2e-090 0.00 4e-065

Anyone knows what I am doing wrong here?

Replies are listed 'Best First'.
Re: how to evaluate exponential notations and floating points
by kennethk (Abbot) on Dec 15, 2009 at 20:15 UTC
    I cannot replicate your problem with the posted code. The following code, taken almost entirely from your OP, works for me:

    #!/usr/bin/perl use strict; use warnings; while(my $evalue = <DATA>){ chomp $evalue; if($evalue < 0.001){ print $evalue, "\n"; } } __DATA__ 1e-005 2e-090 0.00 0.67 1.0 4e-065

    The main changes are the file handles. Note that your chomp was not given a variable, and hence was operating on $_ instead of $evalue.

Re: how to evaluate exponential notations and floating points
by ikegami (Patriarch) on Dec 15, 2009 at 20:19 UTC

    Anyone knows what I am doing wrong here?

    Asking questions about one piece of code while posting another.

    It doesn't produce the results you describe, and you would have noticed you're chomping the wrong variable.

    while($evalue = <DATA>){ chomp; # Should be chomp( $evalue ); if($evalue < 0.001){ print $evalue, "\n"; } } close DATA; __DATA__ 1e-005 2e-090 0.00 0.67 1.0 4e-065
    1e-005 2e-090 0.00 4e-065
Re: how to evaluate exponential notations and floating points
by GrandFather (Saint) on Dec 15, 2009 at 22:44 UTC

    Always use strictures (use strict; use warnings; - see The strictures, according to Seuss).

    Providing a stand alone script that reproduces the error helps a lot too. In this case something like the following (although this sample doesn't demonstrate your issue):

    use strict; use warnings; while (my $evalue = <DATA>) { chomp $evalue; if ($evalue < 0.001) { print $evalue, "\n"; } } __DATA__ 1e-005 2e-090 0.00 0.67 1.0 4e-065

    True laziness is hard work
Re: how to evaluate exponential notations and floating points
by bichonfrise74 (Vicar) on Dec 15, 2009 at 20:15 UTC
    Take a look at this.
    #!/usr/bin/perl use strict; while( my $evalue = <DATA> ){ chomp $evalue; if($evalue < 0.001){ print "$evalue\n"; } } __DATA__ 1e-005 2e-090 0.00 0.67 1.0 4e-065