Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

RE: RE: My views on pseudohashes

by Zoogie (Curate)
on Jun 24, 2000 at 00:03 UTC ( #19654=note: print w/replies, xml ) Need Help??

in reply to RE: My views on pseudohashes
in thread My views on pseudohashes

I had no clue what a pseudohash was... until I started searching for "pseudo-hash" instead of "pseudohash". Apparently, you can use an array as a hash. Here's a a snippet from perlref on the subject:

Beginning with release 5.005 of Perl you can use an array reference in some contexts that would normally require a hash reference. This allows you to access array elements using symbolic names, as if they were fields in a structure.

For this to work, the array must contain extra information. The first element of the array has to be a hash reference that maps field names to array indices. Here is an example:

$struct = [{foo => 1, bar => 2}, "FOO", "BAR"]; $struct->{foo}; # same as $struct->[1], i.e. "FOO" $struct->{bar}; # same as $struct->[2], i.e. "BAR" keys %$struct; # will return ("foo", "bar") in some order values %$struct; # will return ("FOO", "BAR") in same some order while (my($k,$v) = each %$struct) { print "$k => $v\n"; }

- Zoogie

Replies are listed 'Best First'.
RE: RE: RE: My views on pseudohashes
by Adam (Vicar) on Jun 24, 2000 at 03:17 UTC
    That's pretty cool. I wonder what it's good for.
      This is just a guess (perhaps somebody who's more experienced with Perl compiler optimizations can help out here). Since accessing array elements tends to be faster than accessing hash elements, if the Perl compiler resolves the pseudo-hash keys to the array indexes at compile-time, it makes building structures with named elements more efficient at run-time than simply using hashes.

      The perlref page notes that the compiler does something to this effect for using named fields in objects. I'm not at all familiar with objects in Perl, so I can't really elaborate on that.

      - Zoogie

        Array access is faster than hash access. Yes, with a good hashing algorithm, access is O(1), but it'll never be as fast as array index access. You still have to pay the overhead of the hashing algorithm. There's also the increased memory requirement.

        Of course, with arrays you either use numbers for indexes or constants. That's a bit of trouble to go to either way, and with constants you still have some memory overhead.

        Let's not forget autovivification of hash keys. That could bite you when you least expect it.

        The pseudo-hash tries to solve all of those. Yes, it's a little more complex than your average array, but you can access it either way, and it's faster than a hash, doesn't autovivify keys, and it allows you to refer to values by name instead of by index.

        See perlref for more information, as well as the documentation for (I learned about them from Object Oriented Perl, a fine book.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://19654]
[atcroft]: james28909: No, unless you are a politician, I wouldn't say that (and even if you are, I still probably wouldn't say that).
[james28909]: i mean how hard can it be? its just subtracting days lol
[atcroft]: james28909: What about October 5, 1582?
[stevieb]: atcroft: "Make both hands into fists..."... is something my Ma taught me in our native lang, but I was to ignorant and young to pay attention. Thanks for that :)
[atcroft]: stevieb: Sad to say that I only recently learned that particular trick, but I have since found it very useful.... :)
[james28909]: ill be back with a solution eventually
[stevieb]: it's a reminder to re-inforce it :P
[atcroft]: james28909: That particular questions was a bit of trick, actually (depending on the country you are in). More interesting is, if you are trying to subtract from an epoch time, for instance, you might have to consider when/if DST occurs for a location,
[atcroft]: because you may have to adjust the number of seconds you change from an epoch from 86400 (not to mention leap seconds)....

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2017-04-29 04:35 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (531 votes). Check out past polls.