in reply to Re: Passing an array of hashes to a subroutine
in thread Passing an array of hashes to a subroutine

Fix:

my @array; { my %hash; ... push @array, \%hash; } { my %hash; ... push @array, \%hash; }

Or:

my @array; my %hash; # ... push @array, { %hash }; # ... push @array, { %hash };

Replies are listed 'Best First'.
Re^3: Passing an array of hashes to a subroutine
by ikegami (Patriarch) on May 22, 2007 at 05:29 UTC

    No, not quite. You would need to add something to clear the hash after it's pushed.

    my @array; my %hash; # ... push @array, { %hash }; %hash = (); # ... push @array, { %hash };

    But I don't like it. Since the natural progression is a loop, you've just advocated the following:

    my @array; my %hash; for (...) { %hash = (); # ... push @array, { %hash }; }

    I much prefer the following:

    my @array; for (...) { my %hash; # ... push @array, { %hash }; }
      No, not quite. You would need to add something to clear the hash after it's pushed.

      Not necessarily: one may want to clear it, or to modify some entries. Who knows?

      my @array; my %hash=(foo => 2, bar =>3); for (1..4) { push @array, {%hash}; %hash=(%hash, map { +"$_^2" => $hash{$_}**2 } keys %hash); }
      Since the natural progression is a loop, you've just advocated the following:

      I hadn't understood we were talking about a loop, in which case you're perfectly right, and I never imagined of having advocate what you claim I did.