in reply to Building hash tree from data file -contd

I suspect there is a little XY Problem going on here. However the following code builds a lookup hash that stores translations from codes to names and lists of names associated with names:

use warnings; use strict; use Data::Dump::Streamer; my %lookup; my $main; my $action; my $text; while (<DATA>) { chomp; if (m/^Main\b/ .. m/^End\b/) { # Process Main/End blocks next if ! /Name = (\w+)/; $main = $1; } elsif (m/^Sub\b/ .. m/^End\b/) { # Process sub/end blocks $action = $1, next if /Action = Find: (\w+)/; $text = $1, next if /Text = (.*)/; next if ! /^End/ or ! defined $text; # Here if we have an End and $text is defined $lookup{$action} = $text if defined $action; my $key = $main; $key = $lookup{$key} if $key =~ /^\w\w$/; push @{$lookup{$key}}, $text; $action = undef; $text = undef; } } Dump (\%lookup);
__DATA__ Main Name = Countries End Sub Action = Find: NA Text = North America End Sub Action = Find: EU Text = Europe End --- Main Name = NA End Sub Action = Find: US Text = United States End Sub Action = Find: CA Text = Canada End Sub Action = Find: MX Text = Mexico End --- Main Name = US End Sub Action = Text = Boston End Sub Action = Text = Atlanta End --- Main Name = EU End Sub Action = Text = France End

Prints:

$HASH1 = { CA => 'Canada', Countries => [ 'North America', 'Europe' ], EU => 'Europe', Europe => [ 'France' ], MX => 'Mexico', NA => 'North America', "North America" => [ 'United States', 'Canada', 'Mexico' ], "United States" => [ 'Boston', 'Atlanta' ], US => 'United States' };

DWIM is Perl's answer to Gödel

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

    Thanks for the responses

    I was also trying to use the recursive functions to read and print from the data file.

    The code doesn't have the logic to build the tree but I was trying to get the sequence correctly.

    Looking at your scripts, I think I should scrap this and look to build on what you provided.

    Below is the code:-

    my @arr = ( 'NA', 'EU' ); my $base = 'Countries'; process_full_list( $base, @arr ); sub process_full_list { my ($base1, @arr1 ) = @_; my $elem; print "############################\n"; print "In process full list: $base1 : @arr1\n"; print "############################\n"; foreach $elem ( @arr1 ) { process_next( $base1, $elem); } } sub process_next { my ( $r_base, $r_elem ) = @_; my ( $new_base, $new_elem, @new_arr); print "\tIn process next: $r_base : $r_elem\n"; # The if loop is temporary. It will be replaced by a # sub that will fetch the next array to process by # reading from the file based on the search string. if ( $r_elem eq 'NA' ) { @new_arr = ( 'US', 'CA' ); } elsif ( $r_elem eq 'EU') { @new_arr = ( 'FR', 'IT' ); } elsif ( $r_elem eq 'US') { @new_arr = ( 'NY', 'LA' ); } elsif ( $r_elem eq 'CA') { @new_arr = ( 'TO', 'VC' ); } if( defined ( $r_elem ) && (@new_arr) ) { print "\tnew_base = $r_elem: new_arr=@new_arr\n"; process_full_list( $r_elem, @new_arr ); } }

    The output for the above is

    ########################################### In process full list: Countries : NA EU ########################################### In process next: Countries : NA new_base = NA: new_arr=US CA ########################################### In process full list: NA : US CA ########################################### In process next: NA : US new_base = US: new_arr=NY LA ########################################### In process full list: US : NY LA ########################################### In process next: US : NY In process next: US : LA In process next: NA : CA new_base = CA: new_arr=TO VC ########################################### In process full list: CA : TO VC ########################################### In process next: CA : TO In process next: CA : VC In process next: Countries : EU new_base = EU: new_arr=FR IT ########################################### In process full list: EU : FR IT ########################################### In process next: EU : FR In process next: EU : IT