Jim Gibson wrote:
On 2/27/09 Fri Feb 27, 2009 8:33 AM, "Rick" <rich.j...@gmail.com>
scribbled:
Jim Gibson wrote:
On 2/26/09 Thu Feb 26, 2009 3:53 PM, "John W. Krahn" <jwkr...@shaw.ca>
scribbled:
I get "Use of uninitialized value in numeric gt (>) at ..." for that, since
@max is undefined the first time through the loop. Here is a version using
each that doesn't produce that error. It uses the fact that each saves its
state from one call to the next, even if the calls are not on the same line:
#!/usr/bin/perl
use strict;
use warnings;
my %files = ( one => 1, thirty => 30, four => 4, never => 997,
forever => 100001, five => 5 );
my @max = each(%files);
while( my @next = each(%files) ) {
@max = @next if $next[1] > $max[1];
}
print "biggest: (@max)\n";
__OUTPUT__
biggest: (forever 100001)
Above solution works well..
one more question, how come below does not work the way I expected?
use strict;
use warnings;
my %files = ( one => 1, thirty => 30, four => 4, never => 997,
forever => 100001, five => 5 );
my (@next,@max);
@next = @max = each(%files);
for ( @next ) {
@max = @next if $next[1] > $max[1];
}
print "biggest: (@max)\n";
__OUTPUT__
biggest: (five 5)
I would have thought it is the samething.. no?
No, it is not. The way to diagnosis this type of problem is to look at the
contents of your variables, either by stepping through your program with a
debugger or by adding print statements to your program. If you do that, you
will find that the @next array is not assigned the key/value pairs of the
hash in the loop. Hint: you are missing a call to each.
now I understand, you were just assigning the one pair to @max for
default value since you were saying w/ strict it would not pass.
Now I understand
my @max = each(%files);
One odd thing however is that I thought hash keeps keys in random order
but whenever i do
my @max = each(%files);
print Dumper(@max);
I always get --> five 5... why? shoudln't result be different ??
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/