Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Is hash evaluated with in double quotes

by anbutechie (Sexton)
on Mar 05, 2009 at 12:03 UTC ( [id://748500]=perlquestion: print w/replies, xml ) Need Help??

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

Hi
Lets take
%hash =('k1'=>'v1''k2'=>'v2','k3'=>'v3'); print "Key value pair in hash: %hash";

Actual result for the above code is: Key value pair in hash: %hash
Is hash not evaluated within double quotes. If yes, is there any way to display content of hash.
Regards,
Anbarasu

Replies are listed 'Best First'.
Re: Is hash evaluated with in double quotes
by almut (Canon) on Mar 05, 2009 at 12:17 UTC

    No, it isn't. But if you want to just quick-n-dirty interpolate, you could do something like

    print "Key value pair in hash: @{[%hash]}";

    which evaluates the hash in list context, and prints its entries in unsorted order:

    Key value pair in hash: k2 v2 k1 v1 k3 v3
      Can you de-construct this "@{[%hash]}" please.

      @{ } interpret as an array [ ] ??? %hash The hash
      Is that the Anon Array constructor? What is its function here?


      It is always better to have seen your target for yourself, rather than depend upon someone else's description.

        [%hSomeHash] creates an array reference populated by all of the members of %hSomeHash. @{} says extract the array from the array reference found between {...}.

        Best, beth

      Fentastic its working
      but why the order has changed.
      Thank you
      Anbarasu
        but why the order has changed

        A hash is unordered. See Tie::Hash::Sorted for keeping hashes sorted.

        -- 
        Ronald Fischer <ynnor@mm.st>
Re: Is hash evaluated with in double quotes
by ww (Archbishop) on Mar 05, 2009 at 12:15 UTC

    Aside from the typo (missing comma after the first key=>value declaration) the rest of your question would have been answered by using perldoc -q hash, whence came this answer:

    #!/usr/bin/perl use strict; use warnings; # 748500 my %hash =('k1'=>'v1','k2'=>'v2','k3'=>'v3'); while ( (my $key, my $value) = each %hash) { print "$key = $value\n"; }

    which prints:

    k2 = v2 k1 = v1 k3 = v3

    Updates:

    • s/command/comma/ in typo description.
    • If you want to restore the order, use sort; if you need to retain the order see Tie::IxHash

Re: Is hash evaluated with in double quotes
by rovf (Priest) on Mar 05, 2009 at 13:58 UTC
    is there any way to display content of hash

    Data::Dumper

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: Is hash evaluated with in double quotes
by velusamy (Monk) on Mar 05, 2009 at 12:11 UTC
    Hi,

    You can't get hash values directly.
    You can use foreach to get the hash values.

    print $_,":",$hash{$_},"\n" foreach ( keys %hash);
Re: Is hash evaluated with in double quotes
by sundialsvc4 (Abbot) on Mar 05, 2009 at 15:52 UTC

    Someone else beat me to it... Data::Dumper.

    Perl does not take it upon itself to “know how to” print everything. It has a certain built-in way to “stringify” various things, and you've already seen what it does by-default for a hashref.

    For a situation like yours, several good possibilities have already been suggested. Take your pick.

    I think that this is also a good place to say, use CPAN, frequently and aggressively. Anytime you suspect that “someone must have already encountered and tackled this problem,” rest assured that they have, and you'll find it (maybe dozens of flavors of it...) on CPAN. Get in the habit of looking there first.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-26 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found