'dfsd','b'=>'fd...">

Perl hash printing

I want to know the following code, why print "2/8".

#!/usr/bin/perl
#use strict;
#use warnings;
%a = ('a'=>'dfsd','b'=>'fdsfds');
print %a."\n";
+5
source share
2 answers

You print the hash in a scalar context , combining it with the string '\ n'

If you evaluate the hash in a scalar context, it returns false if the hash is empty. If there are key / value pairs, it returns true; More precisely, the return value is a string consisting of the number of buckets used and the number of buckets allocated, separated by a slash.

2/8means that 8 , 2 is highlighted . Given that you entered only 2, this is good so far :)

, , , , -. print %a; .

+9

@Dark.. .

, "Data:: Dumper"

use Data::Dumper;
...
...
print Dumper(%a);

:

use Data::Dumper; 
my %hash = ( key1 => 'value1', key2 => 'value2' ); 
print Dumper(%hash); # okay, but not great 
print "or\n"; 
print Dumper(\%hash); # much better

:

$VAR1 = 'key2'; 
$VAR2 = 'value2'; 
$VAR3 = 'key1'; 
$VAR4 = 'value1'; 
or 
$VAR1 =    { 
             'key2' => 'value2', 
             'key1' => 'value1' 
           };
+5

All Articles