I finally had my first Perl epiphany the other week.

Someone had posted to a newsgroup about how to extract arrays from a hash. Not being a programmer by trade, but more by happenstance, I still feel sorry for those among us (like myself) who are not gurus. Anyway, I thought I'd take a crack at explaining it, since I was curious myself. I made myself a little hash of arrays of arrays, and pondering it meditatively, I thought to myself: if only scalars are stored in the symbol table, why can't I just use the scalar name of the first array in the first array to get at the values of that array (what a mouthful!).

It all seemed too easy, so I quickly finished up the script to test whether

@extract = @{$hoa{'arrays'}[0]}
would grab the values I wanted. I was so amazed I had to look it up in the camel book just to make sure I hadn't made a discovery that would rock the Perl community to its foundations (okay, just to make sure I was right).

Anyway, that was the point at which referencing and dereferencing values suddenly made a whole lot more sense to me.

So beware Perl gurus, wherever you are; I'm on my way to the top!

matt

Replies are listed 'Best First'.
Re (tilly) 1: Things that make you go hmmm
by tilly (Archbishop) on Nov 01, 2001 at 17:59 UTC
    The way that you are talking about "naming" the other array makes me think that you have just learned about symbolic references.

    In which case allow me to help you take the next step by learning why symbolic references are unwise. (Read all three parts.) Then you can read up on strict.pm to find out how to avoid doing that by accident.

    Which means that you do want to use references to anonymous arrays. Now that may seem like a mouthful, but here is all that it means. You can think of an anonymous array as a private array that you have been given a private name for. It will work for you exactly like naming other arrays did above, but because it is your private array, you don't have to worry as you do with symbolic references about things like accidentally reusing a name and getting a conflict, or accidentally using a name that is special to Perl.

    In other words it is the same thing, only without the gotchas. :-)

      Point well taken! And thanks for the quick heads-up. I try to avoid symbolic references in my code, but now I'll try a little bit harder. : )

      matt