

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Slicing Array Elements in Perl
You can also extract a "slice" from an array - that is, you can select more than one item from an array in order to produce another array.
Example
#!/usr/bin/perl @days = qw/Mon Tue Wed Thu Fri Sat Sun/; @weekdays = @days[3,4,5]; print "@weekdays\n";
Output
This will produce the following result −
Thu Fri Sat
The specification for a slice must have a list of valid indices, either positive or negative, each separated by a comma. For speed, you can also use the .. range operator −
Example
#!/usr/bin/perl @days = qw/Mon Tue Wed Thu Fri Sat Sun/; @weekdays = @days[3..5]; print "@weekdays\n";
Output
This will produce the following result −
Thu Fri Sat
- Related Questions & Answers
- Replacing Array Elements in Perl
- Accessing Array Elements in Perl
- Adding and Removing Elements in Perl Array
- Accessing Hash Elements in Perl
- Perl Array Variables
- Understanding Perl Array
- Array Size in Perl
- Selecting Elements from Lists in Perl
- Add and Remove Elements in Perl Hashes
- Object Slicing in C++
- How to create Array in Perl?
- Alternate range slicing in list (Python)
- Python List Comprehension and Slicing?
- Operating system time slicing in round robin scheduling
- What is object slicing in C++ or Java?
Advertisements