(e-mail address removed)
says...
Ben Bacarisse said:
I have a plain text file with each line in the format:
Start of line followed immediately by a string of character(s), a
whitespace, another string, a newline.
-------- file.txt -------
SOMESTRING XXX
SOMESTRING ZZZ
SOMEOTHERSTRING YYYZZ23
DIFFERENTSTRING HELLO
This can be implemented using a very simple, clear on-liner in
awk, right from your shell prompt.
The lines marked <- are my tty input; the others are awk output:
$ awk '{ if (! ($1 in seen)) { print $0 ; seen[$1] } }'
There are one-line Perl versions as well of course. Maybe
perl -ane '$seen{$F[0]} = print unless $seen{$F[0]}'
That's a neat idea. Obvious extension of that:
perl -ane '$seen{$F[0]} //= print'
I've written many untilities and tools in Perl and I don't understand
these one liners at all...
I can't speak for other people's motives, but for me, I tend to see
Perl one-liners as humor, in most cases. Occasionally there's a
clever technique that's useful and maintainable, and of course I'm
excluding simple education in powerful features that I didn't know
about (e.g., s///r is relatively new). But in most cases, I consider
it to be humor, and of course showing off one's Perl l33t skyllZ (or
however the hep cats express it to-day).
But I suggest that you try to decrypt them, just as a learning
exercise. If there are specific points that still confuse you, please
ask about them here.
"man perlrun" on most systems explains the command line. "perl -p -e"
is something I use occasionally; "perl -pie" even less often; I've
never had to use "perl -a".
This subthread have used the fact that "print" is a function that
returns true if the printing succeeded, which it really ought to do.
"perldoc -f print" should give you its docco.
"//" is a newish operator: "man perlop". "||" would have worked just
as well in this case, I think -- the return values of print on my
system appear to be 1 and undef.