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}, );
use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Replies are listed 'Best First'.
Re^2: Array / Hash Confusion!
by andy503 (Initiate) on Jan 07, 2014 at 19:50 UTC

    thanks for your reply. okay I see that it returns a single scalar and I understand that the result I'm getting is a hash.

    I don't understand the idea that the scalar is a reference to an array or a hash, which is clearly where I'm going wrong.

      perlreftut and perlref are the best places to start learning about references. A reference can be thought of as a pointer to an array or hash (and a few other datatypes too, but we'll stick with arrays and hashes for now). You can have multiple references (pointers) to the same hash or array, allowing them all to see the same data. Here's a quick example:

      use strict; use warnings; use feature 'say'; my @arr = (); my $ref1 = \@arr; my $ref2 = $ref1; push @arr, "Hello"; # To access an array or hash via the reference # we need to use a pointy arrow... say $ref1->[0]; # says "Hello" say $ref2->[0]; # says "Hello" again # We use an @{...} to "dereference" an array ref. # That is, to treat it like an array. push @{$ref1}, "World"; say $arr[1]; # says "World" say $ref2->[1]; # says "World" again # The dereferencing syntax can be interpolated into # strings. say "@{$ref2}"; # says "Hello World" my %h = (); # You can't store an array in a hash because hash # values are always scalars. $h{foo} = @arr; # an array in scalar context # evaluates to its length. # This stores "2" in the hash. # But you can store a reference, because a reference # is a hash. $h{bar} = $ref1; # Let's print that "Hello" string again, via the hash. say $h{bar}->[0]; # says "Hello" # Now let's take a reference to that hash. my $ref3 = \%h; # We can also print "Hello" via the hash reference say $ref3->{bar}->[0]; # says "Hello" # When you've got a big chain of pointy arrows to # navigate through references, you only need the # first one. say $ref3->{bar}[0]; # says "Hello" # Here's something a little advanced. We'll # assign a reference to a localized glob. our %h2; local *h2 = $ref3; # Now %h2 (which is a real hash, not a reference to # a hash) is an ALIAS for %h. say $h2{foo}; # says "2" delete $h2{foo}; # also deletes $h{foo}.
      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      You can think of a reference to an array (or to a hash) as a simple scalar variable that contains the memory address of that data structure. Something quite similar to a pointer if you ever used a language using pointers.

      could this perlref be of any help?

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me