in reply to list of hashrefs?

I'd have to see more of your code to be sure, but if you're using a global %data, you'll definitely run into clobbering problems, because you're assigning a reference to the *same* hash each time. You can take two approaches:
while (<DATA>) { chomp; my @fields = split(/$sep/, $_); my %data; @data{@fieldnames} = @fields; push @datalist, %data; }
Or:
while (<DATA>) { chomp; my $data = {}; @$data{@fieldnames} = split(/$sep/, $_); 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.

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
    Thanks! I'd realised I was reusing the same hash with a little Data::Dumper experimenting, but I couldn't figure out how to 'detach' the current hash - the lexical %data does that.

    That all said, I just tried both of your suggestions, and the first leaves me with a single list of junk, while the second does the deal. %data in a list context like push just dumps out the contents of the hash as field-value pairs, doesn't it? So wouldn't that be \%data ? (just tested, and that works)