On 6/21/07, Vahid Moghaddasi <[EMAIL PROTECTED]> wrote:
On 6/21/07, Chas Owens <[EMAIL PROTECTED]> wrote:
>
> Well, first you don't use awk inside of Perl. This is about as useful
> as riding a bike on a bus.
>
Thanks, that worked very well for me.
Beside the force of habit, awk was the only way I could get the
password file sorted unique and always keep *only* the first
occurrence of the duplicate UID and discard the second duplicate.
Thanks again.
There is a script included in the Perl distribution called a2p. The
next time you are stumped in Perl, but know how to do it in awk, use
it to figure out a way to do it in Perl.
a2p -F:
a[$1]++==0
#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
if $running_under_some_shell;
# this emulates #! processing on NIH machines.
# (remove #! line above if indigestible)
eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
# process any FOO=bar switches
$[ = 1; # set array base to 1
$FS = ':'; # field separator from -F switch
while (<>) {
@Fld = split(/[:\n]/, $_, -1);
print $_ if $a{$Fld[1]}++ == 0;
}
Now, this isn't pretty code, but it can give you a starting point.
The first nine lines are boilerplate and can be safely ignored. The
tenth line is a dangerous thing you should never do. It is being done
here to emulate awk's indexing method, but it will not be necessary in
your Perl code; just remember, Perl starts at zero. The eleventh line
is meaningless in this context (it is only useful if your awk code
referenced $FS at some point). The real meat is in lines thirteen
through sixteen and they are functionally equivalent to the code I
sent. The only real difference is that it is keeping a copy of the
fields for the life of loop in case later code (that does not exist)
wants to look at it.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/