Removing lines containing same first string boundaries?

Discussion in 'Perl Misc' started by Tuxedo, Mar 17, 2014.

  1. Tuxedo

    John Bokma Guest

    Still too easy to read ;-)
    John Bokma, Mar 18, 2014
    1. Advertisements

  2. There are one-line Perl versions as well of course. Maybe

    perl -ane '$seen{$F[0]} = print unless $seen{$F[0]}'

    Ben Bacarisse, Mar 18, 2014
    1. Advertisements

  3. That's a neat idea. Obvious extension of that:

    perl -ane '$seen{$F[0]} //= print'
    Rainer Weikusat, Mar 18, 2014
  4. Tuxedo

    John Black Guest

    I've written many untilities and tools in Perl and I don't understand these one liners at

    John Black
    John Black, Mar 19, 2014
  5. Tuxedo

    Tim McDaniel Guest

    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.
    Tim McDaniel, Mar 19, 2014
  6. I didn't either. Then I've learned I have to practice skills before
    Eric Pozharski, Mar 19, 2014
  7. Tuxedo

    C.DeRykus Guest

    Golfers would whine about an extra stroke:

    perl -lanE '$_{$F[0]} //= say'
    C.DeRykus, Mar 19, 2014
  8. At the expense of some 'loss in generality' (aka 'risk of causing weird
    effects in case of name collisions with Perl special variables'), the
    symbol table already provides a perfectly usable hash:

    perl -ane '${$F[0]} //= print'
    Rainer Weikusat, Mar 19, 2014
  9. Tuxedo

    John Bokma Guest

    Indeed, thanks.
    John Bokma, Mar 19, 2014
  10. Tuxedo

    Kaz Kylheku Guest

    Note that this symbol table hack has obvious flaws: namely that you don't own
    the symbol space, and that space has content already:

    Here is an obvious failing test case for the perl:

    $ perl -lanE '${$F[0]}//=say'
    _ foo

    Some other nonworking test cases:

    ( foo
    ? foo
    $ foo

    The output is blank instead of the expected _ foo. The fix is to properly
    namespace the keys so that they don't clash with existing symbols; but the
    broken solution is already over par for the course as it is:

    perl -lanE '${$F[0]}//=say'

    awk '{if(!s[$1]++)print;}'

    Also, this is almost normal, everyday awk, representing how that tool is
    typically used: all that is lacking is whitespace and a meaningful name for the
    hash instead of s.
    Kaz Kylheku, Mar 19, 2014
  11. "At the expense of some 'loss in generality' (aka 'risk of
    causing weird effects in case of name collisions with Perl
    special variables'),"

    A more interesting test case would be

    \ bla

    (when using print instead of say).

    awk '!s[$1]--'

    (tested with gawk)

    But this is sort of off topic in a Perl news group. Also,

    $_{$F[0]}//=print # [*]

    is really a nicer algorithm because it doesn't do anything if the test

    [*] I don't particularly like the idea of using a special command-line
    option telling perl to strip newlines on input just because this means
    that a builtin which unconditionally adds a newlines can be used.
    Rainer Weikusat, Mar 19, 2014
  12. Tuxedo

    Kaz Kylheku Guest

    We have a winner! :)

    How easy to forget that a lone expression means "print $0 if I am true".
    Kaz Kylheku, Mar 20, 2014
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.