U
usenet
Kindly consider this sample code, if you will, which illustrates my
question. This code works just fine and does exactly what I want,
but... I dunno... I just don't like the approach I've taken. At first,
I thought to approach this with a split() (using a limit of 1) instead
of a regexp, but I couldn't figure out how to make that work in
anything other than a convoluted manner. I'm interested in maybe
learning different techniques from others who may approach the task
differently.
#!/usr/bin/perl
use strict; use warnings;
my %user; # keys are userid's, values are names
while (my $line = <DATA>) {
$line =~ m{^(\w+) +(.*)$} and $user{$1} = $2;
}
print map { "'$_'\t=>\t'$user{$_}'\n" } sort keys %user;
__DATA__
fredf Fred Flintstone
Barn Barney Rubble
bogus
WF Wilma Flintstone
betty Betty Rubble
question. This code works just fine and does exactly what I want,
but... I dunno... I just don't like the approach I've taken. At first,
I thought to approach this with a split() (using a limit of 1) instead
of a regexp, but I couldn't figure out how to make that work in
anything other than a convoluted manner. I'm interested in maybe
learning different techniques from others who may approach the task
differently.
#!/usr/bin/perl
use strict; use warnings;
my %user; # keys are userid's, values are names
while (my $line = <DATA>) {
$line =~ m{^(\w+) +(.*)$} and $user{$1} = $2;
}
print map { "'$_'\t=>\t'$user{$_}'\n" } sort keys %user;
__DATA__
fredf Fred Flintstone
Barn Barney Rubble
bogus
WF Wilma Flintstone
betty Betty Rubble