How to create Array in Perl?


Perl Array variables are prefixed with the @ sign and are populated using either parentheses or the qw operator. For example −

@array = (1, 2, 'Hello');
@array = qw/This is an array/;

The second line uses the qw// operator, which returns a list of strings, separating the delimited string by white space. In this example, this leads to a four-element array; the first element is 'this' and last (fourth) is 'array'. This means that you can use different lines as follows −

@days = qw/Monday
Tuesday
...
Sunday/;

You can also populate an array by assigning each value individually as follows −

$array[0] = 'Monday';
...
$array[6] = 'Sunday';

Updated on: 28-Nov-2019

119 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements