in reply to list of hashrefs?
Or:while (<DATA>) { chomp; my @fields = split(/$sep/, $_); my %data; @data{@fieldnames} = @fields; push @datalist, %data; }
I'd choose the first, as it's sometimes tricky to get at lists from hash references, and it's more clear. I would get rid of @fields, though, as it's a synthetic variable.while (<DATA>) { chomp; my $data = {}; @$data{@fieldnames} = split(/$sep/, $_); push @datalist, $data; }
The trick here is making sure you have a new lexical %data each time. Just a guess, but it's bitten me a few times.
Update: Yes, the push line in the first example should use \%data. That'll teach me to get up early.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: list of hashrefs?
by howie (Sexton) on Jan 31, 2001 at 21:18 UTC |