- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Accessing Array Elements in Perl
When accessing individual elements from an array in Perl, you must prefix the variable with a dollar sign ($) and then append the element index within the square brackets after the name of the variable. For example −
Example
#!/usr/bin/perl @days = qw/Mon Tue Wed Thu Fri Sat Sun/; print "$days[0]\n"; print "$days[1]\n"; print "$days[2]\n"; print "$days[6]\n"; print "$days[-1]\n"; print "$days[-7]\n";
Output
This will produce the following result −
Mon Tue Wed Sun Sun Mon
Array indices start from zero, so to access the first element you need to give 0 as indices. You can also give a negative index, in which case you select the element from the end, rather than the beginning, of the array. This means the following −
print $days[-1]; # outputs Sun print $days[-7]; # outputs Mon
- Related Articles
- Accessing Hash Elements in Perl
- Slicing Array Elements in Perl
- Replacing Array Elements in Perl
- Adding and Removing Elements in Perl Array
- How do you use ‘foreach’ statement for accessing array elements in C#
- How do you use a ‘for loop’ for accessing array elements in C#?
- Accessing elements of a Pandas Series
- Array Size in Perl
- Selecting Elements from Lists in Perl
- Perl Array Variables
- Understanding Perl Array
- Add and Remove Elements in Perl Hashes
- Accessing array out of bounds in C/C++
- Accessing inner element of JSON array in MongoDB?
- How to create Array in Perl?

Advertisements