Vahid Moghaddasi wrote:
Hi,
Hello,
I have a very strange problem (to me anyways). I have the following subroutine to sort unique a UNIX password file based on UID and username. But the problem is that some of the users get disappeared the output password file. I couldn't figure out the pattern of user disappearance but always the same few users are filtering. Can someone please take a look at my code and tell me why is it messing with me?
In the Unix password file format the user name has to be unique however the UID need not be so it is possible that two or more users have the same UID.
Any suggestion to rewrite it is also welcome. Thank you all. sub Sort { my ($infile,$outfile) = @_; my @unique = (); my %seen = ();
my() creates lexical variables that are already empty by default so assigning nothing to them is pointless.
open my $tmp_out, '>', "$outfile" or die "could not write the sorted list: $!"; open my $tmp_in, '<', "$infile" or die "could not open $infile to sort: $!";
perldoc -q quoting Found in /usr/share/perl/5.8/pod/perlfaq4.pod What’s wrong with always quoting "$vars"?
while (<$tmp_in>) { next if (m/^#/); # Skip comments next if (m/^\s*$/); # Skip blank lines my $uname = (split /:/)[0]; # username my $uid = (split /:/)[2]; # uid
Instead of split()ting twice you can extract both fields with one split(): my ( $uname, $uid ) = ( split /:/ )[ 0, 2 ];
next if $seen{ $uid }++; next if $seen{ $uname }++; push @unique, $_; } print $tmp_out @unique; print "DEBUG: Finishing Sort function.\n" if $DEBUG; }
If you are reading from *the* /etc/passwd file then you could use perl's built-in getpwent()/setpwent()/endpwent()/getpwnam()/getpwuid() functions.
John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/