how to do this job using perl?

S

sonet

#items parent
01.attr 0
02.attr 0
04.attr 01.attr
03.attr 01.attr
06.attr 02.attr
07.attr 03.attr
08.attr 03.attr
09.attr 07.attr
=========================================
transform to this form
ps: The order must follow above
=========================================
01.attr
04.attr
03.attr
07.attr
09.attr
08.attr
02.attr
 
P

Paul Lalli

sonet said:
#items parent
01.attr 0
02.attr 0
04.attr 01.attr
03.attr 01.attr
06.attr 02.attr
07.attr 03.attr
08.attr 03.attr
09.attr 07.attr
=========================================
transform to this form
ps: The order must follow above
=========================================
01.attr
04.attr
03.attr
07.attr
09.attr
08.attr
02.attr

How to do it using Perl?
Step 1) Learn Perl
Step 2) Write a Perl program
Step 3) Run your Perl program

What have you tried so far? How did it fail? With what step do you
need help?

Please, before replying, make sure you read the Posting Guidelines for
this group, which are posted here twice a week, and can be easily found
by browsing the archives.

Paul Lalli
 
J

jack

I'm not sure I understand the question.
What do you mean by "the order must follow above"

You have "02.attr 0" as the second line in 'above' and in the last
line 'below'

Also, be aware that almost nobody here will actually solve this problem
for you. You have to try to solve it, then provide an explicit perl
based question about an aspect of your attempted solution that fails.

There is no such thing as a free lunch
 
J

Josef Moellers

sonet said:
#items parent
01.attr 0
02.attr 0
04.attr 01.attr
03.attr 01.attr
06.attr 02.attr
07.attr 03.attr
08.attr 03.attr
09.attr 07.attr
=========================================
transform to this form
ps: The order must follow above

solution must be delivered to teacher until 5pm today.
=========================================
01.attr
04.attr
03.attr
07.attr
09.attr
08.attr
02.attr


What have YOU tried so far and where did YOUR code not produce the
desired result?
 
S

sonet

First, this is not a homework.

The Tree::Simple can do this job after i search modules in CPAN.
But i don't want to do this by modules only if i can not slove it.
If this is anOS directory , i know how to solve it. But the data is in
a file. Someone design his system fllow this rules and i just want to
try it.

Sorry! My mother language is not english.And my english is very poor.
Please help me and give me a keyword about this topic. I can slove it
by myself. You don't need to write some code. Just give a suggestion.
 
J

jack

sonet said:
First, this is not a homework.
Cool


The Tree::Simple can do this job after i search modules in CPAN.
But i don't want to do this by modules only if i can not slove it.
If this is anOS directory , i know how to solve it. But the data is in
a file. Someone design his system fllow this rules and i just want to
try it.

You could 'pinch' some ideas from the perl source for Tree::Simple.
Although how that is any better than using the module itself is a bit
of a mystery. The module will almost invariably be less buggy than
anything you come up with, unless you spend the more hours in your
development than have gone into Tree::Simple.
Sorry! My mother language is not english.And my english is very poor.
Please help me and give me a keyword about this topic. I can slove it
by myself. You don't need to write some code. Just give a suggestion.

Did you try posting this quesiton in your native language version of a
perl newsgroup - if such a thing exists. Example: German Language is
de.comp.lang.perl.misc
 
S

sonet

Thanks for your reply!
My native language version of a perl newsgroup had become a spam group.
It just have less 5%'s atricles is not spam.(I live in taiwan and my native
language
is chinese[big5]).

Yes! I should read Tree::Simple first.

In fact,i want to know the principle of the Hierarchical DB Like LDAP .
It is using DBD or LDBM to store the data. I want to find that how to
using HASH DB design Hierarchical. It is have many atricles about
Hierarchical
in RDBMS.But i don't find the topic about HASH DB.

Anyway! Thank your suggest!
 
D

David K. Wall

sonet said:
#items parent
01.attr 0
02.attr 0
04.attr 01.attr
03.attr 01.attr
06.attr 02.attr
07.attr 03.attr
08.attr 03.attr
09.attr 07.attr
=========================================
transform to this form
ps: The order must follow above
=========================================
01.attr
04.attr
03.attr
07.attr
09.attr
08.attr
02.attr

See if the post at
http://groups.google.com/group/comp.lang.perl.misc/msg/55ee2e6f76c4cbeb
helps any. It's a similar idea.
 
A

A. Sinan Unur

[ Please do not top-post ]
First, this is not a homework.

That is good.
The Tree::Simple can do this job after i search modules in CPAN.
But i don't want to do this by modules only if i can not slove it.

That's admirable. I think you should give it a try, and post your best
attempt. That will make it *easier* for us to help you.
Sorry! My mother language is not english.And my english is very poor.

There is no need to apologize for poor English, especially if you post
code, because then we can figure out what you are trying to do and what
is not working by looking at the code. That is why the posting guidelines
recommend:

Speak Perl rather than English, when possible

Perl is much more precise than natural language. Saying it in
Perl instead will avoid misunderstanding your question or problem.

Do not say: I have variable with ``foo\tbar'' in it.

Instead say: I have $var = ``foo\tbar'', or I have $var = 'foo\tbar',
or I have $var = <DATA> (and show the data line).

You can find the posting guidelines at

http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

Do read them, they contain invaluable pointers on how to help yourself,
and help others help you.
Just give a suggestion.

Use a hash :) (just joking)

The question itself is a little more complicated than I can figure out
right now (which is why I personally would use the module for the
immediate problem, and then try to read the source code, or consult an
algorithms book to see how to do it).

Sinan
 
B

Brian McCauley

sonet said:
01.attr 0
02.attr 0
04.attr 01.attr
03.attr 01.attr
06.attr 02.attr
07.attr 03.attr
08.attr 03.attr
09.attr 07.attr
=========================================
transform to this form
=========================================
01.attr
04.attr
03.attr
07.attr
09.attr
08.attr
02.attr

use strict;
use warnings;

my %children;

sub r {
my ($depth, $items) = @_;
return unless $items;
for ( @$items ) {
print " " x $depth, "$_\n";
r($depth+1,$children{$_});
}
}

while (<DATA>) {
my ($child,$parent) = /(.*) (.*)/ or die;
push @{$children{$parent}} => $child;
}

r(0,$children{0});

__DATA__
01.attr 0
02.attr 0
04.attr 01.attr
03.attr 01.attr
06.attr 02.attr
07.attr 03.attr
08.attr 03.attr
09.attr 07.attr
 
M

MrReallyVeryNice

sonet said:
#items parent
01.attr 0
02.attr 0
04.attr 01.attr
03.attr 01.attr
06.attr 02.attr
07.attr 03.attr
08.attr 03.attr
09.attr 07.attr
=========================================
transform to this form
ps: The order must follow above
=========================================
01.attr
04.attr
03.attr
07.attr
09.attr
08.attr
02.attr
Hello,

I believe that the following code should fit the bill. It is a bit long
but it also performs some minimum data validation (you will notice that
I added to your data set).

use strict;
use warnings;

my %tree;
my $initialK;
my $K;
my $V;
my $MaxDepth = 10;
my $depth;

while (<DATA>)
{
my ($dt1,$dt2) = /(.*) (.*)/ or die;
$tree{$dt1} = $dt2;
}

sub modifyValue
{
if ($depth >= $MaxDepth )
{
$tree{$initialK} = "XXXXXXXXXXX: Following $initialK leads a Depth >
$MaxDepth\n";
}
else
{
if(!exists $tree{$V} and $V ne "0")
{
$tree{$initialK} = "YYYYYYYYYYY " . $tree{$initialK};
}
elsif ($V ne "0" )
{
$depth = $depth + 1;
$K = $V;
$V = $tree{$K};
$tree{$initialK} = $V . " " . $tree{$initialK};
modifyValue();
}
}
}


foreach my $key (sort hashValueDescendingNum(keys(%tree)))
{
$initialK = $key;
$K = $key;
$V = $tree{$key};
$depth = 0;
modifyValue;
$tree{$initialK} = $tree{$initialK} . " " . $initialK;
}


foreach my $key (sort hashValueAscendingNum(keys(%tree)))
{
if (substr($tree{$key},0,11) eq "XXXXXXXXXXX")
{
print "\nData set problem: $key may be nested to more than $MaxDepth
level deep!" ;
}
elsif (substr($tree{$key},0,11) eq "YYYYYYYYYYY")
{
print "\nData set problem: $key or one of its ancestor may not
have a root!" ;
}
else
{
my @spaces =split(/ /,$tree{$key});
print (" "x($#spaces-1) . $key . "\n");
}
}



sub hashValueDescendingNum
{
$tree{$b} cmp $tree{$a};
}

sub hashValueAscendingNum
{
$tree{$a} cmp $tree{$b};
}


__DATA__
01.attr 0
02.attr 0
04.attr 01.attr
03.attr 01.attr
06.attr 02.attr
07.attr 03.attr
08.attr 03.attr
09.attr 07.attr
10.attr 11.attr
a.attr 0
b.attr a.attr
c.attr b.attr
d.attr c.attr
e.attr d.attr
f.attr e.attr
g.attr f.attr
h.attr g.attr
i.attr h.attr
j.attr i.attr
k.attr j.attr
l.attr k.attr


Would that work for you?
MrReallyVeryNice
 
D

Dave Cross

Thanks for your reply!
My native language version of a perl newsgroup had become a spam group.
It just have less 5%'s atricles is not spam.(I live in taiwan and my native
language
is chinese[big5]).

Another alternative would be to get in touch with a local Perl Monger
group.

See http://www.pm.org/groups/asia.html for a list of groups in Asia.
Taiwan has a surprisingly high number for such a small country.

Dave...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top