Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Google decoder (how to access hash values, perl dereferencing string xpaths data dumper )

by Anonymous Monk
on Sep 10, 2013 at 08:25 UTC ( #1053197=note: print w/replies, xml ) Need Help??


in reply to Google decoder

code outputs perl code like data::dumper only more verbose (ready for copy/paste to access ) ex

$location->{address_components}[0]{types}[0] = "street_number"; #d3 $location->{address_components}[5]{short_name} = "CH"; #d2 $location->{geometry}{location_type} = "ROOFTOP"; #d1

#!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd pp /; my $location = my $VAR1 = { 'formatted_address' => 'Huoben 2, 6023 Rothenburg, Schweiz', 'types' => [ 'street_address' ], 'address_components' => [ { 'types' => [ 'street_number' ], 'short_name' => '2', 'long_name' => '2' }, { 'types' => [ 'route' ], 'short_name' => 'Huoben', 'long_name' => 'Huoben' }, { 'types' => [ 'locality', 'political' ], 'short_name' => 'Rothenburg', 'long_name' => 'Rothenburg' }, { 'types' => [ 'administrative_area_level_2', 'polit +ical' ], 'short_name' => 'Hochdorf', 'long_name' => 'Hochdorf' }, { 'types' => [ 'administrative_area_level_1', 'political' ], 'short_name' => 'LU', 'long_name' => 'Luzern' }, { 'types' => [ 'country', 'political' ], 'short_name' => 'CH', 'long_name' => 'Schweiz' }, { 'types' => [ 'postal_code' ], 'short_name' => '6023', 'long_name' => '6023' } ], 'geometry' => { 'viewport' => { 'southwest' => { 'lat' => '47.0860081197085', 'lng' => '8.2563233197085' }, 'northeast' => { 'lat' => '47.0887060802915', 'lng' => '8.2590212802915' } }, 'location' => { 'lat' => '47.0873571', 'lng' => '8.2576723' }, 'location_type' => 'ROOFTOP' } }; rehohy( $location, 0, '$location->' ); dd( $location->{geometry}{location_type} ); dd( $location->{geometry}{viewport}{northeast}{lng} ); dd( $location->{address_components}[4]{long_name} ); dd( $location->{address_components}[5]{types}[1] ); exit( 0 ); sub rehohy { use Data::Dump qw/ pp /; my( $hoh, $depth, $path, $callback ) = @_; $depth ||= 0; $path ||= ''; if( 'HASH' eq ref $hoh ) { while( my( $key, $val ) = each %$hoh ) { $key = pp( $key ) if $key =~ /\s|\W/; if( ref $val ) { rehohy( $val, $depth + 1, "$path\{$key}" ); } else { if( $callback ) { $callback->( $val, "$path\{$key}" ); } else { my $vall = pp( $val ); print "$path\{$key}\n", ( " " x $depth ), "= $vall; #d$depth\n\n"; } } } } elsif( 'ARRAY' eq ref $hoh ) { my $key = 0; for my $val ( @$hoh ) { if( ref $val ) { rehohy( $val, $depth + 1, "$path\[$key]" ); } else { if( $callback ) { $callback->( $val, "$path\[$key]" ); } else { my $vall = pp( $val ); print "$path\[$key]\n", ( " " x $depth ), "= $vall; #d$depth\n\n"; } } $key++; } } } ## end sub rehohy __END__
  • Comment on Re: Google decoder (how to access hash values, perl dereferencing string xpaths data dumper )
  • Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1053197]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2019-11-12 03:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (64 votes). Check out past polls.

    Notices?