How to pass the array in Perl

Task. In the code of the Perl program has an array @arr (the first three letters from the word "array"). In this array has some values. You need to go through all the array elements and perform some action with each item.

Go through the array using foreach

The recommended way to solve this problem is to use foreach. Here is a sample program:

▶ Run
#!/usr/bin/perl

my @arr = (1, 2, 3);

foreach my $el (@arr) {
    warn $el;
}

It's all very simple. There is an array. Next we create a variable $el (an abbreviation of the word element). Cycle foreach places in that variable the first element of the array (in our example this number is 1). Executes the loop body warn $el; — this brings the value on the screen. Cycle foreach puts into the variable $el the next value from the array again executes the loop body. This is repeated until the loop body is not executed for all array elements.

Go through the array using a loop for

Here is a sample program how to walk on a Perl array using a loop similar to the loop in the C language:

▶ Run
#!/usr/bin/perl

my @arr = (1, 2, 3);

for (my $i = 0; $i < @arr; $i++) {
    warn $arr[$i];
}

What's going on. There is an array @arr. Then we created a new variable $i and assigned it the value 0 (0 is the index the first element in a Perl array). Then executes the loop body warn $arr[$i];. After that, the variable $i with operator ++ is increased by one. It then checks the conditions $i < @arr. If the condition returned true, then all repeats.

This solution is somewhat more complicated than the solution using foreach. You need to carefully work with the boundary values. If it is wrong to write a check, you can either skip the last value or to access a nonexistent element beyond the last element of the array (this treatment will return undef).

Here is an example program that works the same way, but use a different condition for:

▶ Run
#!/usr/bin/perl

my @arr = (1, 2, 3);

for (my $i = 0; $i <= $#arr; $i++) {
    warn $arr[$i];
}

for array it makes sense to use if you are in the loop body should not only the value of the element, and its number. But in this case you can use foreach cycle, manually exposing the variable with the index:

▶ Run
#!/usr/bin/perl

my @arr = (1, 2, 3);
my $i = 0;

foreach my $el (@arr) {
    warn "index: $i value: $el";
    $i++;
}

This option is easier perceived by the person and it is less susceptible to the Bahamas with boundary value. But there's a more convenient way to say the same thing with each.

Go through the array using while and each

Keyword each is not used that very often, but almost always when it's used it used to work with Hesham. Little known, but each can be used to go through the array, with the index of the element and the element itself. Here is the previous code written using each:

▶ Run
#!/usr/bin/perl

my @arr = (1, 2, 3);

while (my ($i, $el) = each @arr) {
    warn "index: $i value: $el";
}

each can be applied to arrays from Perl 5.12. If you run this code on more earlier versions of Perl, it will error:

Type of arg 1 to each must be hash (not private array) at script.pl line 5, near "@arr) "
Execution of script.pl aborted due to compilation errors.

Related topics

Other articles

Comments