Pages

8/30/2014

Iterate through multidimensional array in perl


Its quite easy to declare one dimensional array in perl and iterate through them.for example
1
2
3
4
5
my @array = (1,2,3,"Hell World");
foreach my $ar (@array)
   {
      print("$ar\n");
   }

but when comes to multiple dimensional array,what will do?.Its quite challenging but easy to implement.Multiple deimensional means array inside an array for example my @array = ([1,2,3],[4,5,6]);
1
my @array = ([1,2,3],[4,5,6]);

Here [1,2,3] is the first array and [4,5,6] is the second array,The strategy is to first iterate through arrays then iterate inside each arrays for example
1
2
3
4
5
6
7
8
my @array = ([1,2,3,4],[5,6,7,8]);
    foreach my $ar (@array)
    {
     foreach my $arr (@$ar)
     {
       print("$arr\n");
     }
    }

No comments:

Post a Comment

widget

installing perl dancer in ubuntu

For installing perl dancer you need curl utility for installing curl ,   sudo apt-get install curl Now we can install Perl-Dancer by thi...