Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Getting rid of a temporary variable in AoH

by AnomalousMonk (Monsignor)
on Jun 12, 2013 at 02:58 UTC ( #1038376=note: print w/ replies, xml ) Need Help??


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 => "!\@\$%^&" }, ]


Comment on Re^2: Getting rid of a temporary variable in AoH
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1038376]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2014-08-23 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (174 votes), past polls