#! /usr/bin/perl use strict; use Data::Dumper; my ( %linked_dsc, $link_ref ); my ( %link_parent, %link_text ); # open (FILE, "./Data.txt") or die "Can't open input: $!"; $/ = "---\n"; # each iteration will read up to the next "---" line while () { my ( $name ) = ( /Name = (.*)/ ); my @actions = ( /Action = (.*)/g ); my @texts = ( /Text = (.*)/g ); if ( $name eq 'Countries' ) { $linked_dsc{$name} = undef; $link_ref = \%linked_dsc; # ref = top of structure } else { $link_ref = $link_parent{$name}; # ref = inner layer $name = $link_text{$name}; } for my $i ( 0..$#actions ) { $link_ref->{$name}{$texts[$i]} = undef; if ( $actions[$i] =~ /Find: (.*)/ ) { $link_text{$1} = $texts[$i]; # keep track of where $link_parent{$1} = $link_ref->{$name}; # we are } } } print Dumper ( \%linked_dsc ); __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 Sub Action = Text = Italy End