← Back to Perl Course | Chapter 6: References | Lesson 3 of 7

Hash References का परिचय

एक hash reference named values का एक group साथ ले जाने का एक handy तरीका है।

In this page:

  1. Hash References

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

perl
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
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. anonymous hash braces को एक code block से confuse करना
  2. $ref->{key} की बजाय %ref->{key} लिखना
  3. यह भूल जाना कि 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.