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 |