Perl nested array problem

I have a array reference which has some number of array references inside it.The nested array references also contains the array references.

my $Filename = "sample.xml";

my $Parser = new XML::Parser( Style => 'tree' );
my $Tree = $Parser->parsefile( $Filename );

Here the $Tree is the array reference it will be array reference , the contents and the nested depth all depends on the xml file.I want to traverse through the nested array $Tree and print the contents.

Any help !
Thanks in advance !

Hi,

If i understood correctly, If you want to traverse array reference.

my @array= ( 1,2,3,4,5);
my $arr_ref = \@array;

for (@{$arr_ref}) {
print "Element $_\n";
}

No . The array reference may contain some array references and those references may contain some and so on .So I need to traverse the all the levels and print the data.

If you just need to see the contents, try Data:: Dumper

I know Data:: Dumper.I asked the way for traversing .

Something like this:

$ cat trav.pl
#!/usr/bin/perl

use strict;
use warnings;

my $start = [ 0, [ 1, 2, 3 ], [ 4, [ 5, 6, 7 ], 8 ], 9 ];

sub print_arr {
    my ( $level, $entry ) = @_;
    return unless ( ref $entry eq "ARRAY" );
    my @arr = @$entry;
    for ( my $i = 0 ; $i <= $#arr ; $i++ ) {
        if ( ref $arr[$i] eq "ARRAY" ) {
            print_arr( $level + 1, $arr[$i] );
        }
        else {
            print " " x $level;
            print "Element ", $i, ": ", $arr[$i], "\n";
        }
    }
}

print_arr( 0, $start );
$ perl trav.pl
Element 0: 0
 Element 0: 1
 Element 1: 2
 Element 2: 3
 Element 0: 4
  Element 0: 5
  Element 1: 6
  Element 2: 7
 Element 2: 8
Element 3: 9

Excellent