- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Replacing Array Elements in Perl
Now we are going to introduce one more function called splice(), which has the following syntax −
Syntax
splice @ARRAY, OFFSET [ , LENGTH [ , LIST ] ]
This function will remove the elements of @ARRAY designated by OFFSET and LENGTH, and replaces them with LIST if specified. Finally, it returns the elements removed from the array. Following is the example −
Example
#!/usr/bin/perl @nums = (1..20); print "Before - @nums\n"; splice(@nums, 5, 5, 21..25); print "After - @nums\n";
Output
This will produce the following result −
Before - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 After - 1 2 3 4 5 21 22 23 24 25 11 12 13 14 15 16 17 18 19 20
Here, the actual replacement begins with the 6th number after that five elements are then replaced from 6 to 10 with the numbers 21, 22, 23, 24 and 25.
- Related Articles
- Accessing Array Elements in Perl
- Slicing Array Elements in Perl
- Adding and Removing Elements in Perl Array
- Accessing Hash Elements in Perl
- Array Size in Perl
- Selecting Elements from Lists in Perl
- Perl Array Variables
- Understanding Perl Array
- Replacing array of object property name in JavaScript
- Add and Remove Elements in Perl Hashes
- How to create Array in Perl?
- JavaScript: replacing object keys with an array
- How to replace elements in array with elements of another array in JavaScript?
- Replacing Substrings in a Java String
- Replacing words with asterisks in C++

Advertisements