Nested Data Structures का परिचय
References आपको real data model करने के लिए hashes के अंदर arrays और arrays के अंदर hashes रखने देते हैं।
In this page:
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
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
Login to try C/C++/Java/PHP code in the editor
आम गलतियां
- यह भूल जाना कि हर level एक reference है और इसका अपना subscript चाहिए
- एक nested key पढ़ते वक्त parent levels बनने पर surprised होना
- एक 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: