calculating a list..

Discussion in 'Perl Misc' started by Dave, Jun 28, 2005.

  1. Dave

    Dave Guest

    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?
     
    Dave, Jun 28, 2005
    #1
    1. Advertising

  2. Dave

    Greg Bacon Guest

    In article <>,
    Dave <> 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(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
    --
    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
     
    Greg Bacon, Jun 28, 2005
    #2
    1. Advertising

  3. Dave

    Dave Guest

    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
     
    Dave, Jun 28, 2005
    #3
  4. Dave

    Greg Bacon Guest

    In article <>,
    Dave <> 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;

    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
    --
    Security engineering involves programming Satan's computer. And Satan's
    computer is hard to test.
    -- Bruce Schneier
     
    Greg Bacon, Jun 28, 2005
    #4
  5. Dave

    Dave Guest

    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!
     
    Dave, Jun 29, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Asad
    Replies:
    2
    Views:
    447
    Matt Berther
    Apr 27, 2004
  2. Sparky Arbuckle

    Calculating a Subtotal for Shopping Cart

    Sparky Arbuckle, Mar 6, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    1,647
    Sparky Arbuckle
    Mar 6, 2005
  3. Nathan Sokalski

    Calculating the width of an element

    Nathan Sokalski, May 30, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    466
    Herfried K. Wagner [MVP]
    May 30, 2005
  4. =?Utf-8?B?RGlmZmlkZW50?=

    Calculating the allocation size of an object

    =?Utf-8?B?RGlmZmlkZW50?=, Feb 3, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    444
    =?Utf-8?B?RGlmZmlkZW50?=
    Feb 3, 2006
  5. =?Utf-8?B?RG9uYWxk?=

    Calculating Bandwidth

    =?Utf-8?B?RG9uYWxk?=, Feb 8, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,245
    S. Justin Gengo [MCP]
    Feb 8, 2006
Loading...

Share This Page