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

Nested Data Structures का परिचय

References आपको real data model करने के लिए hashes के अंदर arrays और arrays के अंदर hashes रखने देते हैं।

In this page:

  1. Nested Data Structures

Nested Data Structures

क्योंकि array और hash elements scalars हैं, वे दूसरी arrays और hashes के references hold कर सकते हैं। Perl आपको subscripts के बीच arrows omit करने देता है, इसलिए $data->{users}[0]{name} structure में गहराई तक पहुँचता है। एक deep path को assign करना intermediate levels automatically बनाता है, जिसे autovivification कहते हैं।

Note: एक deep path पर exists इससे ऊपर के parts autovivify कर देगा, इसलिए अगर यह matter करता है तो हर level test करें।

उदाहरण: Nested data structures

perl
use strict;
use warnings;

my %school = (
    name => "Perl High",
    classes => [
        { subject => "Math",    students => ["Ann", "Ben"] },
        { subject => "Science", students => ["Cy"] },
    ],
);
print "school: $school{name}\n";
print "first subject: $school{classes}[0]{subject}\n";
print "second student: $school{classes}->[0]->{students}->[1]\n";
for my $class (@{ $school{classes} }) {
    printf "%s has %d student(s): %s\n", $class->{subject}, scalar @{ $class->{students} }, join(", ", @{ $class->{students} });
}
$school{teachers}{math}{count} = 3;
print "teachers keys: ", join(",", sort keys %{ $school{teachers} }), "\n";

# Output:
# school: Perl High
# first subject: Math
# second student: Ben
# Math has 2 student(s): Ann, Ben
# Science has 1 student(s): Cy
# teachers keys: math
{# 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. यह भूल जाना कि हर level एक reference है और इसका अपना subscript चाहिए
  2. एक nested key पढ़ते वक्त parent levels बनने पर surprised होना
  3. एक inner list के लिए brackets की बजाय parentheses इस्तेमाल करना
चैप्टर सारांश
  • Elements references hold कर सकते हैं
  • Subscripts के बीच Arrows optional हैं
  • Deep assignment levels autovivify करता है
  • Inner structures के लिए [ ] और { } इस्तेमाल करें
🔒

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.