Desidero sapere il seguente codice perché stampare "2/8".Stampa hash in Perl
#!/usr/bin/perl
#use strict;
#use warnings;
%a = ('a'=>'dfsd','b'=>'fdsfds');
print %a."\n";
Desidero sapere il seguente codice perché stampare "2/8".Stampa hash in Perl
#!/usr/bin/perl
#use strict;
#use warnings;
%a = ('a'=>'dfsd','b'=>'fdsfds');
print %a."\n";
si sta stampando un hash in scalar context concatenando con una stringa '\ n'
Se si valuta un hash in contesto scalare, restituisce false se l'hash è vuoto. Se ci sono coppie chiave/valore, restituisce true; altro con precisione, il valore restituito è una stringa costituita dal numero di bucket utilizzati e dal numero di bucket allocati, separati da una barra .
2/8
significa che i allocati, sono stati toccati. Considerando che hai inserito solo valori 2, sta andando bene finora :)
Il valore è ovviamente inutile, tranne per valutare quanto bene sta facendo la funzione hash. Utilizzare print %a;
per stampare il suo contenuto.
Oppure 'print% a," \ n ";'. –
@DarkCthulhu: Voglio sapere se i bucket sono correlati con il sistema configure? – baozailove
@baozailove I bucket e la distribuzione ottenuta dipendono dalla funzione hash in uso. Puoi leggere a riguardo in dettaglio [qui] (http://www.perl.com/pub/2002/10/01/hashes.html). Dovrebbe essere * indipendente * dal sistema sottostante. –
Come indicato da @Dark .. si stampa un hash in contesto scalare.
se davvero si vuole stampare un hash, quindi utilizzare Data :: Dumper
use Data::Dumper;
...
...
print Dumper(%a);
per esempio:
use Data::Dumper;
my %hash = (key1 => 'value1', key2 => 'value2');
print Dumper(%hash); # okay, but not great
print "or\n";
print Dumper(\%hash); # much better
E l'output:
$VAR1 = 'key2';
$VAR2 = 'value2';
$VAR3 = 'key1';
$VAR4 = 'value1';
or
$VAR1 = {
'key2' => 'value2',
'key1' => 'value1'
};
a sarathi: Grazie mille! – baozailove
Perché sono rigorosi e avvertimenti commentati? – squiguy
@squiguy: Il codice è testato ~ – baozailove
Si dovrebbe * sempre * avviare ogni programma con 'use strict' e' use warnings' se è in sviluppo, test o produzione. Nonostante quello che dici, il tuo programma non può essere stato testato con 'use strict' sul posto in quanto non sarebbe stato compilato. – Borodin