[pm-h] Good use of hashes for our more junior members

Reini Urban rurban at x-ray.at
Thu Nov 21 09:51:22 PST 2013


But remember those quirks:

1. hashes can only hold strings as key (on the left side), so
everything is stringified. subs as hash keys should use the ref to be
unique.

2. namespaces are called stashes ("symbol table hash") and are hashes.
%main::, %main::mypkg::, ...
methods in objects also (subs in the namespace), and mostly even
object values - in slow objects. but you are free to define a
different object value layout, such as arrays.

3. it's pretty hard to protect hashes and hash values by setting them
to readonly. You may want to have readonly packages (stashes) or
objects to be able to get compile-time warnings when you access the
wrong method.

So why is this dangerous?
local ${"$var\::"};


On Thu, Nov 21, 2013 at 10:05 AM, G. Wade Johnson <gwadej at anomaly.org> wrote:
> On Thu, 21 Nov 2013 08:25:27 -0600
> "B. Estrade" <estrabd at gmail.com> wrote:
>
>> Thank you, Wade.
>>
>> I tend to use hashes as a "go to" way to trade time complexity for
>> memory, taking advantage of the "O(1)" look up the hash provides.
>>
>> For example, if I have a complex data structure that will require some
>> iterating to get various bits of information, I will store it in 2 or
>> more ways that provide the look up in more efficient ways. I guess
>> it'd be like a poor man's heap structure. There is overhead in
>> maintaining more than one version of a data object, but if you're
>> needing to optimize for speed and can spare the memory, this is a
>> legitimate thing to do.
>
> I've found this very useful with hierarchical data as well. A
> multidimensional hash as the main data structure and (sometimes) a
> couple more for other slices through the data.
>
>> Another way I will use a hash is to replace a longish if/elsif/else
>> block with a hash based dispatch table.  This eliminate the need for
>> any conditional chaining and allows the proper subroutine of code
>> (that would otherwise be branched into if a condition was met) to be
>> executed immediately. Not only is this much more efficient (O(1)
>> branching versus O(n), where n is the number of conditionals to
>> test), but is makes for much cleaner and easier to read code.
>
> Dispatch tables are one of my favorite uses of hashes. As long as you
> don't use too many anonymous subs (or more importantly too complex
> anonymous subs) in the table, it's definitely easier to read.
>
>> In general, hashes in Perl (due to the fact that the key can point to
>> literally anything) are a really nice tools not just for creating
>> complex data structures, but for optimizing your code on a fairly
>> high level.
>
> Agreed.
>
> The reason I pointed to the article is that it sometimes takes a while
> to realize how many advantages you gain with careful uses of a hash or
> two. More junior Perl programmers need to see.
>
> G. Wade
>
>> Brett
>>
>>
>> On Thu, Nov 21, 2013 at 8:13 AM, G. Wade Johnson <gwadej at anomaly.org>
>> wrote:
>>
>> > I know that everyone who has been programming in Perl for a while
>> > will be aware of this, but some of our members may not have done
>> > this.
>> >
>> > http://perlmaven.com/count-words-in-text-using-perl
>> >
>> > Gabor does a good job of setting up a simple problem and explaining
>> > it well. I have used variations of this code more times than I can
>> > count.
>> >
>> > He really does not go into using regular expressions to extract
>> > other things besides just words (e.g. IP addresses, telephone
>> > numbers), which is fine because he is focusing on the counting
>> > technique.
>> >
>> > There are also a number of things you can do with this hash when you
>> > are finished. The reports he shows are pretty straight-forward, but
>> > graphing the output is not incredibly hard if that's what you need.
>> >
>> > G. Wade
>> > --
>> > DON'T PANIC! I'm a trained professional, and far more qualified to
>> > panic in this situation than you are.
>> > _______________________________________________
>> > Houston mailing list
>> > Houston at pm.org
>> > http://mail.pm.org/mailman/listinfo/houston
>> > Website: http://houston.pm.org/
>> >
>
>
> --
> C++ tries to guard against Murphy, not Machiavelli.
>                                                     -- Damian Conway
> _______________________________________________
> Houston mailing list
> Houston at pm.org
> http://mail.pm.org/mailman/listinfo/houston
> Website: http://houston.pm.org/



-- 
Reini Urban
http://cpanel.net/   http://www.perl-compiler.org/


More information about the Houston mailing list