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!

In reply to Open and parsing a file with array of hashes!? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.