in reply to Array / Hash Confusion!
my @data = decode_json($response); is a big red flag. JSON's decode_json function doesn't return a list. It always returns a single scalar. This scalar will be a reference to either an array or a hash.
Assigning a scalar value to the array variable @data is not wrong per se, but all it does is set the array to be a single-item array (hence the appearance of foreach "not working").
In this case, it appears to be returning a reference to a hash.
Try something like this:
my $data = decode_json($response); printf( "'%s' was released in year %d and it is rated %s.\n", $data->{title}, $data->{year}, $data->{rating}, );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Array / Hash Confusion!
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 |