Gossamer Forum
Home : General : Perl Programming :

hash help

Quote Reply
hash help
hi peoples,

Lets say i had the following hash:

%test = (
1 => belgium
5 => brazil
8 => france
)

Is there a way to sort those elements and grab the value(in this case country) of the lowest numeral key ? Basically , since 1 is the lowest number, i want it's value , which is belgium. I've been able to sort them, but i have no idea how to sort, then grab just the value. Thanks
Quote Reply
Re: [mtorres] hash help In reply to
%test = (
1 => belgium ,
5 => brazil ,
8 => france ,
) ;


foreach(sort keys %test){

print $_ , $test{$_};

}

Cheers,

Cheers,

Dat

Programming and creating plugins and templates
Blog
Quote Reply
Re: [mtorres] hash help In reply to
Code:
my %test = (
65 => belgium,
15 => brazil,
78 => france,
);

my $city = $test{(sort {$a <=> $b} keys %test)[0]};

print "The city is $city\n";

Outputs:
The city is brazil

Cool

--mark
Quote Reply
Re: [tandat] hash help In reply to
That prints each one, not just the element with the lowest key number.
Quote Reply
Re: [Mark Badolato] hash help In reply to
Sorry, I missed this point

Cheers,

Cheers,

Dat

Programming and creating plugins and templates
Blog
Quote Reply
Re: [tandat] hash help In reply to
Thanks guys. both of your examples are great. you guys rock.



- marcus