Hash References का परिचय
एक hash reference named values का एक group साथ ले जाने का एक handy तरीका है।
In this page:
Hash References
एक \%hash से या anonymous constructor { key => value } से एक बनाएँ। $ref->{key} से एक value पाएँ, और keys %$ref से सारी keys पाएँ। Hash references records represent करने का सामान्य तरीका हैं, और इन्हें एक single argument की तरह functions को pass किया जा सकता है।
Note:
एक key बिना बनाए test करने के लिए arrow के साथ exists function इस्तेमाल करें, जैसे exists $ref->{key}।
उदाहरण: Hash references
use strict;
use warnings;
my %config = (host => "localhost", port => 8080);
my $href = \%config;
$href->{debug} = 1;
print "host: $href->{host}\n";
print "original sees debug: $config{debug}\n";
my $person = { name => "Grace", langs => 2 };
for my $key (sort keys %$person) {
print "$key = $person->{$key}\n";
}
print "has email? ", (exists $person->{email} ? "yes" : "no"), "\n";
my @copy_keys = sort keys %{$href};
print "keys: @copy_keys\n";
# Output:
# host: localhost
# original sees debug: 1
# langs = 2
# name = Grace
# has email? no
# keys: debug host port
Login to try C/C++/Java/PHP code in the editor
आम गलतियां
- anonymous hash braces को एक code block से confuse करना
- $ref->{key} की बजाय %ref->{key} लिखना
- यह भूल जाना कि keys इस्तेमाल करने के लिए एक hash reference dereference होना ज़रूरी है
चैप्टर सारांश
- { ... } एक anonymous hash reference बनाता है
- $ref->{key} एक value पढ़ता है
- keys %$ref keys list करता है
- References अच्छे records बनाते हैं
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: