linux poison RSS
linux poison Email

Perl Script - Manipulating Arrays of Arrays (Multidimensional Arrays)

Below is simple Perl scrip which demonstrate the usage of multi-dimension arrays (array of arrays)

Feel free to copy and use this code ..

Source: cat multi-dimensional_array.pl
#!/usr/bin/perl

# Multi-dimension array by reference.
$array = ["A", "B", ["1", "2", "3",  ["x", "y", "z"]]];

print "\$array->[0] == $array->[0] \n";
print "\$array->[1] == $array->[1] \n";

print "\$array->[2][1] == $array->[2][1] \n";
print "\$array->[2][2] == $array->[2][2] \n";

print "\$array->[2][3][0] == $array->[2][3][0] \n";
print "\$array->[2][3][1] == $array->[2][3][1] \n";
print "\$array->[2][3][2] == $array->[2][3][2] \n";

print " --------------------------------------- \n" ;

@array1 = (
  "A", "B",
  ["1", "2", "3",  ["x", "y", "z"]]
);

print "\$array1[0] == $array1[0] \n";
print "\$array1[1] == $array1[1] \n";

print "\$array1[2][1] == $array1[2][1] \n";
print "\$array1[2][2] == $array1[2][2] \n";

print "\$array1[2][3][0] == $array1[2][3][0] \n";
print "\$array1[2][3][1] == $array1[2][3][1] \n";
print "\$array1[2][3][2] == $array1[2][3][2] \n";

Output: perl multi-dimensional_array.pl
$array->[0] == A
$array->[1] == B
$array->[2][1] == 2
$array->[2][2] == 3
$array->[2][3][0] == x
$array->[2][3][1] == y
$array->[2][3][2] == z
 ---------------------------------------
$array1[0] == A
$array1[1] == B
$array1[2][1] == 2
$array1[2][2] == 3
$array1[2][3][0] == x
$array1[2][3][1] == y
$array1[2][3][2] == z




0 comments:

Post a Comment

Related Posts with Thumbnails