Skip navigation.
Home
Your source for Perl tips, howto's, faq and tutorials

initialize all elements of an array with the same value

( categories: | )

A useful method to initialize all the elements of an array with the same value is with the 'x' repetition operator.

The 'x' operator is mostly used in scalar context to build a string with a repeated pattern but, when used in a list context, it allows to set all the elements of an array with the same value.

Examples:
#-- initialixe @array with 50 zeroes
@array = 0 x 50;
 
#-- reset all elements to value '1'
@array = (1) x @array


get the last element and/or index value of an array

( categories: )

Array index values start from zero.To obtain the last index value of an array ('n-1' if the array has 'n' elements), use the following syntax:

   $#array

To get the value of the last element of the array, you can either do:

   $array[$#array]

or

   $array[-1]
 

Example:

@array = qw(rock paper scissors);
 
print "Last index value of array: " . $#array . "\n";
print "Value of last element of array: " . $array[-1] . "\n";


arrange the elements of an array in random order

( categories: )

Use 'shuffle()' from List::Util module.

Example:

#!/usr/bin/perl
 
use List::Util qw(shuffle);
 
@sorted_array = (1, 2, 3, 4, 5, 6);
@random_array = shuffle(@sorted_array);
 
print '@random_array: ' . join(', ', @random_array) . "\n";


adding and removing elements in an array

( categories: )

- ADD A LIST OF ELEMENTS TO THE FRONT

unshift @array, @list;

NOTES:
- unshift returns the new number of elements in the array.
- @list is prepended as a whole; not one element at a time, so it does not behave exactly as a normal "push" operation in a stack.

- REMOVE THE FIRST ELEMENT

shift @array;

NOTES:
- shift returns the value of the discarded element
- if shift is applied on an empty array, it returns undef

- ADD A LIST OF ELEMENTS AT THE END

push @array, @list;

NOTE:
- push returns the new number of elements in the array


sort an array of numbers

( categories: )

By default the sort function uses alphabetical order to sort elements. To sort using numerical order, you have to use the syntax shown below.

- In ascending order:

@sorted_array = sort { $a <=> $b } @array;

- In descending order:

@sorted_array = sort { $b <=> $a } @array;


iterate through an array

( categories: )

- BASIC

The general way of iteration is using foreach:

# print all elements of an array
foreach my $element ( @array )
{
  print $element . "\n";
}

- ADVANCED

If you want to iterate through an array to obtain a subset of its elements you may use grep:

# get all elements that begin with 'fun'
@fun_array = grep /^fun/ @array;

If you want to do something to each element of an array, you can use map:

# convert all elements to lowercase
@lowercase = map { lc } @array;


pick a random element from an array

( categories: )

$random_element = $array[rand @array];


get the size of an array

( categories: )

- METHOD 1

Use the array in scalar context to get the size (number of elements) of the array. To force an array to be evaluated in scalar context, use the scalar operator.

Example:
print "Size of array is " . scalar @array;

IMPORTANT! Please notice that this method ONLY works with arrays, it doesn't work with lists; if you evaluate a list in scalar context it will return the value of the last element of the list (like in the C language)

Example:


reverse an array

( categories: )

@reversed_array = reverse(@array);


empty an array

( categories: )

To empty an array:

@array = ();

Please notice that the above method doesn't free the memory already associated with the array; to recover the used memory you must use:

undef @array;


Syndicate content