http://www.perlmonks.org?node_id=1038376


in reply to Re: Getting rid of a temporary variable in AoH
in thread Getting rid of a temporary variable in AoH

A variation (imagine the  my $datum = shift @data is reading a file; no need to create  @fields array each time through loop; state only available with 5.10+; either version works the same):

>perl -wMstrict -MData::Dump -lE "my @data = ( 'foo 123 %^&', 'huggle 9876 @*', 'ab 55555 !@$%^&', ); ;; my @users; while (my $datum = shift @data) { # use constant FIELDS => qw( alphas digits stuff) +; state $fields = [ qw( alphas digits stuff) ] +; my @items = $datum =~ m{ ([[:alpha:]]+) \s+ (\d+) \s+ ([^[:alpha:]\ +d]+) }xms; # @{ $users[@users] }{ FIELDS() } = @items; @{ $users[@users] }{ @$fields } = @items; } ;; dd \@users; " [ { alphas => "foo", digits => 123, stuff => "%^&" }, { alphas => "huggle", digits => 9876, stuff => "\@*" }, { alphas => "ab", digits => 55555, stuff => "!\@\$%^&" }, ]