[Perl] printing a hash

Ido Trivizki ido at hotmail.com
Mon Jun 24 11:41:36 PDT 2002


">print doesn't put any whitespace or other separators between items it
> prints" - Assuming you didn't set $,. Setting it will put it between the
args: print LIST; is like print join $,,LIST;

BTW, If you do want to print your hash in a double-quoted string use one of
the following:
1. print "@{[%hash]}";
Which will make an anon-arrayref from the list of key-value pairs %hash in
lists context returns, and deref it. The result will be the list joined by
$". Something like print join $",%hash;
2. print "@hash{keys %hash}";
Values only, again joined by $".

Ido.



----- Original Message -----
From: "Reuven M. Lerner" <reuven at lerner.co.il>
To: <perl at perl.org.il>
Sent: Monday, June 24, 2002 2:58 PM
Subject: [Perl] printing a hash


> >>>>> "Offer" == Offer Kaye <oferk at oren.co.il> writes:
>
>     Offer> print "%arr_hash1\n";
>     Offer> print %arr_hash1, "\n";
>
>     Offer> i.e. the second "print" worked as I expected, the first did
>     Offer> not. How come?
>
> In the first case, the hash was inside of double quotes.  Scalars and
> arrays are interpolated into double quotes, but hashes aren't.  So
> Perl prints the hash name as you entered it.
>
> In the second case, you put the hash outside of double quotes.  This
> is one of those cases when the boundary between hashes and lists is a
> bit thin: Perl turns the hash into a list (since perl handles its
> arguments in list context), and then prints the arguments.    The
> hash is turned into a list, sort of like the list you use to
> initialize a hash:
>
>     my %arr_hash1 = ('1' , 'do' , '2' , 're' , '3' , 'me' ,
>      '4' , 'fa' , '5' , 'so' , '6' , 'la' ,
>      '7' , 'se' , '8' , 'do');
>
> Or somewhat more readably:
>
>     my %arr_hash1 = ('1' => 'do' , '2' => 're' , '3' => 'me' ,
>      '4' => 'fa' , '5' => 'so' , '6' => 'la' ,
>      '7' => 'se' , '8' => 'do');
>
> print doesn't put any whitespace or other separators between items it
> prints, so you get all of the keys and values from your hash mushed
> together.  In other words, you basically did the same thing as this:
>
>     print 1, 2, 3, "\n";
>
> Which of course produces the output:
>
>     123
>
> Remember that you can't reasonably expect to know the order of pairs
> in a hash.  But you *can* be sure that keys will be at even indexes
> (0, 2, 4, etc.) and values will be at odd indexes (1, 3, 5, etc.), and
> that key-value pairs will be together.
>
> If you really want to display a hash for debugging purposes, there are
> lots of ways to do it. I generally do something like:
>
>     foreach my $Key (sort keys %hash)
>     {
> print "'$key' => '$hash{$key}'\n";
>     }
>
> I always put a delimiter around the key and value (single quotes, in
> this case0 so that I don't get confused if the variable is empty,
> contains whitespace, or is otherwise hard to understand.
>
> Reuven
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://www.perl.org.il/cgi/listinfo/perl
>



More information about the Perl mailing list