On Sep 24, 2009, at 14:31, pa...@compugenic.com wrote:
On Thu, Sep 24, 2009 at 02:16:05PM -0400, Uri Guttman wrote:
"p" == pablo <pa...@compugenic.com> writes:
p> I have a script that tells me the largest file (in bytes) for
each
p> extension. I store the largest size in a hash, keyed by the
extension.
p> The numeric comparison portion is clearly not working, since it
is returning true every time regardless of the numbers being
compared. Here's a snippet, along with the output shown:
p> Snippet:
p> if ( $bytes > $sizes{$ext}->{largest} ) {
you use 'largest' there. assuming that isn't ever set, this compares
bytes to undef. this would have been detected if you had enabled
warnings. this is why we always say to make your code warnings
clean. there is a reason for this!
p> print "$bytes is larger than $sizes{$ext}->{largest_size}\n";
p> $sizes{$ext}->{largest_size} = $bytes;
and 'largest_size' in those two lines.
uri
That's good advice which I always follow. I always use warnings and
strict, unless testing something. I temporarily disabled it to make
writing the testing block a bit easier.
But, to demonstrate that having warnings disabled is not causing the
erroneous numeric comparison problem, I enabled warning by changing
#!/usr/bin/perl
use strict;
to
#!/usr/bin/perl -w
use strict; # Notice I'm using strict too!
Then I tweaked my code as follows:
if ( $bytes > ($sizes{$ext}->{largest}||0) ) {
print "$bytes is larger than ", $sizes{$ext}->{largest_size} ||
0, "\n";
$sizes{$ext}->{largest_size} = $bytes;
}
else {
print "$bytes is smaller than $sizes{$ext}->{largest_size}\n";
}
The output was:
304785408 is larger than 0
407666688 is larger than 304785408
595656704 is larger than 407666688
597778432 is larger than 595656704
568844288 is larger than 597778432
157255680 is larger than 568844288
512342016 is larger than 157255680
616058880 is larger than 512342016
728705024 is larger than 616058880
6008832 is larger than 728705024
Now what?
Pablo
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/
You still need to address the fact that you are comparing the value
associated with the "largest" key but printing the value associated
with the "largest_size" key.
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/