#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $VAR1 = { '99155' => { 'PR' => [ 'state_name=Puerto Rico', 'county_names_all=Adjuntas|Utuado', ], 'AK' => [ 'state_name=Alaska', 'county_names_all=Ketchikan Gateway|Prince of Wales-Hyder', ], 'WA' => [ 'state_name=Washington', 'county_names_all=Pend Oreille|Spokane|Lincoln|Adams', 'comments=America/Los_Angeles' ] }, '26134' => { 'WV' => [ 'state_name=West Virginia', 'county_names_all=Wirt|Wood|Jackson|Ritchie|Calhoun', 'comments=America/New_York' ] } }; # Flatten this out to a row structure. One line per unique combination of stuff. # Each row is represented by an anonymous hash. # An Array of Hash is similar to the C concept of an Array of Struct. # An Array of Array representation is also possible. # This db structure is easily adapatable to an SQL DB. my @rows; foreach my $zip (keys %$VAR1) { foreach my $twoLetters (keys %{$VAR1->{$zip}}) { my %fieldHash; foreach my $field ( @{$VAR1->{$zip}->{$twoLetters}} ) { my ($detail_name, $detail_value) = split (/=/,$field); $fieldHash{$detail_name} = $detail_value; } push @rows, { zip => $zip, state => $twoLetters, %fieldHash }; } } print Dumper \@rows; =header Prints $VAR1 = [ { 'state_name' => 'Alaska', 'county_names_all' => 'Ketchikan Gateway|Prince of Wales-Hyder', 'zip' => '99155', 'state' => 'AK' }, { 'comments' => 'America/Los_Angeles', 'county_names_all' => 'Pend Oreille|Spokane|Lincoln|Adams', 'zip' => '99155', 'state' => 'WA', 'state_name' => 'Washington' }, { 'county_names_all' => 'Adjuntas|Utuado', 'state' => 'PR', 'zip' => '99155', 'state_name' => 'Puerto Rico' }, { 'comments' => 'America/New_York', 'county_names_all' => 'Wirt|Wood|Jackson|Ritchie|Calhoun', 'state' => 'WV', 'zip' => '26134', 'state_name' => 'West Virginia' } ]; =cut #print state names: my @state_names = map{$_->{state_name}}@rows; print join ",",@state_names,"\n"; # Alaska,Puerto Rico,Washington,West Virginia, #print only state names that have a comment: my @comment_state_names = map{($_->{comments}) ? $_->{state_name}: ()}@rows; print join ",",@comment_state_names,"\n"; # West Virginia,Washington, #### # Print each state and the counties that have a 'w' in them foreach my $row_ref (@rows) { my @counties = grep{/w/i}split /\|/,$row_ref->{county_names_all}; foreach my $county (@counties) { print "$row_ref->{state} $county\n"; } } =prints WV Wirt WV Wood AK Ketchikan Gateway AK Prince of Wales-Hyder =cut