Leser: 3
10 Einträge, 1 Seite |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper qw/Dumper/;
use Perl6::Say;
use Tree::Simple;
my $tree = Tree::Simple->new("0", Tree::Simple->ROOT);
my $sub_tree = $tree->addChild(Tree::Simple->new("1"));
$sub_tree->addChildren(
Tree::Simple->new("2.1"),
Tree::Simple->new("2.2")
);
say $tree->getChild("2.1")->getNodeValue();
say $tree->getChild("2.2")->getNodeValue();
Quote2.2
2.2
1 2
print $tree->getChild(1)->getNodeValue(); print $tree->getChild(2)->getNodeValue();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
sub find { my $tree = shift; my $node_value=shift; my $last=0; my $child; my $func; $func = sub ($) { $child=shift(); $last=1 if($child->getNodeValue() eq $node_value); my @children=$child->getAllChildren(); $func->(shift(@children)) while(@children && !$last); }; $func->($tree); return $child if($last); return undef; }
my $node = find($tree,"<NodeValue>")
1 2 3 4 5 6 7 8 9 10 11 12 13 14
sub hash_nodes { my $tree=shift; my %h=(); my $sub; $sub = sub ($) { my $node=shift; my $value=$node->getNodeValue(); push(@{$h{$value}},$node); }; $tree->traverse($sub); return %h; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
sub find { my $tree = shift; my $node_value=shift; my @childs=(); my $func; $func = sub ($) { my $child=shift(); push(@childs,$child) if($child->getNodeValue() eq $node_value); my @children=$child->getAllChildren(); $func->(shift(@children)) while(@children); }; $func->($tree); return @childs; }
my @nodes=find($tree,"<NodeValue>");
10 Einträge, 1 Seite |