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 };

In reply to Array / Hash Confusion! by andy503

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.