Merging Arrays in Perl



Because an array in Perl is just a comma-separated sequence of values, you can combine them together as shown below −

Example

 Live Demo

#!/usr/bin/perl @numbers = (1,3,(4,5,6)); print "numbers = @numbers\n";

Output

This will produce the following result −

numbers = 1 3 4 5 6

The embedded arrays just become a part of the main array as shown below −

Example

 Live Demo

#!/usr/bin/perl @odd = (1,3,5); @even = (2, 4, 6); @numbers = (@odd, @even); print "numbers = @numbers\n";

Output

This will produce the following result −

numbers = 1 3 5 2 4 6
Updated on: 2019-11-29T05:27:58+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements