从文件数据构建树::简单对象 [英] Build Tree::Simple object from file data
问题描述
有没有人有使用Tree::Simple
的经验a> 或在 Perl 中将数据放入树中?
Does anyone have any experience of using Tree::Simple
or putting data into a tree in Perl?
假设我有以下 CSV 数据文件:-
Say I have the following CSV file of data:-
Bob, Bill, Ben, Brett
Bob, Bill, Brian
Bob, John, Jim
Alice, John, Jill, Jane
Alice, Jean
Alice, Janet, Brian
我想将其转换为树结构以获得以下内容:
I would like to convert it into a tree structure in order to obtain the following:
1, Bob
2, Alice
1.1, Bill
1.1.1, Ben
1.1.1.1, Brett
1.1.2, Brian
1.2, John
1.2.1, Jim
2.1, John
2.1.1, Jill
2.1.1.1, Jane
2.2, Jean
2.3, Janet
2.3.1, Brian
我查看了 Tree::Simple
并且知道如果我可以将数据放入树中,我可以使用 Tree::Parser
以正确的编号输出它.
I have looked at Tree::Simple
and know that if I can get the data into a tree I can use Tree::Parser
to output it with the correct numbering.
我想要的是一个如何逐行输入数据的示例.我可以首先将文件的每一行读入一个数组,然后添加 $array[0]
作为第一个子元素 - 例如
What I would like is an an example of how I could input the data line by line. I could start by reading each line of the file into an array, and adding $array[0]
in as the first child - e.g.
$tree->addChildren(Tree::Simple->new($array[0]));
我不知道该怎么做的是:
What I don't know how to do is:
检查树中是否已经有一个叫这个名字的孩子,这样我就不会添加两次 say Bob 了.
Check whether there already is a child with that name in the tree, so that I don't add say Bob in twice.
为每个孩子找到正确的父母,以便说 Jill 被输入为 John 的孩子,John 是 Alice 的孩子,而不是 Bob 的孩子
Find the correct parent for each child so that say Jill is entered as a child of the John who is the child of Alice, not the child of Bob
我非常感谢您对这个问题的帮助.我已经为此工作了 4 天,但无法到达那里.如果 Tree::Simple
不是最好的方法,也许还有另一种方法?
I would much appreciate some help on this problem. I've been working on it for 4 days and cannot get there. If Tree::Simple
is not the best way of doing this, perhaps there is another way?
推荐答案
该程序从 DATA
文件句柄中读取您显示的示例数据,并且似乎可以满足您的需求.
This program reads the sample data you have shown from the DATA
filehandle and appears to do what you need.
它通过将所有节点放在由每个节点的值索引的散列中来跟踪树中的所有节点.每次输入文件中出现一个值时,都会检查散列以查看它的节点是否已经存在.如果不是,则创建一个新的树节点并将其添加到哈希中.无论哪种方式,该节点都用作同一行上下一个值的父节点.
It keeps track of all the nodes in the tree by putting them in a hash indexed by each node's value. Every time a value appears in the input file the hash is checked to see if a node for it already exists. If not then a new tree node is created and added to the hash. Either way the node is used as the parent for the next value on the same line.
没有检查数据的一致性,假设每个节点最多有一个父节点.如果某个值在数据文件中第二次出现在不同父名称下,则新关联将被忽略.
There is no check for consistency of the data, and it is assumed that every node has at most one parent. If a value appears a second time in the data file beneath a different parent name the new association is ignored.
use strict;
use warnings;
use Tree::Simple;
use Tree::Parser;
my %nodes;
my $root = Tree::Simple->new('root');
while (<DATA>) {
my $parent = $root;
for my $name (split) {
$name =~ tr/,//d;
$nodes{$name} = Tree::Simple->new($name, $parent) unless $nodes{$name};
$parent = $nodes{$name};
}
}
my $tp = Tree::Parser->new($root);
$tp->useDotSeparatedLevelFilters;
print "$_\n" for $tp->deparse
__DATA__
Bob, Bill, Ben, Brett
Bob, Bill, Brian
Bob, John, Jim
Alice, John, Jill, Jane
Alice, Jean
Alice, Janet, Brian
输出
1 Bob
1.1 Bill
1.1.1 Ben
1.1.1.1 Brett
1.1.2 Brian
1.2 John
1.2.1 Jim
1.2.2 Jill
1.2.2.1 Jane
2 Alice
2.1 Jean
2.2 Janet
这篇关于从文件数据构建树::简单对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!