Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

how to print key and value of hash as it is

by lakshmikant (Initiate)
on Jun 03, 2016 at 07:42 UTC ( #1164837=perlquestion: print w/replies, xml ) Need Help??

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

I want to print key and value pair as it is which in hash how to do that

my %hash = ( 4 => 20, 2 => 40, 3 => 30, 1 => 50, 5 => 10, ); while{ my $a = ((sort values %hash)); # key based on the second highest value my $b = ((sort { $hash{$b} <=> $hash{$a} } keys %hash)); print $b = $a; }

Replies are listed 'Best First'.
Re: how to print key and value of hash as it is
by hippo (Chancellor) on Jun 03, 2016 at 08:14 UTC
    print $b = $a;

    It isn't immediately obvious to me what you expect this statement to do. What it actually does is to extract the value from $a, store it in $b and then print it. What did you intend?

    Other things you might be interested to know:

    • while takes a logical expression to evaluate and you have not given it one.
    • $a and $b are not good choices for variable names especially when using sort.
    • The ordering of keys within hashes is not a trivial thing.
    • You never need double brackets around one expression in Perl.

    It might be better all round for you to provide your expected output because it's not at all clear to me what your code is intended to do.

Re: how to print key and value of hash as it is
by kzwix (Sexton) on Jun 03, 2016 at 08:46 UTC

    Err... I'm not sure of what you're going for, with your code.

    However, if you wish to print the contents of your hash in the order of its keys, then:

    for my $key (sort keys %hash) {
        print "The key '$key' is associated to the value '$hash{$key}'\n";
    }

    If you wish to print the contents of your hash in the order of its CONTENTS (not of its keys), then yes, you need a custom sort method. The following code is merely an adaptation of an example found on the Perl "sort" documentation page:

    for my $key (sort { $hash{$b} <=> $hash{$a} } keys %hash) {
        print "The key '$key' is associated to the value '$hash{$key}'\n";
    }

    However, please consider performance when doing so: If you merely need to access it once, then the sort method is fine. If you need it ordered by value, and accessed a lot of times, then a reverse hash might be the solution. In this case, if you may have multiple instances of the same value, you might have collisions, and thus, will need to store references to lists containing the different values.

Re: how to print key and value of hash as it is
by haukex (Bishop) on Jun 03, 2016 at 08:20 UTC

    Hi lakshmikant,

    In addition to what the others have said, have a look at "Data: Hashes" in perlfaq4, especially "How do I process an entire hash?" and "How do I sort a hash (optionally by value instead of key)?".

    Hope this helps,
    -- Hauke D

Re: how to print key and value of hash as it is
by vinoth.ree (Monsignor) on Jun 03, 2016 at 07:48 UTC
    Hi

    Add,

    use strict;
    use warnings;

    then run your code ?

    "my" variable %hash masks earlier declaration in same scope at testt.pl line 14.
    syntax error at testt.pl line 12, near "while{"
    Can't use "my $a" in sort comparison at testt.pl line 17.
    
    Update:

    Hashes are not ordered, but as usual, CPAN offers a solution: Tie::IxHash


    All is well. I learn by answering your questions...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1164837]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2020-07-06 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?