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

Arrays का परिचय

एक array scalars की एक ordered list है जिस तक आप position से पहुँचते हैं।

In this page:

  1. Arrays

Arrays

Array names @ से शुरू होते हैं और आप इन्हें my से declare करते हैं। Elements एक $ और square brackets से access होते हैं, इसलिए $fruits[0] पहला element है, और negative indexes अंत से count करते हैं। Special expression $#fruits last index है, और array को scalar context में इस्तेमाल करना इसकी length देता है।

Note: बिना quotes या commas के simple words की एक list लिखने के लिए qw(a b c) इस्तेमाल करें।

उदाहरण: Arrays

perl
use strict;
use warnings;

my @fruits = qw(apple banana cherry);
print "first: $fruits[0]\n";
print "last: $fruits[-1]\n";
print "last index: $#fruits\n";
print "length: ", scalar(@fruits), "\n";
$fruits[3] = "date";
print "all: @fruits\n";
for my $f (@fruits) {
    print "- $f\n";
}

# Output:
# first: apple
# last: cherry
# last index: 2
# length: 3
# all: apple banana cherry date
# - apple
# - banana
# - cherry
# - date
{# 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. @fruits[0] इस्तेमाल करना जब आपका मतलब scalar element $fruits[0] से है
  2. $#array (last index) को elements की संख्या से confuse करना
  3. यह उम्मीद करना कि पहला element index 1 पर है
चैप्टर सारांश
  • Arrays @ से शुरू होते हैं
  • Elements $array[index] इस्तेमाल करते हैं
  • Indexes 0 से शुरू होते हैं और -1 last element है
  • $#array last index है और scalar(@array) length
🔒

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.