Bootstrap FreeKB - Perl (Scripting) - Getting Started with Arrays
Perl (Scripting) - Getting Started with Arrays

Updated:   |  Perl (Scripting) articles

An array in created by using the @ character. Following are both valid examples of how to create an empty array (does not contain any values).

my @fruits;
my @fruits = ();

 

In this example, an array named fruit contains different types of fruit.

my @fruits = ("apple", "banana", "orange", "grapes");

 

Instead of placing values in the array, variables can be used.

my @fruits = ("$citrus", "$berries", "$dry", "$fleshy");

 

Instead of placing quotes around each items in the array, the qw (quote words) function can be used. This also has the added benefit of not having to separate the data with commas. However, this cannot be used with variables in the array.

my @fruits = qw(apple banana orange grapes);

 

The print command can then be used to print each element in the array.

print @fruits;

 

Which should produce a list like this.

apple banana orange grapes

 

Each element in an array has a unique index number.

  • $fruits[0] = apple
  • $fruits[1] = banana
  • $fruits[2] = orange
  • $fruits[3] = grapes

 

You can print a specific element in the array by specifying the index of the element.

print $fruits[2];

 

Which, in this example, will print:

orange

 

The following can be used to determine the index number of a specific element in the array.

my ($index) = grep { $fruits[$_] eq "orange" } (0 .. @fruits-1);
$index = defined $index ? $index : -1;
print "$index \n";

 


Dumper

Dumper can be used to verify that each item in the array is a unique value. You first need to tell your Perl script to use Dumper.

use Data::Dumper;

 

You can then use print the Dumper output of an array.

print Dumper @fruits;

 

In this example, each piece of fruit is a uniqe value in the array.

$VAR1 = 'apple';
$VAR2 = 'banana';
$VAR3 = 'orange';
$VAR4 = 'grapes';

 


Files / Directories (glob)

If you want to create an array of files or directories, glob is a really good approach.

 


For each loop

foreach loop can be used to loop through a loop through an array. In this example, a foreach loop prints each piece of fruit in the array.

#!/usr/bin/perl

my @fruits = qw(apple banana orange grapes);

foreach my $fruit (@fruits) {
    print "$fruit\n";
}

 

Running the script will produce the following output.

apple
banana
orange
grapes

 


Creating an array in a foreach loop

Let's say you want to create arrays inside of the for each loop. In this example, the @afruit array will contain fruit that begins with the letter "a", and the @bfruit array will contain fruit that begins with the letter "b". The "i" option here is to ignore case (e.g. case insensitive match).

#!/usr/bin/perl

my @fruits = qw(apple banana orange grapes);
my @afruit = ();
my @bfruit = ();

foreach $fruit (@fruits) {
    print "$fruit\n";

    if ($fruit =~ /^a/i) {
      @afruit = $fruit
    }

    if ($fruit =~ /^b/i) {
      @bfruit = $fruit
    }
}

print "@afruit \n";
print "@bfruit \n";

 

Running the script will produce the following output.

apple
banana

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 2394a5 in the box below so that we can be sure you are a human.