in reply to Array refs as hash keys

I should say that I don't believe I've fully understood what you're looking for.

In answer to the first part of your question, if you need to use any kind of reference as a hash key, what's wrong with using the stringified version of it as a hash key? It's unique (even under threads and fork) so it should do the trick. Yet you should think about what this means: it is the reference itself that it the key in this case, not the contents (whatever you get when you deference it). Is this what you mean? Maybe. I can think of times when this is useful. The only caveat is that the stringified version of the reference won't increase the reference count of the object itself, so you will have to be careful that the object isn't destroyed when you still need it. Embedding the reference inside the value of the hash element for which it is the key might be a good trick to force that.

In the second part of your question though you seem to want something different: you seem to want to index based on the value of the object pointed to. This will not work of course: [ qw( a b c ) ] != [ qw (a b c ) ] (they are different objects). Do you want to use a serialized (stringified) representation of the contents of the reference? That's not very doable in general (think of self referential data structures or structures that contain code references). So I guess it depends on what you are really trying to do.

A major disclaimer being: I am a HUGE fan of programatic data structures. By this I mean I have a tendancy to create static complex data structures that will allow for conditional switching, and cool one-liners to utilize easily modifiable structures rather than having to go back for raw logic changes later; just change the structure.

I don't consider that "disclaimer material". Behaviour driven by data ans the real "code" only acts in support of that. It's very maintainable and often self documenting. A very good practice, in my opinion, as long as you pay attention to security issues.

Replies are listed 'Best First'.
Re^2: Array refs as hash keys
by wazzuteke (Hermit) on Dec 16, 2005 at 05:12 UTC
    Thanks for the reply, and such a detailed one at that.

    I guess my real question was somewhat lost in my lenghty description of my recent situation. Really, it was more of a design philosiphy question rather than a Perl question.

    To be clearer: [ qw( a b c ) ] (could be) == [ qw( a b c ) ] when you are referencing the reference to the anonymous array back as the reference to the key. Again, I know this isn't possible... and I also realize that the stringified value could be unique (though not guaranteed since it is to be the memory reference address which could be re-used by another object/struct/etc), still not exactly what I was looking for.

    Really, I know I can't do this as I really want to. It was a methodology I used when I designed apps in (dare I say it) Java (and a long time ago, when I was a little dumber). I was really hoping to see if anyone else has ran into such a situation, and if so, how did they get around it.

    Thanks again for your reply, though; sometimes it's harder to explain a programatic situation in a textfield that I sometimes give myself credit for.

    ---hA||ta----
    print map{$_.' '}grep{/\w+/}@{[reverse(qw{Perl Code})]} or die while ( 'trying' );