Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: quickly create reference to boring hash.

by starbolin (Hermit)
on May 14, 2008 at 02:06 UTC ( [id://686431]=note: print w/replies, xml ) Need Help??


in reply to quickly create reference to boring hash.

A hash would not appear to be the ideal data structure to contain your transition tables. A hash contains a lot of magic behind the scenes to insure that each key maps to exactly one container in the internal structure. A precaution which your application expressly does not need. A hash also has to work over a wide population of keys and insure that the population of keys is distributed evenly over the internal storage. Your, admittedly simple, example has a fixed and small key range of a..z which could be stored sequentially.

It would seem from your example code that needs would be served by a simple array with a symbolic index. Perl supplies something called a pseudo-hash though I don't know how efficient it is. The module Hash::Util allows a similar functionality with "locked" hashes.

#!/usr/bin/perl # this code build a table of array-refs keyed to alphabetic # characters. The arrays could be used to hold state transistions. require 5.8.8; use strict; use fields; use Data::Dumper; my $inputs = fields::phash([('a'..'z')], [ map { [1,1] } ('a'..'z')]); $inputs->{c} = [ 0, 1 ]; $inputs->{f} = [ 2, 1 ]; $inputs->{g} = [ 0, 1 ]; print Dumper map { $inputs->{$_} } ( 'a'..'g' ); __END__ Troll [127] as maint ~/ %perl src/symarray.pl + [6:40pm] $VAR1 = [ 1, 1 ]; $VAR2 = [ 1, 1 ]; $VAR3 = [ 0, 1 ]; $VAR4 = [ 1, 1 ]; $VAR5 = [ 1, 1 ]; $VAR6 = [ 2, 1 ]; $VAR7 = [ 0, 1 ];

I'm not sure if this is as efficient as I would like. Question for perlmonks; Does Perl actually create an array here or otherwise optimize or do I still suffer from the overhead of hash lookups?


s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}

Replies are listed 'Best First'.
Re^2: quickly create reference to boring hash.
by starbolin (Hermit) on May 14, 2008 at 04:06 UTC

    Except my previous post is wrong. Perl hashes are so dang slick that they beat all my hackery with arrays and pseudo-hashes. In deference to those who say CPU cycles DO NOT MATTER!, the difference is small enough that the OP could chose the method that is the most readable.


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
      Pseudo-hashes have been deprecated; they never worked really well in the first place, and don't work at all in newer Perls. Using them will lock your code into a particular version of Perl, which is of course inadvisable.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://686431]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-03-28 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found