Velocity Reviews > Perl > calculating a list..

# calculating a list..

Dave
Guest
Posts: n/a

 06-28-2005
I was about to create an input to this script:

http://www.destroydrop.com/javascripts/tree/

basically its an tree structute that can be created, so i thought i
would give it a try. If you look at the code you need to generate it
similar to this:

d.add(9,0,'My Pictures','example01.html','Pictures I\'ve taken over the
years','','','img/imgfolder.gif');
d.add(10,9,'The trip to Iceland','example01.html','Pictures of Gullfoss
and Geysir');
etc

what i need to do is to create the tree structure with perl i.e:

1,0
2,1
3,1
4,1
5,0
6,5
7,5
8,5
9,5
10,0
11,10
etc..

i know that at every 5th interation i need to put in a zero to make a
new "tree" and after that we need to add children to that tree. ideas?

Greg Bacon
Guest
Posts: n/a

 06-28-2005
In article <(E-Mail Removed) .com>,
Dave <(E-Mail Removed)> wrote:

: I was about to create an input to this script:
:
: http://www.destroydrop.com/javascripts/tree/
:
: basically its an tree structute that can be created, so i thought i
: would give it a try. If you look at the code you need to generate it
: similar to this:
:
: d.add(9,0,'My Pictures','example01.html','Pictures I\'ve taken over the
: years','','','img/imgfolder.gif');
: d.add(10,9,'The trip to Iceland','example01.html','Pictures of Gullfoss
: and Geysir');
: etc
:
: what i need to do is to create the tree structure with perl i.e:
:
: 1,0
: 2,1
: 3,1
: 4,1
: 5,0
: 6,5
: 7,5
: 8,5
: 9,5
: 10,0
: 11,10
: etc..
:
: i know that at every 5th interation i need to put in a zero to make a
: new "tree" and after that we need to add children to that tree. ideas?

I think you misunderstood the intent. The second parameter identifies
the parent node.

Consider the following:

#! /usr/local/bin/perl

sub print_tree {
my(\$root,\$indent) = @_;

\$indent = "" unless \$indent;

print \$indent, "- \$root->{TEXT}\n";
foreach my \$kid (@{ \$root->{KIDS} || [] }) {
print_tree(\$kid, \$indent . " ");
}
}

## main
my \$root;
my %node;

while (<DATA>) {
my(\$id,\$pid,\$text,\$url) = split /\s*,\s*/, \$1;

for (\$text, \$url) {
s/\\'/'/g;
s/^'|'\$//g;
}

my \$node = {
ID => \$id,
PID => \$pid,
TEXT => \$text,
URL => \$url
};

\$node{\$id} = \$node;

if (\$id == 0 && \$pid == -1) {
\$root = \$node;
}
else {
push @{ \$node{\$pid}{KIDS} } => \$node;
}
}
}

print_tree \$root;

__DATA__

Its output is as we'd expect:

- My example tree
- Node 1
- Node 1.1
- Node 1.1.1
- Node 1.1.1.1
- Node 1.2
- Node 2
- Node 3
- Node 4
- My Pictures
- The trip to Iceland
- Mom's birthday
- Recycle Bin

Hope this helps,
Greg
--
Editor's Note: Alan Greenspan is chairman of the Federal
Reserve and conductor of the world's greatest experiment in
paper money.
-- The Daily Reckoning, 2004/09/15

Dave
Guest
Posts: n/a

 06-28-2005
sorry i didn't correct the example data to be written. What i want to
accomplish is actually the tree as

- tree
-node1
-node1.1
-node1.1
-node1.1
-node1.1
-node2
-node2.1
-node2.1
etc..indefinetly

Greg Bacon
Guest
Posts: n/a

 06-28-2005
In article <(E-Mail Removed) .com>,
Dave <(E-Mail Removed)> wrote:

: sorry i didn't correct the example data to be written. What i want to
: accomplish is actually the tree as
:
: - tree
: -node1
: -node1.1
: -node1.1
: -node1.1
: -node1.1
: -node2
: -node2.1
: -node2.1
: etc..indefinetly

Do you want a code generator for the Javascript tree?

\$ cat tree
#! /usr/local/bin/perl

use warnings;
use strict;

sub usage { "Usage: \$0 kids grandkids-per-kid\n" }

## main
die usage unless @ARGV >= 1;

my(\$kids,\$grandkids) = @ARGV;
\$grandkids = 4 unless defined \$grandkids;

my \$id = 1;
for (my \$k = 1; \$k <= \$kids; \$k++) {
my \$parent = \$id++;

for (my \$gk = 1; \$gk <= \$grandkids; \$gk++) {
my \$text = "Node \$k.\$gk";
++\$id;
}
}

\$ ./tree 3

Hope this helps,
Greg
--
Security engineering involves programming Satan's computer. And Satan's
computer is hard to test.
-- Bruce Schneier

Dave
Guest
Posts: n/a

 06-29-2005
Yea thanks it worked like a charm after some editing, bad thing i
discoverd though that the performence from the tree structure got
really bad, i created a tree with 88 nodes, and evry node had 15
children. It even caused iexplorer to not show the tree, while firefox
warned me from beeing unresponsive but loaded the tree acurate, after a
couple of seconds. Iexplorer complained about some illegal charachter,
that didn't make sense when i looked it up. Since the performence was
quite bad i guess i will be looking at alternatives anyway. Thanks for
the help!