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.
Using the warnings pragma is especially important when writing tests AFAIC. > #!/usr/bin/perl > use strict; > to > #!/usr/bin/perl -w > use strict; # Notice I'm using strict too! Unless you are using a version of perl that is < 5.6, you should replace '-w' with 'use warnings;'. > 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 On my machine, when I explicitly set some variables, it works. You may want to use Data::Dumper to print out some of your vars to ensure they are set to what you think they are: #!/usr/bin/perl use warnings; use strict; my $bytes = 30; my %sizes; my $ext = 'this'; $sizes{$ext}->{largest} = 320; $sizes{$ext}->{largest_size} = 340; 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"; } __END__ Prints: 30 is smaller than 340 I notice that you are comparing $bytes to $sizes{$ext}->{largest} or 0, then printing '$bytes is larger than... $sizes->{ext}->{largest_size}. You are then promptly setting largest size in your result. So, the integer after the "is larger than" in your output is NOT what you are comparing $bytes to in the first place. Put a "print Dumper \%sizes" just before the if statement, so you can get an idea of what exactly 'largest' and 'largest_size' are set to on each pass. For instance, if $ext is something different on each pass through the loop, then 'largest' will also be different than what you may be expecting. Steve
smime.p7s
Description: S/MIME Cryptographic Signature