[Ma-linux] perl hash construction
Przemek Klosowski
przemek.klosowski at nist.gov
Tue May 4 13:53:36 EDT 2010
this has got to be a common Perl use case, but I can't think of a
compact, Perlish way of writing it. I am reading tabular data into a
Perl hash---I want to refer to the data by the field names:
date temp humidity
Mon 75 40
Tue 80 35
Wed 65 55
so I am writing
my $data;
while (<>) {
($date,$temp,$humidity) = split;
$data->{'date'}=$date;
$data->{'temp'}=$temp;
$data->{'humidity'}=$humidity;
# make a hash of hashes, indexed by date, returning the entire record
$record{$date}=$data;
}
Isn't there a way to assign the temporary hash more succintly?
while (<>) {
$data = ...???... ('date','temp','humidity'), split;
# make a hash of hashes, indexed by date, returning the entire record
$record{$data->{'date'}}=$data;
}
More information about the Ma-linux
mailing list