Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

My views on pseudohashes

by vroom (Pope)
on Jun 23, 2000 at 22:01 UTC ( #19636=poll: print w/ replies, xml ) Need Help??

vote on My views on pseudohashes

Used em
[bar] 8/4%
Know what they are and approve of their use
[bar] 15/7%
Know what they are and think they're evil and ugly
[bar] 17/8%
I've heard the term
[bar] 32/15%
What?
[bar] 146/67%
218 total votes
Comment on My views on pseudohashes
RE: My views on pseudohashes
by jrsmith (Pilgrim) on Jun 23, 2000 at 22:40 UTC
    what exactly is a pseudohash?
      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

        That's pretty cool. I wonder what it's good for.
RE: My views on pseudohashes
by Jonathan (Curate) on Jun 26, 2000 at 11:08 UTC
    The only thing I miss from 'C' is structures. As perl has data containers as opposed to data types, pseudo-hashes are a very neat way to help build you own structures
RE: My views on pseudohashes
by Kevman (Pilgrim) on Jun 26, 2000 at 16:31 UTC
    I'm making a pseudo-hash out of my life?
    This what you mean?
RE: My views on pseudohashes
by extremely (Priest) on Jun 28, 2000 at 02:05 UTC
    Pseudo-hash == evil nightmare. You are just asking for trouble using them. The occasional benefit of looking up things in an ordered list using hash like names isn't worth the nightmare that maintaining the code becomes. After explaining that @var is the list and $var[] is the item in a list, explain to a new programmer that $var{} is ALSO an item in the list SOMETIMES but is usually an item in a vaguely related hash. You wind up telling people about 'glob's and symbol tables when they still aren't sure when $_ is able to be used. Bleagh, bleagh, bleagh.

    --
    friend: Can `perl -e "print 'Hello, World!';"` be simplified?
    me: What? How? I guess you could make it less formal. Like "Hi, folks." or something. Gah.

      am I missing something .. aren't pseudo-hashes and anonymous arrays the same thing ?
        No they aren't. An anonymous array is an array without a formal name. Like:
        my $q = [ $a, $b, $c, $d];
        returns an anonymous array ref into $q, you created an array without ever declaring an named array like with `my'.

        A pseudo-hash is an array (anonymous or not) that has a special first entry. Pseudo-hashes are still listed in `perldoc perlref' as experimental. try this:

        my @ph; $ph[0]={Foo=>1, Bar=>2, Bag=>3}; $ph[1]="what Foo points to"; $ph[2]="what Bar points to"; $ph[3]="what Bag points to"; print "$ph{Foo} is $ph[1]\n";

        In effect making the first item in an array a hash where each key points to a row index lets you use the array like a hash. All you favorite hash tricks work, `keys', `values', `exists' and more. The real win here is that you have an intrisic order to the hash thanks to the array, but the hash tools won't naturally return in that order, you have to do it yourself. Also, you can't add a new field with a simple `$ph{New}="a new item"' cause it doesn't know how to do it.

        If you decide to play with em, PLEASE read the `perldoc perlref' section on it. There are some handy-dandy tools in perl 5.6 like `perldoc fields' that will add some compile time sanity and ease of use to them.

        HTH somebody. =)

        -- mark

      Yes, writing maintainable code is very important, and it is not a task that perl makes easy, however it is not a reason to discard half the tools in the tool box. In the event that your code starts to get to difficult for some one new to understand, include a couple helpful comments, and if they still can't get it then maybe they aren't ready to work and that project yet and should be put to work on something simpler until they are ready. You can't expect a freshman year physics major to maintain a nuclear power plant.
      The real win in using pseudohashes is when creating and using objects. As an example, if I create an object based on a hash:
      package hashobject; sub new { my ($class) = @_; $class = ref($class) || $class; my $self = {}; $self->{variable} = 'foo'; return(bless($self, $class)); }
      and then use the resulting object, I can still autovivify member variables, leading to a whole raft of typo problems:
      my $obj = new hashobject; $obj->{variablee} = 'bar'; # TYPO... print $obj->{variable}; # Prints 'foo'
      If we move to a pseudohash, the object looks like this:
      package pseudohashobject; use fields qw(variable); sub new { my ($class) = @_; $class = ref($class) || $class; my $self = bless([\%FIELDS], $class]); $self->{variable} = 'foo'; return($self); }
      And then (mis)use the object in the same way:
      my $obj = new pseudohashobject; $obj->{variablee} = 'bar'; # TYPO print $obj->{variable};
      the typo actually causes a runtime exception! This makes very hard to find bugs easy to find, and hence is a good thing (tm). I would suggest looking into Damien Conway's Object Oriented Perl (Manning) for more information. I would almost rate this book as more important than the Camel!
        Hi,

        I'm developing an application that creates a binary tree of objects.
        Previously they were all hash-based, but using pseudohashes has sped up processing by about 3 times!

        Having to use "no strict 'refs';" does worry me a bit though...

        Are pseudohashes really implemented in perl, or just a hack? will they be supported in future versions?

View List Of Past Polls


Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2014-12-21 06:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (104 votes), past polls