Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Getting rid of a temporary variable in AoH

by hbm (Hermit)
on Jun 11, 2013 at 14:56 UTC ( #1038271=note: print w/replies, xml ) Need Help??


in reply to Getting rid of a temporary variable in AoH

Interesting, I was also thinking of yesterday's "splice/mesh" discussion. (I wouldn't really do it this way.)

But do notice in your RE, I changed delimiter and did not escape forward slashes, commas or hyphens.

use strict; use warnings; use List::MoreUtils qw(mesh); use Data::Dumper; my @users; my @fields = qw(login uid gid name home shell); while (<DATA>) { m#^\s*([\w-]+):x:(\d+):(\d+):([\w\s\(\),-]*):([\w/]+):([\w/]*)$# or die "Malformed input [$_]"; push @users, { mesh @fields, @{[//]} }; } print Dumper \@users; __DATA__ root:x:0:0::/:/usr/bin/ksh paul:x:201:1::/home/paul:/usr/bin/ksh jdoe:x:202:1:John Doe:/home/jdoe:/usr/bin/ksh

Update: The parens don't need to be escaped either:

m#^\s*([\w-]+):x:(\d+):(\d+):([\w\s(),-]*):([\w/]+):([\w/]*)$#

Replies are listed 'Best First'.
Re^2: Getting rid of a temporary variable in AoH
by Arunbear (Prior) on Jun 11, 2013 at 15:53 UTC
    Or using named captures:
    use strict; use warnings; use Data::Dumper; my @users; while (<DATA>) { m#^\s*(?<login>[\w-]+) :x :(?<uid>\d+) :(?<gid>\d+) :(?<name>[\w\s(),-]*) :(?<home>[\w/]+) :(?<shell>[\w/]*)$#x or die "Malformed input [$_]"; push @users, { %+ }; } print Dumper \@users; __DATA__ root:x:0:0::/:/usr/bin/ksh paul:x:201:1::/home/paul:/usr/bin/ksh jdoe:x:202:1:John Doe:/home/jdoe:/usr/bin/ksh
Re^2: Getting rid of a temporary variable in AoH
by hdb (Monsignor) on Jun 11, 2013 at 15:14 UTC

    If the format of the input is as fixed as in case of a passwd file, then splitting on colon makes much more sense, even if one needs to chomp in that case.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2021-09-24 09:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?