Re: perl intersect problem
- From: vahid.moghaddasi@xxxxxxxxx (Vahid Moghaddasi)
- Date: Tue, 22 Jan 2008 22:56:27 -0500
On Jan 22, 2008 9:31 PM, Chas. Owens <chas.owens@xxxxxxxxx> wrote:
Off hand I think that would only happen if the user name or the uid
was the same for some of your users. Can you present a cleaned up
version of your data the exhibits the problem? Also, You might want
to rewrite your loop more compactly and avoid duplicate calls to
Thanks for the code. I used it but have some problems, it prints
GLOB(0x9de5ce4) about 1000 times all in one line.
Shouldn't my code keep one of the duplicate users instead of taking
them both out?
split:
Warning, the following code is untested
#this function does not sort anything so Sort is a bad name
#also Perl functions don't normally have uppercase letters
sub make_passwd_unique {
my ($infile, $outfile) = @_;
#$out and $in are only scoped to this function, there is no need
for the tmp_ prefix
#also, it is handy to print the name of the file that errored
open my $out, '>', "$outfile"
or die "could not write to $out: $!";
open my $in, '<', "$infile"
or die "could not open $infile to sort: $!";
#declare variables close to where they are used, not at the top
my @unique;
my %seen;
while (<$in>) {
#skip comments and blank lines
next if /^#/ || /^\s*$/; #the m is only needed if you
are using a delimiter other than /
my ($uname, $uid) = (split /:/)[0,2]; #use a slice
instead of calling split twice (hey that rhymed!)
#if uid was seen then the uname would never be marked as seen
#this may not be a bug, but if so it should be
documented and you should revert to your old code
$seen{ $uid }++;
$seen{ $uname }++;
next if $seen{uid} > 1 || $seen{$uname} > 1;
print $out; #why save the data and then print it? Print as you go
}
print "DEBUG: Finishing make_passwd_unique function.\n" if $DEBUG;
}
--
This e-mail address is not monitored so please do not send me anything
important here. Thanks.
.
- Follow-Ups:
- Re: perl intersect problem
- From: Chas. Owens
- Re: perl intersect problem
- References:
- perl intersect problem
- From: Vahid Moghaddasi
- Re: perl intersect problem
- From: Chas. Owens
- perl intersect problem
- Prev by Date: Re: about the dot
- Next by Date: Re: perl intersect problem
- Previous by thread: Re: perl intersect problem
- Next by thread: Re: perl intersect problem
- Index(es):
Relevant Pages
|
|