Thread Rekursive durcharbeitung von Arrays (29 answers)
Opened by Gast at 2006-08-09 12:40

renee
 2006-08-09 13:37
#68750 #68750
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Mit dem Hash würde es dann so gehen:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/perl

use strict;
use warnings;

my %switches = (switch0 => [qw/switch1 switch2 switch3/],
switch1 => [qw/switch4/],);

my $start = 'switch0';

print_tree(\%switches,$start,0);

sub print_tree{
my ($hashref,$start,$level) = @_;

my $whitespaces = ' ' x ($level * 5);
print $whitespaces.$start,"\n";

for my $switch(@{$hashref->{$start}}){
if(exists $hashref->{$switch}){
print_tree($hashref,$switch,$level+1);
}
else{
my $whitespaces = ' ' x (($level+1) * 5);
print $whitespaces.$switch,"\n";
}
}
}


Edit: Fehler beseitigt\n\n

<!--EDIT|renee|1155117220-->
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/

View full thread Rekursive durcharbeitung von Arrays