Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I have a file called weather.txt that looks like this:
$VAR1 = [ { 'xml_api_reply' => { '-version' => '1', 'weather' => { '-row' => '0', 'current_conditions' = +> { + 'icon' => { + '-data' => '/ig/images/weather/mostly_cloudy.gif' + }, + 'temp_f' => { + '-data' => '73' + }, + 'temp_c' => { + '-data' => '23' + }, + 'wind_condition' => { + '-data' => 'Wind: NW at 6 mph' + }, + 'humidity' => { + '-data' => 'Humidity: 73%' + }, + 'condition' => { + '-data' => 'Mostly Cloudy' + } + }, , 'forecast_information' + => { + 'forecast_date' => { + '-data' => '2012-08-16' + }, + 'city' => { + '-data' => 'Miami, FL' + }, + 'latitude_e6' => { + '-data' => '' + }, + 'postal_code' => { + '-data' => '02151' + }, + 'current_date_time' => { + '-data' => '2012-08-16 12:54:00 +0000' + }, + 'longitude_e6' => { + '-data' => '' + }, + 'unit_system' => { + '-data' => 'US' + } + }, } } }, { 'xml_api_reply' => { '-version' => '1', 'weather' => { '-row' => '0', 'current_conditions' = +> { + 'icon' => { + '-data' => '/ig/images/weather/mostly_cloudy.gif' + }, + 'temp_f' => { + '-data' => '73' + }, + 'temp_c' => { + '-data' => '23' + }, + 'wind_condition' => { + '-data' => 'Wind: NW at 6 mph' + }, + 'humidity' => { + '-data' => 'Humidity: 73%' + }, + 'condition' => { + '-data' => 'Mostly Cloudy' + } + }, 'forecast_information' + => { + 'forecast_date' => { + '-data' => '2012-08-16' + }, + 'city' => { + '-data' => 'Boston, MA' + }, + 'latitude_e6' => { + '-data' => '' + }, + 'postal_code' => { + '-data' => '01908' + }, + 'current_date_time' => { + '-data' => '2012-08-16 12:54:00 +0000' + }, + 'longitude_e6' => { + '-data' => '' + }, + 'unit_system' => { + '-data' => 'US' + } + }, } } }, ];

what is the best way to extract the values from this array of hashes if its in a file? If it was an array of hashes I could access anyhting using this criteria:
my $test_data = $data[0]{'xml_api_reply'}{'weather'}{'forecast_informa +tion'}{'city'}->{"-data"};

But since it is in a file I am a litle confused:
Test Code
.... my %hash; open my $fh, '<', 'weather.txt' or die "Cannot open: $!"; while (my $lines = <$fh>) { my @data = $lines; my $key = shift @data; my $test_data = $data[0]{'xml_api_reply'}{'weather'}{'forecast_infor +mation'}{'city'}->{"-data"}; print "$test_data\n"; } close $fh;

Thanks for the help!

Replies are listed 'Best First'.
Re: Open and parsing a file with array of hashes!?
by hbm (Hermit) on Aug 16, 2012 at 15:08 UTC

    Another option?

    use strict; use warnings; my $weather = do "weather.txt"; print $weather->[0]->{xml_api_reply}->{weather}->{forecast_information +}->{city}->{-data},$/; # prints Miami, FL
      Could it be in a loop in get all the city names?
        use strict; use warnings; my $weather = do "weather.txt"; print $_->{xml_api_reply}->{weather}->{forecast_information}->{city}-> +{-data},$/ for @$weather;

        Prints:

        Miami, FL Boston, MA
Re: Open and parsing a file with array of hashes!?
by Anonymous Monk on Aug 16, 2012 at 14:55 UTC
Re: Open and parsing a file with array of hashes!?
by stonecolddevin (Parson) on Aug 16, 2012 at 16:32 UTC

    yea you should marshall/serialize this. Storable or MooseX::Storable are pretty neat for that purpose.

    Three thousand years of beautiful tradition, from Moses to Sandy Koufax, you're god damn right I'm living in the fucking past

      What about this what do you think?
      #!/usr/bin/perl -w use strict; use Data::Dumper; use YAML; ... # from your code you do what was mentioned here: push @data, $tree; } # end of your foreach for my $record (@data) { my $cities = "$record->{'xml_api_reply'}{'weather'}{'forecast_inf +ormation'}{'city'}->{'-data'}"; print "$cities\n"; }

      Let us know, thanks!
Re: Open and parsing a file with array of hashes!?
by linuxkid (Sexton) on Aug 16, 2012 at 15:23 UTC

    do $file; or better yet use YAML!

    --linuxkid


    imrunningoutofideas.co.cc