I did post to the perl-beginners mails list, still have not gotten any
responses. I will try there first in the future.

Ian's worked great, except it kept repeating. Here is what I came up
with on my own, with a little help from a friend at work. The second
foreach in ShowPage is there to test what I will pass to Template.pm.

#!\usr\bin\perl

use strict;

use vars qw($Down $personHeader $person_number %personTree %persons
%persons @DownList @personHeader);
my $person_number = "571508";

&CreateTableArrays;
&ShowPage;

########################################################################
########
#
########################################################################
########
sub ShowPage() {

        push (@DownList, $person_number);

        foreach my $key ( keys %persons ) {
                if ( $person_number eq $persons{$key}->{personNumber}) {
                        foreach my $key1 ( keys %persons ) {
                                if (
$persons{$key}->{UplinepersonNumber} eq $persons{$key1}->{personNumber}
) {
                                        print "Upline -> personNumber
--> $persons{$key1}->{personNumber}\r\n";
                                }
                        }
                        print "\tpersonNumber ->
$persons{$key}->{personNumber}\r\n";
                        $personTree{$person_number}{$Down} .= ();
                        &GetDown($person_number);
                }
        }

        foreach my $key ( sort keys %personTree ) {
                print "$key|$personTree{$key}->{$personHeader}\r\n";
        }

}       #End of ShowVisionPage

########################################################################
########
#
########################################################################
########
sub GetDown() {

        my ($NewNames) = @_;

        foreach my $key ( keys %persons ) {
                if ( $NewNames eq $persons{$key}->{UplinepersonNumber} )
{
                        print "\t\tDown to $NewNames ->
$persons{$key}->{personNumber}\r\n";
                        $personTree{$NewNames}{$Down} .=
"$persons{$key}->{personNumber} ";

                        &GetDown($persons{$key}->{personNumber});
                }
        }

}       #End of GetDown

########################################################################
########
#
########################################################################
########
sub CreateTableArrays() {

        my @personHeader = qw(personNumber UplinepersonNumber);

        while (my $personLine = <DATA>){
                chomp($personLine);
                my @person_in = split(/\|/, $personLine);
                for ( my $i=0 ; $i <= $#personHeader ; $i++ ) {
                        $persons{$person_in[0]}{$personHeader[$i]} =
$person_in[$i];
                }
        }

}       #End of CreateTableArrays

1;

__DATA__

123456|1438566
123457|1438566
123458|1438566
123459|1438566
123460|1438566
123461|1438566
123462|1438566
123463|1438566
123464|1438566
1438566|22222
564737|123458
564738|123458
564739|123458
564740|123458
564741|123458
571508|1438566
987651|571508
987652|571508
987653|571508
987654|571508
987655|987651
987656|987651
987657|987651
987658|987657

--end--

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] 
Sent: Tuesday, January 07, 2003 5:21 PM
To: [EMAIL PROTECTED]
Subject: Re: Array problem


On 07 Jan 03 at 09:54:13AM, Tim Dolezal wrote:
> If this is the wrong list for this question, can you please let me 
> know whish list I should go to.
> 
> My output should look something like this:
> 
> a-\
>   |-b

 <snip>


> My data file has at least these two columns:
> 
> Col1    Col2
>  a       
>  b       a

 <snip>


> What is the best way to do this?

I wouldn't claim mine is the best solution, but it is reasonable. It
creates a hash of lists, where each node is a list of children for that
node. Then it starts at the root node and recursively prints out the
tree. I fiddled with different mechanisms for correctly printing the
lines that connect to later nodes, but I settled on simply remembering a
string to print ($indent).

----------

SCRIPT:

#!/usr/bin/perl -w

use strict;

my %tree;
while (my $line = <DATA>)
{
    my ($item, $parent) = split /\s+/, $line;
    push @{$tree{$parent}}, $item;
}

print_node("", "");

sub print_node
{
    my ($key, $indent) = @_;
    return unless exists $tree{$key};
    my @children = @{$tree{$key}};

    for (my $i = 0; $i < @children; ++$i)
    {
        my $child = $children[$i];

        print "$indent| \n";
        print "$indent\\-$child\n";
        
        my $new_indent = $indent . (($i == @children - 1)? "  ": "| ");
        print_node($child, $new_indent);
    }
}


__DATA__
a
b       a
c       b
d       c
e       c
f       a
g       d
h       f
i       f
j       i
k       i
l       k
m       a


OUTPUT:

| 
\-a
  | 
  \-b
  | | 
  | \-c
  |   | 
  |   \-d
  |   | | 
  |   | \-g
  |   | 
  |   \-e
  | 
  \-f
  | | 
  | \-h
  | | 
  | \-i
  |   | 
  |   \-j
  |   | 
  |   \-k
  |     | 
  |     \-l
  | 
  \-m


TIM'S OUTPUT:

a-\
  |-b
  | | 
  | \-c
  |   |
  |   \-d
  |   | |
  |   | \-g
  |   |
  |   \-e
  | 
  |-f
  | |
  | \-h
  | |
  | \-I
  |   |
  |   \-j
  |   |
  |   \-k
  |     |
  |     \-l
  |
  \-m


LIMITATIONS:

. It does not sort the child nodes in any way; it wouldn't be hard.

. It does not format the tree exactly as you have specified (although
your formatting for the root node is not consistent anyway). You might
want to put in a special case for the root node to avoid it appearing to
be a child of nothing.

. It is case-sensitive. Your data and output (i, I) suggest
case-insensitivity.

----------

Regards,


Ian



Reply via email to