in reply to Building hash tree from data file -contd

I would use a recursive function. First parse your data and keep it in a temporary hash table. Then use this recursively to substitute your "Find:" strings. Something like this: -
use strict; use Data::Dumper; my %H; while (<DATA>) { my $rootkey; my @elements; if (/Main/) { while (<DATA>) { $rootkey = "$1" if /^\s*Name\s*=\s*(.*)$/; unshift(@elements, "$1") if /^\s*(?:Action|Text)\s*=\s*(.*)$/; last if /---/; } } $H{$rootkey} = {@elements}; } my %G = process(\%H, 'Countries'); print Dumper(\%G); sub process { my ($href, $key) = @_; my %G; foreach my $k (keys %{$href->{$key}}) { $G{$key}{$k} = $href->{$key}{$k} =~ /^Find:\s+(.*)$/ ? {process(\%H, $1)} : $href->{$key}{$k}; } return %G; } __DATA__ Main Name = Countries End ... The rest of your data..

Replies are listed 'Best First'.
Re^2: Building hash tree from data file -contd
by Anonymous Monk on Jul 12, 2006 at 04:37 UTC

    Thanks a lot for your help but I am still having trouble with the output

    The output from your code is

    $VAR1 = { 'Countries' => { 'Europe' => { 'EU' => { 'France' => '' } }, 'North America' => { 'NA' => { 'Mexico' => {}, 'United States' => { 'US' => { 'Atlanta' => '', 'Boston' => '' } }, + 'Canada' => {} } } } };

    The 'find' codes such as 'EU','NA', 'US', etc. are still geting added to the data structure. I tried to modify your code without much success. I am still trying but it would be of great help, if you can help..