in reply to how to construct tree from parent pointer list
If you don't feel like using the Tree module, you can build your data structure by temporarily storing the references to each hash in a separate hash, as follows.
I'm not sure what you mean by I have no idea how to store this either. . If your looking for a way to save and load data to disk, you can use Data::Dumper and 'do' as follows.
This code generates the following dump of %data:use strict; use Data::Dumper; my %data; { my %temp; while (<DATA>) { chomp; /^(.*?):(.*?)$/; my $key = $2; my $value = $1; if (!defined $temp{$key}) { $temp{$key} = {}; $data{$key} = \%{$temp{$key}}; }; $temp{$key}{$value} = \%{$temp{$value}}; }; } print Dumper \%data; __DATA__ b:a c:a d:b e:c f:c
$VAR1 = { 'a' => { 'c' => { 'e' => {}, 'f' => {} }, 'b' => { 'd' => {} } } };
I'm not sure what you mean by I have no idea how to store this either. . If your looking for a way to save and load data to disk, you can use Data::Dumper and 'do' as follows.
Be aware though that saving and loading hashes this way can be a security risk if your script and saved file have different read/write permissions for different users. (i.e. If security is set up in such a way that a user is unable to edit the Perl script, but is able to edit save.out, this would introduce a way for that user to execute code in the Perl script.)use strict; use Data::Dumper; my %saved = ( 'test1' => { 'test2' => 'b' }, 'b' => [1,2,3,4], 'test3' => 'x' ); print "Constructed hash\n"; print Dumper \%saved; open FILE, ">save.out"; print FILE Dumper \%saved; close FILE; my %loaded = %{do 'save.out'}; print "Loaded hash\n"; print Dumper \%loaded;
In Section
Seekers of Perl Wisdom