Lesson 5: Accessing elements of an array

Example Array:

my @favs = ("Pink Floyd" , "Led Zeppelin", "The Doors");
index value
0 Pink Floyd
1 Led Zeppelin
2 The Doors
  • To access only one element of the array you are accessing a single scalar.
  • The $ and not the @ will be used.
my $first_ele = $favs[1];
print $first_ele , "\n";


%% ./pieces_of_the_array.pl
Led Zeppelin
  • Notice the square brackets [ ].
  • [ ] indicate that we are accessing a piece of the array.
  • The number inside the [ ] indicate the exact element.
  • @favs[1] is incorrect


  1. Write a script in which you create an array.
  2. In this script assign the 0th and the 3rd elements to two scalar variables
  3. Add a print statement in which you print a sentence that uses the two scalar variables of the two elements

Print Friendly

Leave a Reply

Your email address will not be published. Required fields are marked *