andy503 has asked for the wisdom of the Perl Monks concerning the following question:
hi venerable monks,
I'm new to Perl so please be fairly gentle...
I have the following code that's building a rather nice array of data, and I can get to that data. However I have a strong feeling that this is by luck rather than design, especially in the array / hash area near line 22/23!
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use LWP::Simple; use JSON; ### using api from http://mymovieapi.com/ # define url for get request # ghostbusters url my $url = "http://mymovieapi.com/?id=tt0087332&type=json&plot=simple&e +pisode=1&lang=en-US&aka=simple&release=simple&business=0&tech=0"; # receive JSON into $response, via get call using LWP::Simple my $response = get $url; # die if we have problems making the GET request die 'Error making GET request -> $url' unless defined $response; # usual print line for troubleshooting #print Dumper($response); ### using JSON's decode_json we decode the JSON string into an array @ +data # my array @data gets the decoded JSON string my @data = decode_json($response); #print Dumper($data); # for each element in the now decoded array, make them accessible foreach (@data) { print $_->{title} . " was released in year -> " . $_->{year} . " , + and it is rated " . $_->{rating}; }
first question... @data is an array but should I be rather trying to build a hash in order to more simply access the key pairs and deal with editing later on?
second question... with @data, I'm accessing the values via $_->{title}, which is a global modifier whatchamacallit, there must be a safer/better way to do this? something like foreach $element in @data... $element->{title} ?
third question... @data is not returning multiple results to necessitate foreach, but I can't access the results without it... I would like to follow my @data...; with something like $_->{title} but it bleats about an uninitialized value?
the JSON response is successful and is noted here so that you can see the depth of the array and the various key pairs.
$VAR1 = { 'genres' => [ 'Comedy', 'Fantasy', 'Sci-Fi' ], 'plot_simple' => 'Three unemployed parapsychology professors + set up sh op as a unique ghost removal service.', 'poster' => { 'imdb' => 'http://ia.media-imdb.com/images/M/M +V5BNTUzMDU 3OTEyNV5BMl5BanBnXkFtZTYwOTk1MDE5._V1_SY317_CR2,0,214,317_.jpg', 'cover' => 'http://imdb-poster.b0.upaiyun.com/ +000/087/33 2.jpg!cover?_upt=a7275e191389164607' }, 'title' => 'Ghost Busters', 'year' => 1984, 'imdb_url' => 'http://www.imdb.com/title/tt0087332/', 'country' => [ 'USA' ], 'also_known_as' => [ 'Ghostbusters' ], 'writers' => [ 'Dan Aykroyd', 'Harold Ramis' ], 'language' => [ 'English' ], 'rating_count' => 167966, 'type' => 'M', 'directors' => [ 'Ivan Reitman' ], 'actors' => [ 'Bill Murray', 'Dan Aykroyd', 'Sigourney Weaver', 'Harold Ramis', 'Rick Moranis', 'Annie Potts', 'William Atherton', 'Ernie Hudson', 'David Margulies', 'Steven Tash', 'Jennifer Runyon', 'Slavitza Jovan', 'Michael Ensign', 'Alice Drummond', 'Jordan Charney' ], 'imdb_id' => 'tt0087332', 'runtime' => [ '105 min' ], 'filming_locations' => 'Greystone Park & Mansion - 905 L +oma Vista Drive, Beverly Hills, California, USA', 'rating' => '7.8', 'release_date' => 19841202 };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Array / Hash Confusion!
by tobyink (Canon) on Jan 07, 2014 at 19:40 UTC | |
by andy503 (Initiate) on Jan 07, 2014 at 19:50 UTC | |
by tobyink (Canon) on Jan 07, 2014 at 20:11 UTC | |
by Laurent_R (Canon) on Jan 07, 2014 at 19:53 UTC | |
by 2teez (Vicar) on Jan 07, 2014 at 20:00 UTC | |
|
Re: Array / Hash Confusion!
by toolic (Bishop) on Jan 07, 2014 at 19:41 UTC | |
by andy503 (Initiate) on Jan 07, 2014 at 19:56 UTC | |
by SuicideJunkie (Vicar) on Jan 07, 2014 at 20:03 UTC | |
by toolic (Bishop) on Jan 07, 2014 at 20:03 UTC | |
|
Re: Array / Hash Confusion!
by Laurent_R (Canon) on Jan 07, 2014 at 19:46 UTC | |
|
Re: Array / Hash Confusion!
by locked_user sundialsvc4 (Abbot) on Jan 07, 2014 at 21:59 UTC | |
by chromatic (Archbishop) on Jan 08, 2014 at 18:44 UTC |