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.