[Ma-linux] perl hash construction

John Holland jbholland at gmail.com
Tue May 4 14:35:22 EDT 2010


Can't perl have multiple values on the left side of an = ? Couldn't you do

($data->{'date'} ,$data->{'temp'}, $data->{'humidity'})= split;

???




On 05/04/2010 01:53 PM, Przemek Klosowski wrote:
> 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;
> }
> _______________________________________________
> Ma-linux mailing list
> Ma-linux at calypso.tux.org
> http://calypso.tux.org/mailman/listinfo/ma-linux
>    



More information about the Ma-linux mailing list