Top 6 ways to Create empty size array, & Initialize with values(Examples)

This tutorial explains multiple ways

  • create an empty-sized array with a length of zero
  • create and initialize an array with elements containing zero
  • create and initialize an array with elements containing space

How to create an empty array with zero elements in Perl

The array can be declared as follows

  • Declare an Array variable the variable is declared using the my keyword, followed by a variable prefixed with @.
my @numbers;

The empty array is created in multiple ways.

An empty array is a new array created with an empty(zero sizes), and no elements exist.

One way is to assign with empty size by assigning ()

The second way to create an empty array is by assigning w() Third way, assign sigil(@) followed by Square brackets in Perl 5.2 version The fourth way, assign parenthesis with the under function

In all cases above, It creates an empty list with a new array reference.

Here is an example

#Declare array variable
my @numbers;
print @numbers;

## Declare and assign an empty list
my @numbers1=();
print @numbers1;

## Empty array with qw() without elements
my @number3=qw()

# Empty Array with @sigil followed by square brackets in Perl 5.2  or more
my @numbe4=@[]

## Create an empty array with an undef function
my @number5=(undef)

Output empty array

How to create an array initialized with zero values

In Perl, You can create zero values using the x operator.

the array can be assigned with elements containing zero using multiplier syntax.

syntax

(element) x n

element is an element that is populated into an array. to create an array with elements zero, use (0). x is a multiplier operator n tells the number of elements to populate

my @numbers1 = (0) x 4;
print @numbers1;
0000

How to create an array assigned with empty spaces

the array can be assigned with empty spaces using multiplier syntax.

syntax

(element) x n

element is an element that is populated into an array. to create an array with empty spaces, use ("") n tells the number of elements to populate x is a multiplier in Perl

Here is an example

my @numbers1 = (" ") x 5;
print @numbers1;

How do I initialize an array in Perl?

To initialize an array, Please follow the below steps.

  • Declare an array variable using the @ prefix
  • Arrays contain any scalar values such as string, numbers, floating numbers
  • Assign an array using a list of values enclosed in ()

for example, an array is initialized in many ways

First, way, assign with numbers, separated commas in ().

Second, way, Initialize with strings, and separate commas in (). The third way is to declare an array with a range of numbers using .. with numbers, separated .. in (). Fourth, Define an array initialized with a mix of scalar types strings, numbers, and floats. Fifth, Declare an array with repeated values using the X operator. Sixth, Array is initialized with an expression using the athematic operator. Elements contain the result of these operators.

# Array of numbers

my @numbers=(1,2,3,4,5,6,7,8,9,10)

# Array of strings

my @word=("one", "two", "three")

# Initialize Array of numbers

my @numbers1 = (10..16);

# Initialize Array with scalars strings, numbers, floating

my @mixedValues = (3, "one", "two", 4.12, "four", 11);

# Initialize Array with repeated value

my @samevaluesarray = (2) x 5;

# Initialize Array with expression value

my @arrayexpressions = (2+1,3+4,5-2,2\*2);

You can test by printing an array using the print join(", ", @array), "\n"; syntax.

In Summarize, Perl provides multiple ways with empty array syntax, initialize with different ways. Choose based on your code and readability.