[Israel.pm] Another newbie question - Hashes

Yosef Meller mellerf at netvision.net.il
Tue Feb 3 14:01:29 PST 2004


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Gaal Yahas wrote:
| On Tue, Feb 03, 2004 at 05:40:18PM +0200, Yuval Yaari wrote:
|
|>I tried making my example as basic as possible (a simple question
|>deserves a simple answer).
|>Anyway, here's what I do in real life (as always TMTOWTDI):
|>
|>### BEGIN ###
|>open(FILE, "<file");
|>my $file = do{ local $/; <FILE> };
|>close(FILE);
|>my %hash = map { split(/\s+/, $_, 2) } split (/\n/, $file);
|>### END ###
|
|
| Well, if you're already willing to read the whole file to memory why
| don't you just use <> in list context:
|
|     open my $fh, "filename" or die "$0:open:$!";
|     my %hash = map { split(/\s+/, $_, 2) } <$fh>;
|     close $fh or die "$0:close:$!";      # or simply let $fh go out of
scope
|

This is starting to look like a game of Perl Golf... Here's my try:

open(STDIN,"filename")or die "$0:open:$!";
/^(\S*) (.*)/ && $hash{$1}=$2 for(<>);
close STDIN or die "$0:close:$!";

Mine: 116 strokes.
Yours: 123 strokes.

It's easy to improve upon this, but it's not an answer to a newbee of
course :)

- --
perl -e'$b=unpack"b*",pack"H*","59dfce2d6b1664d3b26cd9969503";
for(;$a<length$b;$a+=9){print+pack"b8",substr$b,$a,8;}'

My public key:
http://wwwkeys.pgp.net:11371/pks/lookup?op=get&search=0x3D2CA0A8
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFAIBo4aElZPz0soKgRAtQCAKCrPt9K3Wg2BihWiUcEyKkER3Ll8QCghGE7
FMTwVmLjttU/Dqtwa3QVMaA=
=kvIw
-----END PGP SIGNATURE-----



More information about the Perl mailing list