[Lvlug] last chance to drink beer with perl programmers

Ricardo SIGNES rjbs-lvlug at lists.manxome.org
Wed Sep 21 23:24:03 EDT 2005


* Mark <mstanley at technologist.com> [2005-09-21T22:36:32]
> On Wednesday 21 September 2005 05:43 pm, Ricardo SIGNES wrote:
> > Well, not your last chance, but short re-notice: I'm heading over to
> > J.P. McGrady's on 3rd and Mechanics in Bethlehem tonight, and will be
> > there from about 18:30 to about 21:00 or so.  Come by!
> 
> Well, after our little meeting all I could think about was that 'map' command 
> and it's associated hash algorithm.  I'm curious how the hash works with the 
> 'associative mapping' that you mentioned?  When it hashes the table does it 
> use the hash as a 'key' to associate the table items?

Well, let me first just clear this up a little:

map takes as its arguments a block of code and a list of values.  It
returns the result of applying the code to each value in turn.  There is
no associate array required.

	@foo = map { $_ + 10 } (0, 1, 10, 33);

Now @foo is (10, 11, 20, 43)

Associative arrays are known in some languages (like Python) as
dictionaries, or in some other languages (like Perl or Ruby) as hashes.
They're called hashes because a hashing algorithm is used to implement
the data type.

A hash in Perl might be defined like this:

	my %number_for = (
		police   => 911,
		operator => 0,
		john_doe => 610_555_1029,
	);

Later on, maybe:

	$number_for{information} = 411;

	my $who_to_call = prompt("Dial whom? ");

	if (exists $number{$who_to_call}) {
		print "Now calling: $number{$who_to_call} !\n";
	} else {
		die "don't know number for $who_to_call";
	}

-- 
rjbs
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://www.thelinuxlink.net/pipermail/lvlug/attachments/20050922/c2cb3ace/attachment.bin


More information about the Lvlug mailing list