Search code examples
perlhashperl-data-structures

Getting data out of perl hash structure?


I'm trying to modify existing perl script to support geocoding. Found this module for it: http://metacpan.org/pod/Geo::Coder::Google

I just can't figure out how to extract data from the hash structure it returns (I'm not a perl coder, this is just some legacy script I have to fix).

       {
        'AddressDetails' => {
          'Country' => {
            'AdministrativeArea' => {
              'SubAdministrativeArea' => {
                'SubAdministrativeAreaName' => 'San Francisco',
                'Locality' => {
                  'PostalCode' => {
                    'PostalCodeNumber' => '94107'
                  },
                  'LocalityName' => 'San Francisco',
                  'Thoroughfare' => {
                    'ThoroughfareName' => '548 4th St'
                  }
                }
              },
              'AdministrativeAreaName' => 'CA'
            },
            'CountryNameCode' => 'US'
          }
        },
        'address' => '548 4th St, San Francisco, CA 94107, USA',
        'Point' => {
          'coordinates' => [
            '-122.397323',
            '37.778993',
            0
          ]
        }
      }

Tried all the hash tutorials I found on google already, the most I can get it to print is something like HASH(0x91e5558). My code thus far is what the module show's as an example:

use Geo::Coder::Google;
my $geocoder = Geo::Coder::Google->new(apikey => 'Your API Key');
my $location = $geocoder->geocode( location => 'Hollywood and Highland, Los Angeles, CA'); 

I'd just want the Point -> coordinates data to it's own variables which I can then write to database.


Solution

  • Is this what you want?

    $lon = $location->{Point}{coordinates}[0];
    $lat = $location->{Point}{coordinates}[1];