Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

fun with hashes!
too long has this list been quiet. but i have a fun question. i am
writing up slides for an all about hashes class. one goal i have is to
list and explain as many different uses for hashes as reasonably
possible. i have a short list to start with but i am sure the hive mind
of fwp can scrape up a bunch more. each one should have a name and not
overlap too much with any of this list (my initial list should be
obvious to you).

dispatch table

call code ref based on a key

isa
is a key valid? (not same as in a set). ISA usually works on
fixed sets of keys

my @foos = qw( axxj djdj whwh ) ;
my %is_a_foo = map { $_ => 1 } @foos

mapping/conversion

short month to long month

records

single row from a db or similar

data structures

hash refs are the core to most deeper data structures

sets

set of active objects in a class. plenty of other examples

sparse arrays

saves major space when you have very few entries in a
very large array.

thanx,

uri

--
Uri Guttman  ------  uri@stemsystems.com  -------- [url]http://www.stemsystems.com[/url
]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding
-
Search or Offer Perl Jobs  ----------------------------  [url]http://jobs.perl.org[/url
]

Report this thread to moderator Post Follow-up to this message
Old Post
Uri Guttman
11-24-07 12:29 AM


Re: fun with hashes!
On Fri, Nov 23, 2007 at 01:12:37PM -0500, Uri Guttman wrote:
> 	isa
> 		is a key valid? (not same as in a set). ISA usually works on
> 		fixed sets of keys
>
> 		my @foos = qw( axxj djdj whwh ) ;
> 		my %is_a_foo = map { $_ => 1 } @foos

I don't have any additional uses for hashes to add, but I do have a
great hash initializer I picked up from Damian when he was in town a
few years ago (borrowing from your example):

my @foos = qw( axxj djdj whwh ) ;
my %is_a_foo;
@is_a_foo{@foos} = (1) x @foos;

It runs *way* faster than the $_ aliasing does in the map (at least by
my benches). I thought that was fun when I saw it.

Scott
--
Scott Wiersdorf
scott@ipartner.net

Report this thread to moderator Post Follow-up to this message
Old Post
Scott Wiersdorf
11-24-07 12:29 AM


Re: fun with hashes!
Uri Guttman wrote:
> too long has this list been quiet. but i have a fun question. i am
> writing up slides for an all about hashes class. one goal i have is to
> list and explain as many different uses for hashes as reasonably
> possible. i have a short list to start with but i am sure the hive mind
> of fwp can scrape up a bunch more. each one should have a name and not
> overlap too much with any of this list (my initial list should be
> obvious to you).

#!/usr/bin/perl

use strict;
use warnings;

# Unqiueness
{
my @list = qw( a b c  x y z a b c );
my %unique = ();
@unique{@list} = @list;
@list = keys %unique;
print "@list\n";
}

# Counts
{
my @list = qw( a b c  x y z a b c );
my %counts = ();
$counts{$_} ++ for @list;
print "$_ : $counts{$_}\n" for keys %counts;
}

__END__

--
Just my 0.00000002 million dollars worth,
Shawn

+------------\
| Shangri La  \
| 40,000 km   /
+------------/

Report this thread to moderator Post Follow-up to this message
Old Post
Mr. Shawn H. Corey
11-24-07 12:29 AM


Re: fun with hashes!
on Friday, Nov 23rd 2007 at 13:12 -0000, quoth Uri Guttman:

=>
=>too long has this list been quiet. but i have a fun question. i am
=>writing up slides for an all about hashes class. one goal i have is to
=>list and explain as many different uses for hashes as reasonably
=>possible. i have a short list to start with but i am sure the hive mind
=>of fwp can scrape up a bunch more. each one should have a name and not
=>overlap too much with any of this list (my initial list should be
=>obvious to you).
=>
=>	dispatch table
=>
=>		call code ref based on a key
=>

I admit I'm better with python these days than I am with perl, but one of
the caveats that the dispatch table should be taught with is that
selecting the correct code ref is different from calling through it.

Given:
table = { ... }

eg
ref = table{ expr }
ref()

is different from
table{expr}()

The latter can be a bear to debug ;-)

--
Time flies like the wind. Fruit flies like a banana. Stranger things have  .
0.
happened but none stranger than this. Does your driver's license say Organ .
.0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 0
00
individuals! What if this weren't a hypothetical question?
steveo at syslang.net

Report this thread to moderator Post Follow-up to this message
Old Post
Steven W. Orr
11-24-07 12:29 AM


Re: fun with hashes!
>I admit I'm better with python these days than I am with perl, but one of
>the caveats that the dispatch table should be taught with is that
>selecting the correct code ref is different from calling through it.

You might want to explain yourself in fuller, properly punctuated, detail th
ere
(particularly since it seems to not be about hashes per se)
--
Free map of local environmental resources: http://CambridgeMA.GreenMap.org
--
MOTD on Boomtime, the 35th of The Aftermath, in the YOLD 3173:
You don't need a Swiss Army knife to cut yourself, a piece of paper will do 
just fine. --JP

Report this thread to moderator Post Follow-up to this message
Old Post
Jerrad Pierce
11-24-07 12:29 AM


Re: fun with hashes!
>>>>> "SW" == Scott Wiersdorf <scott@ipartner.net> writes:

SW> I don't have any additional uses for hashes to add, but I do have a
SW> great hash initializer I picked up from Damian when he was in town a
SW> few years ago (borrowing from your example):

SW>   my @foos = qw( axxj djdj whwh ) ;
SW>   my %is_a_foo;
SW>   @is_a_foo{@foos} = (1) x @foos;

very old and i covered it in my hash slice tutorial:

http://sysarch.com/Perl/hash_slice.txt

SW> It runs *way* faster than the $_ aliasing does in the map (at least by
SW> my benches). I thought that was fun when I saw it.

even faster is

@is_a_foo{@foos} = () ;

and then use exists() to test instead of truth.

uri

--
Uri Guttman  ------  uri@stemsystems.com  -------- [url]http://www.stemsystems.com[/url
]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding
-
Search or Offer Perl Jobs  ----------------------------  [url]http://jobs.perl.org[/url
]

Report this thread to moderator Post Follow-up to this message
Old Post
Uri Guttman
11-24-07 12:29 AM


Re: fun with hashes!
>>>>> "SHC" == Shawn H Corey <shawnhcorey@magma.ca> writes:

SHC> # Unqiueness
SHC> {
SHC>   my @list = qw( a b c  x y z a b c );
SHC>   my %unique = ();
SHC>   @unique{@list} = @list;
SHC>   @list = keys %unique;
SHC>   print "@list\n";
SHC> }

SHC> # Counts
SHC> {
SHC>   my @list = qw( a b c  x y z a b c );
SHC>   my %counts = ();
SHC>   $counts{$_} ++ for @list;
SHC>   print "$_ : $counts{$_}\n" for keys %counts;
SHC> }

both good ones that i should have remembered. i will add them to the
list.

thanx,

uri

--
Uri Guttman  ------  uri@stemsystems.com  -------- [url]http://www.stemsystems.com[/url
]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding
-
Search or Offer Perl Jobs  ----------------------------  [url]http://jobs.perl.org[/url
]

Report this thread to moderator Post Follow-up to this message
Old Post
Uri Guttman
11-24-07 12:29 AM


Re: fun with hashes!
>>>>> "AP" == A Pagaltzis <pagaltzis@gmx.de> writes:

AP> * Jerrad Pierce <belg4mit@MIT.EDU> [2007-11-23 22:50]: 

AP>     ( $dispatch{$sub} || sub { warn "no such action '$sub'" } )->();

some variations on that:

my $sub = $dispatch{$key} or die "trying to call missing code" ;
$sub->() ;

or:

my %dispatch = (
foo	=> \&foo,
..
default => \&default
) ;

$key = 'default' unless exists( $dispatch{ $key } ;
$sub = $dispatch{ $key } ;

or:
my $sub = $dispatch{ $dispatch{ $key } ?
$key : $dispatch{ 'default' } } ;

or:

my $sub = $dispatch{ $key } || $dispatch{ 'default' } ;


as you can see there are various ways to handle a missing dispatch
key. this is not a hash issue but a detail about doing dispatch tables
cleanly. sometimes you want to predefine a default code ref in the
dispatch table and other times you want to handle the missing key when
you make the call through the dispatch table.

uri

--
Uri Guttman  ------  uri@stemsystems.com  -------- [url]http://www.stemsystems.com[/url
]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding
-
Search or Offer Perl Jobs  ----------------------------  [url]http://jobs.perl.org[/url
]

Report this thread to moderator Post Follow-up to this message
Old Post
Uri Guttman
11-24-07 12:29 AM


Re: fun with hashes!
Steven W. Orr wrote:
> On Friday, Nov 23rd 2007 at 14:53 -0000, quoth Jerrad Pierce:
>
> =>>I admit I'm better with python these days than I am with perl, but one 
of
> =>>the caveats that the dispatch table should be taught with is that
> =>>selecting the correct code ref is different from calling through it.
> =>
> =>You might want to explain yourself in fuller, properly punctuated,
> =>detail there (particularly since it seems to not be about hashes per se)
>
> Sorry, I thought I was clear and properly punctuated.
>
> Given:
>         table = { ... }

Your example would be clearer if writing in Perl.  Do you mean?

%table = (
foo => \&foo,
# ...
);

Or?

$table = {
foo => \&foo,
# ...
};


--
Just my 0.00000002 million dollars worth,
Shawn

+------------\
| Shangri La  \
| 40,000 km   /
+------------/

Report this thread to moderator Post Follow-up to this message
Old Post
Mr. Shawn H. Corey
11-24-07 12:29 AM


Re: fun with hashes!
Uri Guttman wrote: 
>
>   AP> * Jerrad Pierce <belg4mit@MIT.EDU> [2007-11-23 22:50]: 
>
>   AP>     ( $dispatch{$sub} || sub { warn "no such action '$sub'" } )->();
>
> some variations on that:
>
> 	my $sub = $dispatch{$key} or die "trying to call missing code" ;
> 	$sub->() ;
>
> or:
>
> 	my %dispatch = (
> 		foo	=> \&foo,
> 		...
> 		default => \&default
> 	) ;
>
> 	$key = 'default' unless exists( $dispatch{ $key } ;
> 	$sub = $dispatch{ $key } ;
>
> or:
> 	my $sub = $dispatch{ $dispatch{ $key } ?
> 			$key : $dispatch{ 'default' } } ;

Shouldn't this be?

my $sub = $dispatch{ $dispatch{ $key }
? $key
: 'default'
};


>
> or:
>
> 	my $sub = $dispatch{ $key } || $dispatch{ 'default' } ;
>
>
> as you can see there are various ways to handle a missing dispatch
> key. this is not a hash issue but a detail about doing dispatch tables
> cleanly. sometimes you want to predefine a default code ref in the
> dispatch table and other times you want to handle the missing key when
> you make the call through the dispatch table.
>
> uri
>

Or:

my $sub = ( exists $dispatch{ $key } && ref( $dispatch{ $key } ) eq 'CODE' )
? $dispatch{ $key }
: $dispatch{ 'default' };

Just because you're not paranoid doesn't mean computers don't hate you :)
--
Just my 0.00000002 million dollars worth,
Shawn

+------------\
| Shangri La  \
| 40,000 km   /
+------------/

Report this thread to moderator Post Follow-up to this message
Old Post
Mr. Shawn H. Corey
11-24-07 12:29 AM


Sponsored Links




Last Thread Next Thread Next
Pages (5): [1] 2 3 4 5 »
Search this forum -> 
Post New Thread

PERL Tricks archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:49 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.