Arrays in PERL in comparison with PHP

Published:

Add / read comments

Work with arrays in PHP is quite simple. All about arrays in PHP can be found in official documentation. In PERL is the situation a little bit more complicated. But, on the other hand, after first slight look into the PERL code, you can see if it's associative array or indexed array. In PHP, array variable notation is the same for both array types.

Let's start to work with arrays. Open your favorite code editor, write down the following code and save file as "pl" extension.

#!C:\xampp\perl\bin\perl.exe
 
# header for showing via browser  
print "Content-Type: text/html; charset=utf-8\n\n";
 
print "Work with arrays:";
 
### definition of INDEXED ARRAY (variable name must starts with @ ) ###
@mena = ('Milan', 'Jozef', 'Peter', 'Jan', 'Michal');
 
# break 2 lines 
print "<br /><br />";
 
# printing the result
print @mena;
 
# break 2 lines 
print "<br /><br />";
 
# printing the result with spaces between array values
print "@mena";

As you can see from the code above, variable containing indexed array in PERL must starts with @. Unlike in the PHP, where variable containing array always start with $. Also, new array in PHP is created using the array() language construct or brackets []. Example in PHP:

<?php
// indexed array in PHP 
$mena = array('Milan', 'Jozef', 'Peter', 'Jan', 'Michal');
 
print_r($mena);
 
/**
output: Array ( [0] => Milan [1] => Jozef [2] => Peter [3] => Jan [4] => Michal ) 
*/
?>

Now, if you open the PERL file via web browser, you should see something like this:

Work with arrays:

MilanJozefPeterJanMichal

Milan Jozef Peter Jan Michal

Accessing the array values in the indexed array in PERL is the same as in PHP. Just see following code:

# accessing the first member of indexed array
print "First name from array (index 0!)  ".@mena[0];
 
# accessing the second member of indexed array
print "<br /><br />Second name from array (index 1!)  ".@mena[1];

Open the PERL script via web browser and you should see the following:

First name from array (index 0!) Milan

Second name from array (index 1!) Jozef

But this is something what you can do only in PERL, not in PHP:

# accessing second and third values together (not possible in PHP)
print "<br /><br />Second and third values: @mena[1,2]";
 
# more values at the same time
print "<br />First, second and third values: @mena[0,1,2]";
 
# accessing through shorthand .. (two dots)
print "<br />First, second and third values through two dots: @mena[0 .. 2]";

Open the PERL script via web browser and you should see the following:

Second and third values: Jozef Peter
First, second and third values: Milan Jozef Peter
First, second and third values through two dots: Milan Jozef Peter

Printing the content of indexed array via two dots shorthand is very handy. Open your code / text editor and add the following code to your PERL script.

# definition of INDEXED ARRAY from 1 to 10
@pocet10 = (1 .. 10);
# definition of INDEXED ARRAY from 1 to 100
@pocet100 = (1 .. 100);
# definition of INDEXED ARRAY containing the alphabet
@abeceda = (a .. z);
 
# printing
print "<br /><br />Prints number starting from 1 to 10:<br />";
print "@pocet10";    
 
# print numbers from 5 to 7 ( array's index starts in 0 )
print "<br /><br />Numbers from 5 to 7: @pocet10[4 .. 6]";
 
print "<br /><br />Prints number starting from 1 to 100:<br />";
print "@pocet100"; 
 
print "<br /><br />Prints letter starting from a to z:<br />";
print "@abeceda";

Open the PERL script via web browser and you should see the following:

Prints number starting from 1 to 10:
1 2 3 4 5 6 7 8 9 10

Numbers from 5 to 7: 5 6 7

Prints number starting from 1 to 100:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

Prints letter starting from a to z:
a b c d e f g h i j k l m n o p q r s t u v w x y z

Now, we can start with adding, removing and counting array's values. Open your code / text editor and add the following code to your PERL script. Then, see the result via web browser.

# Adds a new element to the end of an array
push(@mena, "new-value");
 
print "<br /><br />Last value from array: " . @mena[5]; 
 
# Count number of values in array:
print '<br /><br />Array @mena contains: ' . scalar(@mena) . " values (names).";
 
# Removes the last element of the array 
pop(@mena);
 
print "<br /><br /> @mena";
print '<br />Array @mena now contains again: ' . scalar(@mena) . " values (names).";
 
# Adds to the beginning of an array	
unshift(@mena, 'to-begin');	
 
print "<br /><br /> @mena";
print '<br />Array @mena now contains: ' . scalar(@mena) . " values (names).";
 
# Removes the first element of an array
shift(@mena);
 
print "<br /><br /> @mena";
print '<br />Array @mena now contains: ' . scalar(@mena) . " values (names).";
 
# Removes an element value by index number, but keep the index	
delete @mena[1]; # remove second value of the array (index 1!!!)	
 
print "<br /><br /> @mena";
print '<br />Array @mena still contains: ' . scalar(@mena) . " values (names).";

As I mentioned earlier, there is visible difference in variable notation between indexed and associative array in PERL unlike in PHP. Let's see how to define and access values from the associative array in PERL:

### definition of ASSOCIATIVE ARRAY(variable name must starts with % ) ###
%osobne_udaje = ("name","Peter","surname","Ulicny","city","Zilina","street","Limbova","postal_code","82302");
 
# break 2 lines 
print "<br /><br />";
 
# printing the result
print %osobne_udaje;
 
# accessing the values of the associative array ( curly brackets, dollar mark $ )
print "<br /><br />Name: ". $osobne_udaje{'name'};
print "<br />Surname: ". $osobne_udaje{'surname'};
print "<br />City: ". $osobne_udaje{'city'};
print "<br />Street: ". $osobne_udaje{'street'};
print "<br />Postal code: ". $osobne_udaje{'postal_code'};

Open the PERL script via web browser and you should see the following:

cityZilinapostal_code82302streetLimbovanamePetersurnameUlicny

Name: Peter
Surname: Ulicny
City: Zilina
Street: Limbova
Postal code: 82302

And now how it's done in PHP. In PHP it's maybe a little bit more flexible:

# definition of ASSOCIATIVE ARRAY (variable name must starts with $ )
$osobne_udaje = array("name" => "Peter", "surname" => "Ulicny", "city" => "Zilina", "street" => "Limbova", "postal_code" => "82302");
 
# printing the result
print_r ($osobne_udaje);
 
/**
output:
Array ( [name] => Peter [surname] => Ulicny [city] => Zilina [street] => Limbova [postal_code] => 82302 ) 
*/
 
# accessing the values of the associative array ( curly brackets, dollar mark $ )
print "<br /><br />Name: ". $osobne_udaje{'name'};
print "<br />Surname: ". $osobne_udaje{'surname'};
print "<br />City: ". $osobne_udaje{'city'};
print "<br />Street: ". $osobne_udaje{'street'};
print "<br />Postal code: ". $osobne_udaje{'postal_code'};
 
/**
output:
  Name: Peter
  Surname: Ulicny
  City: Zilina
  Street: Limbova
  Postal code: 82302
*/
 
# or just brackets
print "<br /><br />Name: ". $osobne_udaje['name'];
print "<br />Surname: ". $osobne_udaje['surname'];
print "<br />City: ". $osobne_udaje['city'];
 
/**
output:
  Name: Peter
  Surname: Ulicny
  City: Zilina
*/

From these short examples, you could see, that there are slight differences between PERL and PHP when working with arrays.

Published:

Add / read comments

FIND ME

Share, follow or connect with me.