← Back to Perl Course | Chapter 2: Variables | Lesson 7 of 7

Lists का परिचय

एक list values का एक comma-separated group है, और Perl इन्हें एक step में assign, swap, और unpack कर सकता है।

In this page:

  1. Lists

Lists

एक list एक array जैसी चीज़ नहीं है: एक array एक variable है, जबकि एक list values की एक temporary sequence है। आप एक साथ कई variables को एक list assign कर सकते हैं, एक list assignment से दो variables swap कर सकते हैं, और 1..5 से ranges बना सकते हैं। दूसरी lists के अंदर रखी गई lists एक लंबी list में flatten हो जाती हैं।

Note: एक list assignment में, extra values drop हो जाती हैं और missing values undef बन जाती हैं।

उदाहरण: Lists

perl
use strict;
use warnings;

my ($x, $y, $z) = (10, 20);
print "z is ", defined $z ? "defined" : "undef", "\n";
($x, $y) = ($y, $x);
print "swapped: x=$x y=$y\n";
my @small = (1, 2);
my @big = (0, @small, 3, (4, 5));
print "flattened: @big\n";
my @range = (1 .. 5);
print "range: @range\n";
my @letters = ('a' .. 'e');
print "letters: @letters\n";
my ($head, @rest) = @big;
print "head=$head rest=@rest\n";
print "backward range: ", scalar(() = (5 .. 1)), " elements\n";

# Output:
# z is undef
# swapped: x=20 y=10
# flattened: 0 1 2 3 4 5
# range: 1 2 3 4 5
# letters: a b c d e
# head=0 rest=1 2 3 4 5
# backward range: 0 elements
{# 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. एक list के अंदर arrays रखते वक्त यह उम्मीद करना कि nested lists nested रहेंगी
  2. 5..1 जैसी एक range इस्तेमाल करना और एक countdown की उम्मीद करना
  3. यह भूल जाना कि scalar context में एक comma list last element return करती है
चैप्टर सारांश
  • एक list values की एक temporary sequence है
  • List assignment values को variables में unpack करती है
  • Lists combine होने पर flatten हो जाती हैं
  • 1..5 एक range बनाता है
🔒

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.