[Israel.pm] Another newbie question - Hashes

Yuval Yaari yuval at windax.com
Tue Feb 3 07:40:18 PST 2004


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 ###

Notice: I am limiting the the first split so lines like:
Key      Value With Spaces
Would work too.
If the file is big (bigger than the memory you have to offer, that is), 
use my first example.

perldoc -f split
perldoc -f map (my favorite Perl built in function...)


  --Yuval




More information about the Perl mailing list