Skip to main content

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");
     }
    }

Comments

Popular posts from this blog

creating first go program

for creating go program open your editor and paste this code, and save it as filename.go for example helloworld.go

package main import "fmt" func main() { fmt.Println("Hello, 世界") }
I will explain these codes in details for now , we will look how to run these code.

to run type go run helloworld.go  in terminal , we get


you can try golang online as well , golang