CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced: Re: [toolic] Multi dimentional array problem: Edit Log



toolic
User

Sep 6, 2009, 12:25 PM


Views: 1695
Re: [toolic] Multi dimentional array problem

... and here's a recursion example:

Code
use warnings;  
use strict;

my $ref = ['a',['b','c'],'d',['e',['y','z'],'f']];
my $depth = 0;
process($ref);

sub process {
my $aref = shift;
for my $x (@{$aref}) {
if (ref($x) eq 'ARRAY') {
$depth++;
process($x);
$depth--;
}
else {
print "$depth $x\n";
}
}
}

__END__
Prints out:
0 a
1 b
1 c
0 d
1 e
2 y
2 z
1 f


If you just need to print out your data structure, you could use the core module, Data::Dumper.


(This post was edited by toolic on Sep 10, 2009, 9:30 AM)


Edit Log:
Post edited by toolic (User) on Sep 7, 2009, 12:13 PM
Post edited by toolic (User) on Sep 10, 2009, 9:30 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives