2

ジオコーディングをサポートするために既存のperlスクリプトを変更しようとしています。このモジュールが見つかりました:http://metacpan.org/pod/Geo :: Coder :: Google

返されるハッシュ構造からデータを抽出する方法がわかりません(私はPerlコーダーではありません。これは、修正する必要のあるレガシースクリプトです)。

       {
        '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
          ]
        }
      }

すでにグーグルで見つけたすべてのハッシュチュートリアルを試しましたが、印刷できるのはHASH(0x91e5558)のようなものです。これまでの私のコードは、モジュールが例として示しているものです。

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'); 

ポイント->データをそれ自体の変数に調整し、データベースに書き込むことができます。

4

3 に答える 3

5

これは、あなたの望むことですか?

$lon = $location->{Point}{coordinates}[0];
$lat = $location->{Point}{coordinates}[1];
于 2011-09-16T09:34:15.523 に答える
2

これのOOバージョンをコーディングしやすいものを提示したかっただけです。達彦が提供しなかったので、それが可能であることを示したかった

  • 他の場所で作成されたデータ構造を祝福し、データ構造に動作を与えます。
  • メソッドをクラス(パッケージ)にプッシュする

これがパッケージの定義です。

package Geo::Coder::Google::Geocode;
use strict;
use warnings;
use Carp         qw<croak>;
use Params::Util qw<_ARRAY _CLASS _CLASSISA _HASH _INSTANCE>;

sub new { 
    croak( 'Not a valid subclass' )
        unless my $class  = _SUBCLASS( _CLASS( $_[0] ), __PACKAGE__ )
        ;
    croak( 'Not a valid structure!' )
        unless my $struct = _HASH( $_[1] ) 
           and _HASH( $_[0]->{Point} )
        ;
    # Done with checks, just bless it
    return bless( $struct, $class );
}

sub coordinates {

    my ( $self, $point, $coords ) = shift;
    # Make sure each link in the chain exists ( and is populated ).
    return unless _INSTANCE( $self, __PACKAGE__ )
              and $point  = _HASH( $self->{Point} )
              and $coords = _ARRAY( $point->{coordinates} )
              ;
    We have an array ref here, return it.
    return wantarray ? @$coords : $coords;
}

{ package Geo::Coder::Google;
    use Carp         qw<croak>;
    use Params::Util qw<_HASH>;
    sub get_geocode {
        croak( 'Geocode not created!' ) unless my $gcode = _HASH( &geocode );
        return Geo::Coder::Google::Geocode->new( $gcode );
    }
}

次に、次のように使用できます。

my ( $lat, $long )
    = $geocoder->get_geocode( 
       location => 'Hollywood and Highland, Los Angeles, CA'
     )->coordinates
     ;

これにより、迅速なカプセル化が作成され、将来のアクセスのコーディングが容易になるだけでなく、使用するコードに簡単な変更が加えられます。

この関数を追加することもできます。

{ package Geo::Coder::Google;
    use Carp         qw<croak>;
    use Params::Util qw<_HASH>;
    sub get_coordinates {
        croak( 'Geocode not created!' ) unless my $gcode = _HASH( &geocode );
        return Geo::Coder::Google::Geocode->new( $gcode )->coordinates;
    }
}

そして、電話してください:

my ( $lat, $long ) 
    = $geocoder->get_coordinates( location => 'Hollywood and Highland, Los Angeles, CA' )
    ;
于 2011-09-16T15:04:26.187 に答える
0

あなたはこのようなものを探しています:

$hashref = {
    'a' => 'A',
    'b' => {
        'c' => 'C',
        'some' => 'value',
    },
};

print "$hashref->{b}{some}\n"; # output: value
于 2011-09-16T09:43:22.353 に答える