Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default symbolic references

    Greetings,
    I’m trying to generate a tree of nested hashes [of an
    arbitrary size].
    Perhaps you could suggest a better solution.
    Currently I'm trying to use symbolic references
    according to the literature… but without much luck.

    Best regards,
    David

    My script is as follows (for now, I’ve tried to keep
    the tree as simple as possible):

    #!perl

    #use strict;
    # Currently commented-out because of warning: "Can't
    use string ("1") as a SCALAR ref".

    use warnings;
    use Data::Dumper;

    my ($href, %Tree, $cluster_id, $split_val, $num_child,
    $i, $j, $k);
    $href = \%Tree;

    $cluster_id = 698;
    $split_val = 1;
    $num_child = 2;
    # For now, the above values are arbitrary
    place-holders.

    $i = 0;
    $j = 1;
    while ($i<4) {
    $$i = {
    ClusterNum => $cluster_id,
    SplitValue => $split_val,
    NumChildren=> $num_child,
    Child => $$j,
    # ‘Child’ value should be a reference to the
    subsequent hash.
    };
    if ($i==0) {
    $Tree{Root} = \$$i;
    next;
    }
    $k = ($i - 1);
    $$k = \$$i;
    $i = ($i + 2);
    $j = ($j + 2);
    }

    print Dumper($href)."\n";

    __________________________________
    Do you Yahoo!?
    Yahoo! SiteBuilder - Free web site building tool. Try it!
    [url]http://webhosting.yahoo.com/ps/sb/[/url]
    David Byrne Guest

  2. Similar Questions and Discussions

    1. symbolic references and hashes
      Hi folks, I've got the code: package Trainset; my %_BLOCKS=(); # blocks of track my %_TRAINS=(); # trains my %_BOXS=(); # signalboxes
    2. How to rm a symbolic link?
      I have a link with a priviledge of 777 , but the owner is root. how can I rm it?
    3. run-time access to symbolic information
      I am working on a routine for resolution of code address into the module/function name (C/C++, AIX 5.2). I use ldopen()/ldtbread() functions...
    4. too many symbolic links
      How can I get rid of this error :. l: can not access directory./usr/lib/lib/lib/lib/lib/lib/lib/lib/lib/lib/lib/lib/lib/lib/lib/...
    5. Help needed on Symbolic links....
      Does anyone know how to get the actual referenced filename from a symbolic link from a c program. What would be the system call? There must be one,...
  3. #2

    Default Re: symbolic references

    On Feb 4, 2004, at 1:38 PM, David Byrne wrote:
    > Greetings,
    Hello.
    > I’m trying to generate a tree of nested hashes [of an
    > arbitrary size].
    I'm trying to run the code you posted, so I can see what the heck is
    going on. That's not going well for me either. <laughs> My processor
    usage spikes through the roof, but it doesn't seem to get anywhere.
    Does this run for you?
    > Perhaps you could suggest a better solution.
    Perhaps. I know you're building some sort of hash tree, but I have no
    idea why. Can you tell us what you're aiming for?
    > Currently I'm trying to use symbolic references according to the
    > literature but without much luck.
    My opinion is that we can do better than symbolic references, yes.
    Unfortunately, I don't understand what we're trying to do. Take a step
    back and explain the problem to us, if you would, not the problem with
    the implementation.

    James

    James Edward Gray II Guest

  4. #3

    Default Re: symbolic references

    Okay, sorry to be somewhat unclear with my question.
    Here’s some sample input data that may help to clear
    things up:

    #######################
    node_id = 0
    parent_node_id = N/A
    child_node_ids = 1, 2

    node_id = 1
    parent_node_id = 0
    child_node_ids = 3, 4

    node_id = 2
    parent_node_id = 0
    child_node_ids = 5, 6, 7

    node_id = 3
    parent_node_id = 1
    child_node_ids = N/A

    node_id = 4
    parent_node_id = 1
    child_node_ids = 8, 9

    node_id = 5
    parent_node_id = 2
    child_node_ids = N/A

    node_id = 6
    parent_node_id = 2
    child_node_ids = 10

    node_id = 7
    parent_node_id = 2
    child_node_ids = N/A

    node_id = 8
    parent_node_id = 4
    child_node_ids = N/A

    node_id = 9
    parent_node_id = 4
    child_node_ids = 11, 12

    #####...and so on #####

    Notes:
    - node_id = 0 is the root node.
    - N/A means no further relationships exist.
    - Only one parent exists for each child.
    - The 'child_node_id' is redundant data. There will
    be similar descriptive data for each node, but it
    isn’t important for the structure of the tree.

    Question: How can I create a tree (i.e. a hash of
    hashes) given the above relationships?


    --- James Edward Gray II <james@grayproductions.net>
    wrote:
    > On Feb 4, 2004, at 1:38 PM, David Byrne wrote:
    >
    > > Greetings,
    >
    > Hello.
    >
    > > I’m trying to generate a tree of nested hashes [of
    > an
    > > arbitrary size].
    >
    > I'm trying to run the code you posted, so I can see
    > what the heck is
    > going on. That's not going well for me either.
    > <laughs> My processor
    > usage spikes through the roof, but it doesn't seem
    > to get anywhere.
    > Does this run for you?
    >
    > > Perhaps you could suggest a better solution.
    >
    > Perhaps. I know you're building some sort of hash
    > tree, but I have no
    > idea why. Can you tell us what you're aiming for?
    >
    > > Currently I'm trying to use symbolic references
    > according to the
    > > literature but without much luck.
    >
    > My opinion is that we can do better than symbolic
    > references, yes.
    > Unfortunately, I don't understand what we're trying
    > to do. Take a step
    > back and explain the problem to us, if you would,
    > not the problem with
    > the implementation.
    >
    > James
    >
    >
    > --
    > To unsubscribe, e-mail:
    > [email]beginners-unsubscribe@perl.org[/email]
    > For additional commands, e-mail:
    > [email]beginners-help@perl.org[/email]
    > <http://learn.perl.org/>
    > <http://learn.perl.org/first-response>
    >
    >

    __________________________________
    Do you Yahoo!?
    Yahoo! SiteBuilder - Free web site building tool. Try it!
    [url]http://webhosting.yahoo.com/ps/sb/[/url]
    David Byrne Guest

  5. #4

    Default Re: symbolic references

    On Feb 4, 2004, at 4:19 PM, David Byrne wrote:
    > Okay, sorry to be somewhat unclear with my question.
    > Here’s some sample input data that may help to clear
    > things up:
    See if this gets you going. It's one possible answer.

    James

    #!/usr/bin/perl

    use strict;
    use warnings;

    use Data::Dumper;

    my $tree = data_to_hash_tree(); # build tree

    print Dumper($tree); # print tree

    sub data_to_hash_tree {
    my @nodes; # to store nodes during reading

    local $/ = ''; # use "paragraph" read mode

    while (<DATA>) {
    my %node = map { split /\s*=\s*/ } split /\n/; # build node from data
    $nodes[$node{node_id}] = \%node; # add to list
    }

    foreach (@nodes) { # cross reference and clean up nodes
    if ($_->{child_node_ids} eq 'N/A') { $_->{child_node_ids} = [ ]; }
    else {
    $_->{child_node_ids} =
    [ map { $nodes[$_] } split /,\s*/, $_->{child_node_ids} ];
    }

    $_->{parent_node_id} = undef if $_->{parent_node_id} eq 'N/A';

    }

    return $nodes[0]; # return root
    }

    __DATA__
    node_id = 0
    parent_node_id = N/A
    child_node_ids = 1, 2

    node_id = 1
    parent_node_id = 0
    child_node_ids = 3, 4

    node_id = 2
    parent_node_id = 0
    child_node_ids = 5, 6, 7

    node_id = 3
    parent_node_id = 1
    child_node_ids = N/A

    node_id = 4
    parent_node_id = 1
    child_node_ids = 8, 9

    node_id = 5
    parent_node_id = 2
    child_node_ids = N/A

    node_id = 6
    parent_node_id = 2
    child_node_ids = 10

    node_id = 7
    parent_node_id = 2
    child_node_ids = N/A

    node_id = 8
    parent_node_id = 4
    child_node_ids = N/A

    node_id = 9
    parent_node_id = 4
    child_node_ids = 11, 12

    James Edward Gray II Guest

  6. #5

    Default Re: symbolic references

    David Byrne wrote:
    > Okay, sorry to be somewhat unclear with my question.
    > HereÂ’s some sample input data that may help to clear
    > things up:
    >
    > #######################
    > node_id = 0
    > parent_node_id = N/A
    > child_node_ids = 1, 2
    >
    > node_id = 1
    > parent_node_id = 0
    > child_node_ids = 3, 4
    >
    > node_id = 2
    > parent_node_id = 0
    > child_node_ids = 5, 6, 7
    >
    > node_id = 3
    > parent_node_id = 1
    > child_node_ids = N/A
    >
    > node_id = 4
    > parent_node_id = 1
    > child_node_ids = 8, 9
    >
    > node_id = 5
    > parent_node_id = 2
    > child_node_ids = N/A
    >
    > node_id = 6
    > parent_node_id = 2
    > child_node_ids = 10
    >
    > node_id = 7
    > parent_node_id = 2
    > child_node_ids = N/A
    >
    > node_id = 8
    > parent_node_id = 4
    > child_node_ids = N/A
    >
    > node_id = 9
    > parent_node_id = 4
    > child_node_ids = 11, 12
    >
    > #####...and so on #####
    >
    > Notes:
    > - node_id = 0 is the root node.
    > - N/A means no further relationships exist.
    > - Only one parent exists for each child.
    > - The 'child_node_id' is redundant data. There will
    > be similar descriptive data for each node, but it
    > isnÂ’t important for the structure of the tree.
    >
    > Question: How can I create a tree (i.e. a hash of
    > hashes) given the above relationships?
    although i haven't follow this thread close enough, the following seems to
    be what you want:

    #!/usr/bin/perl -w
    use strict;

    use Data::Dumper;

    my %v; my $tree;

    local $/ = '';

    while(<DATA>){
    my %t = map{y/ //d; split /=/} split /\n/;
    $v{$t{node_id}} = $t{parent_node_id};
    for(grep !/n.a/i, split(/,/,$t{child_node_ids})){
    my($p,@k) = $t{node_id};
    while($p){
    push(@k,$p);
    $p = $v{$p};
    }
    eval '$tree->{' . join('}->{',reverse $_,@k,0) . '}={}';
    }
    }

    print Dumper($tree);

    __DATA__

    [data you provided above]

    __END__

    given your above data, the script prints:

    $VAR1 = {
    '0' => {
    '1' => {
    '4' => {
    '8' => {},
    '9' => {
    '11' => {},
    '12' => {}
    }
    },
    '3' => {}
    },
    '2' => {
    '6' => {
    '10' => {}
    },
    '7' => {},
    '5' => {}
    }
    }
    };

    david
    --
    sub'_{print"@_ ";* \ = * __ ,\ & \}
    sub'__{print"@_ ";* \ = * ___ ,\ & \}
    sub'___{print"@_ ";* \ = * ____ ,\ & \}
    sub'____{print"@_,\n"}&{_+Just}(another)->(Perl)->(Hacker)
    David Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139