Perl nested array problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl nested array problem
# 1  
Old 08-04-2010
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.

Code:
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 !
# 2  
Old 08-04-2010
Hi,

If i understood correctly, If you want to traverse array reference.
Code:
my @array= ( 1,2,3,4,5);
my $arr_ref = \@array;

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

# 3  
Old 08-04-2010
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.
# 4  
Old 08-04-2010
If you just need to see the contents, try Data:: Dumper
# 5  
Old 08-04-2010
I know Data:: Dumper.I asked the way for traversing .
# 6  
Old 08-04-2010
Something like this:
Code:
$ 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

# 7  
Old 08-04-2010
Excellent
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fastest alternatives to flattening a non-uniform nested array with regex?

Hello, I'm looking at simplfying a function that flattens an array, it uses recursion and filters objects by type but this seems to be a waste of resources to me at least, using conditionals like this seems like a bad idea. The array can be a generic type, int, string, float but not some... (2 Replies)
Discussion started by: f77hack
2 Replies

2. Shell Programming and Scripting

Attempting to pass my array as a nested parameter to loop. A little lost.

I want to pass this array as a parameter. IFS=$'\n' fortune_lines=($(fortune | fold -w 30 )) Inside of this line screen -p 0 -S ${SCREEN_SESSION} -X stuff "`printf "say ${fortune_lines}\r"`" And I am lost at this point. I am thinking something like this? Then make it loop.. ... (7 Replies)
Discussion started by: briandanielz
7 Replies

3. Shell Programming and Scripting

Perl- Nested 'for' order change

Hello, I'm quite new to perl so my question is rather basic and I know there is probably a simple way around it but I can't seem to find it. I have a medium-length code and there is a part that works with a nested for loop: foreach my $j(@primpiddelta){ for (my $k=1;... (0 Replies)
Discussion started by: acsg
0 Replies

4. Shell Programming and Scripting

Perl nested if statement

I'm just having a bit of trouble running this code. It tells me that there's a syntax error on line 29. Any help appreciated. #!/usr/bin/perl # # Phone Book Application # %phonebook = ( "Wayne", '34687368', "Home", '378643287', "Work", '017374637', "School",... (2 Replies)
Discussion started by: cabaiste
2 Replies

5. Shell Programming and Scripting

[Perl] Strange problem with array

Hi, I have a strange problem with arrays in Perl. That is to say, for me it is strange and perhaps there is a special reason for it that I do not know of. Not a real Perl Ace. This is the program, as an example: #!/usr/bin/perl -w #-d use strict; my $pu; my $pu_list_cmd; my... (2 Replies)
Discussion started by: ejdv
2 Replies

6. Shell Programming and Scripting

Perl - nested substitutions

How can I nest substitutions ? My solution just seems cheap ... sample data Cisco Catalyst Operating System Software, Version 235.5(18) Cisco Catalyst Operating System Software, Version 17.6(7) Cisco Catalyst Operating System Software, Version 19.6(7) Cisco Catalyst Operating System... (1 Reply)
Discussion started by: popeye
1 Replies

7. Shell Programming and Scripting

Perl and string array problem

#!/usr/bin/perl my @arr=("hello", "how", "are", "you"); $l=length(@arr); print $l; This print 1.Why? How can i print the array size = 4? I want to store these in an array. hello how are you And then i want to access these element through indexing. How can i do this? (4 Replies)
Discussion started by: cola
4 Replies

8. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

9. Shell Programming and Scripting

Nested foreach in perl

I have two arrays @nextArray contains some files like \main\1\Xul.xml@@\main\galileo_integration_sjc\0 \main\1\PortToStorageDialog.xml@@\main\galileo_integration_sjc\0 . . . \main\1\PreferencesDialog.xml@@\main\galileo_integration_sjc\0 @otherArray contains some files like ... (2 Replies)
Discussion started by: nmattam
2 Replies

10. Shell Programming and Scripting

PERL, push to hash of array problem

$key = "a"; $value = "hello"; %myhash = {} ; push @{ myHash{$key} }, $hello; print $myHash{$key}."\n"; this script prints "hello" but has following error message. Reference found where even-sized list expected at ./test line 5. can any one help me to fix this problem?? (3 Replies)
Discussion started by: bonosungho
3 Replies
Login or Register to Ask a Question