假设Geo::Coder::Google
数据转储的结构--- dd $location;
。
address_components => [
{
long_name => "Blackheath Avenue",
short_name => "Blackheath Ave",
types => ["route"],
},
{
long_name => "Greater London",
short_name => "Gt Lon",
types => ["administrative_area_level_2", "political"],
},
{
long_name => "United Kingdom",
short_name => "GB",
types => ["country", "political"],
},
{
long_name => "SE10 8XJ",
short_name => "SE10 8XJ",
types => ["postal_code"],
},
{ long_name => "London", short_name => "London", types => ["postal_town"] },
],
formatted_address => "Blackheath Avenue, London SE10 8XJ, UK",
geometry => {
bounds => {
northeast => { lat => 51.4770228, lng => 0.0005404 },
southwest => { lat => 51.4762273, lng => -0.0001147 },
},
location => { lat => 51.4766277, lng => 0.0002212 },
location_type => "APPROXIMATE",
viewport => {
northeast => { lat => 51.4779740302915, lng => 0.00156183029150203 },
southwest => { lat => 51.4752760697085, lng => -0.00113613029150203 },
},
},
types => ["route"],
}
示例电话:
my $long_name = &get_field_for_location("long_name", $location);
以下子返回第一个long_name
(在本例中为--- type = route):
sub get_field_for_location($$) {
my $field = shift;
my $location = shift;
my $address = $location->{address_components};
return $_->{$field} for @$address;
}
如何访问其他类型的long_name
?即如何修改此子以访问给定类型条目的$field
?
答案 0 :(得分:3)
types
是对字符串数组的引用。您需要检查它们中是否符合所需类型。您可以使用List::Util::first
:
use List::Util qw(first);
sub get_field_for_location {
my $field = shift;
my $location = shift;
my $type = shift;
my $address = $location->{'address_components'};
for my $component (@{$address}) {
if (first { $_ eq $type } @{$component->{'types'}}) {
return $component->{$field};
}
}
}
答案 1 :(得分:2)
它应首先返回类型political
,
my $type = "political";
my ($first_of_type) = grep {
grep { $_ eq $type } @{$_->{types}};
} @$address;
return $first_of_type->{$field};
外grep
过滤@$address
数组元素,内grep
过滤types
元素,即。{ ["administrative_area_level_2", "political"]