calculating a list..

D

Dave

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(0,-1,'My example tree');
d.add(1,0,'Node 1','example01.html');
d.add(2,1,'Node 2','example01.html');
d.add(3,1,'Node 1.1','example01.html');
d.add(4,1,'Node 3','example01.html');
d.add(5,1,'Node 1.1.1','example01.html');
d.add(6,1,'Node 1.1.1.1','example01.html');
d.add(7,1,'Node 4','example01.html');
d.add(8,1,'Node 1.2','example01.html');
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');
d.add(11,9,'Mom\'s birthday','example01.html');
d.add(12,9,'Recycle Bin','example01.html','','','img/trash.gif')
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?
 
G

Greg Bacon

: 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(0,-1,'My example tree');
: d.add(1,0,'Node 1','example01.html');
: d.add(2,1,'Node 2','example01.html');
: d.add(3,1,'Node 1.1','example01.html');
: d.add(4,1,'Node 3','example01.html');
: d.add(5,1,'Node 1.1.1','example01.html');
: d.add(6,1,'Node 1.1.1.1','example01.html');
: d.add(7,1,'Node 4','example01.html');
: d.add(8,1,'Node 1.2','example01.html');
: 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');
: d.add(11,9,'Mom\'s birthday','example01.html');
: d.add(12,9,'Recycle Bin','example01.html','','','img/trash.gif')
: 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>) {
while (/\s*d\.add\((.+)\);/g) {
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__
d.add(0,-1,'My example tree');
d.add(1,0,'Node 1','default.html');
d.add(2,0,'Node 2','default.html');
d.add(3,1,'Node 1.1','default.html');
d.add(4,0,'Node 3','default.html');
d.add(5,3,'Node 1.1.1','default.html');
d.add(6,5,'Node 1.1.1.1','default.html');
d.add(7,0,'Node 4','default.html');
d.add(8,1,'Node 1.2','default.html');
d.add(9,0,'My Pictures','default.html');
d.add(10,9,'The trip to Iceland','default.html');
d.add(11,9,'Mom\'s birthday','default.html');
d.add(12,0,'Recycle Bin','default.html');

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
 
D

Dave

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
 
G

Greg Bacon

: 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;

print "d.add(0,-1,'tree');\n";

my $id = 1;
for (my $k = 1; $k <= $kids; $k++) {
my $parent = $id++;
print "d.add($parent,0,'Node $k');\n";

for (my $gk = 1; $gk <= $grandkids; $gk++) {
my $text = "Node $k.$gk";
print "d.add($id,$parent,'$text');\n";
++$id;
}
}

$ ./tree 3
d.add(0,-1,'tree');
d.add(1,0,'Node 1');
d.add(2,1,'Node 1.1');
d.add(3,1,'Node 1.2');
d.add(4,1,'Node 1.3');
d.add(5,1,'Node 1.4');
d.add(6,0,'Node 2');
d.add(7,6,'Node 2.1');
d.add(8,6,'Node 2.2');
d.add(9,6,'Node 2.3');
d.add(10,6,'Node 2.4');
d.add(11,0,'Node 3');
d.add(12,11,'Node 3.1');
d.add(13,11,'Node 3.2');
d.add(14,11,'Node 3.3');
d.add(15,11,'Node 3.4');

Hope this helps,
Greg
 
D

Dave

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!
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top