Ask a Question related to PERL Beginners, Design and Development.
-
David Byrne #1
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
-
symbolic references and hashes
Hi folks, I've got the code: package Trainset; my %_BLOCKS=(); # blocks of track my %_TRAINS=(); # trains my %_BOXS=(); # signalboxes -
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? -
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... -
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/... -
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,... -
James Edward Gray II #2
Re: symbolic references
On Feb 4, 2004, at 1:38 PM, David Byrne wrote:
Hello.> Greetings,
I'm trying to run the code you posted, so I can see what the heck is> I’m trying to generate a tree of nested hashes [of an
> arbitrary size].
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. I know you're building some sort of hash tree, but I have no> Perhaps you could suggest a better solution.
idea why. Can you tell us what you're aiming for?
My opinion is that we can do better than symbolic references, yes.> Currently I'm trying to use symbolic references according to the
> literature but without much luck.
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
-
David Byrne #3
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.
>> an> > I’m trying to generate a tree of nested hashes [of>> > 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?
>> according to the> > Currently I'm trying to use symbolic references>> > 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
-
James Edward Gray II #4
Re: symbolic references
On Feb 4, 2004, at 4:19 PM, David Byrne wrote:
See if this gets you going. It's one possible answer.> Okay, sorry to be somewhat unclear with my question.
> Here’s some sample input data that may help to clear
> things up:
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
-
David #5
Re: symbolic references
David Byrne wrote:
although i haven't follow this thread close enough, the following seems to> 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?
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



Reply With Quote

