weltyj has asked for the wisdom of the Perl Monks concerning the following question:

I've have an app where the hash keys are all predefined, and remember reading somewhere that you could use that information to essentially make the hash and array, and thus really speed up the access times. But I can't remember the name of the technique (and alas my perl programming book is at home). I've searched all over here and google for 20 minutes, to no avail. Any keywords would be greatly appreciated so I can look up more info.

Replies are listed 'Best First'.
Re: Predefined hash keys
by jasonk (Parson) on Mar 19, 2003 at 17:51 UTC

    I think the term you are looking for is 'pseudo-hash', although it's been deprecated for 5.8.0 and will be removed in 5.10.0... There is a Class::PseudoHash class out there, but I've never used it...


    We're not surrounded, we're in a target-rich environment!
Re: Predefined hash keys
by perrin (Chancellor) on Mar 19, 2003 at 19:04 UTC
    I think you're looking for "enumeration":
    use enum qw(Sun Mon Tue Wed Thu Fri Sat); my @reminders; $reminders[Sat] = 'Brunch with Jenny.'; $reminders[Sun] = 'Read paper.'; $reminders[Mon] = 'Despair.';
    Of course you can do this without the enum module too. You can even just use variables ($Sun, $Mon, etc.) for them.
Re: Predefined hash keys
by hardburn (Abbot) on Mar 19, 2003 at 17:47 UTC

    The answer depends on what format the keys are in right now. Serialized using Data::Dumper? Printed to a flat file in 'name:value' pairs? Something else?

    ----
    Reinvent a rounder wheel.

    Note: All code is untested, unless otherwise stated

      No, it's even simpler than that -- I have a sub in a module, and it does stuff like: -- $h{$other_key}{"ymin"} = yada $h{$other_key}{"ymax"} = big_yada -- So, I know the only 2 secondary keys I'll ever need are "ymin" and "ymax" thanks, jw