in reply to Generate Array of Hashes WITHOUT References

An array is a list of scalars. Hashes are not scalars. Ergo, arrays cannot contain hashes.

There are two workarounds if you want to store a hash in an array:

  1. References are scalars. Therefore, you can store a reference to a hash in an array.

  2. Strings are scalars. If you can serialize the hash into a string, you can store that in an array.

There is no #3, and it is almost always a terrible idea to choose #2 over #1.

Why exactly are you trying to avoid using references?

  • Comment on Re: Generate Array of Hashes WITHOUT References

Replies are listed 'Best First'.
Re^2: Generate Array of Hashes WITHOUT References
by tel2 (Pilgrim) on Sep 10, 2014 at 10:41 UTC
    Thanks for that, toby.

    I like to avoid references where there's a simple alternative, because in my case it saves brain power. I seemed to recall that this could be done without this kind of method:
      $rec->{$key} = $value;
    and it seems I was right, as I can use:
      $rec{$key} = $value;
    and I see no advantage of the former method, as the latter is more familiar, concise, and simpler for me, so that's what I'll use. It seems the only place I need to refer to the hash as a reference is when I push it onto the array, which I'm OK with.