abdan has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Perl using $seen with regex (updated)
by AnomalousMonk (Archbishop) on Jul 18, 2022 at 02:27 UTC

    I don't understand what you're trying to do. Note that according to perlrun, the -a autosplit switch can only be used with the -n or -p switches. (Update: The fact that you have what looks like a file name at the end of the OPed pseudo-code suggests you intended to use a -n or -p switch.)

    You seem to be trying to use the ! $seen{ $string }++ idiom. Maybe the following will help clarify the situation although it doesn't actually use the autosplit feature (but neither does the OPed (pseudo)code).

    Win8 Strawberry 5.30.3.1 (64) Sun 07/17/2022 22:13:06 C:\@Work\Perl\monks\abdan >cat story.rst seen first the first found found second seen second first occurred occurred second >perl -ane "print if /(seen|found|occurred)/ && !$seen{ $1 }++" story. +rst seen first the first found first occurred


    Give a man a fish:  <%-{-{-{-<

Re: Perl using $seen with regex
by hippo (Archbishop) on Jul 18, 2022 at 09:08 UTC
Re: Perl using $seen with regex
by LanX (Saint) on Jul 18, 2022 at 01:52 UTC
    $ perl -ae 'print if $seen{/$_(\h\.)?}++' story.rst

    this code doesn't compile or even run

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: Perl using $seen with regex
by AnomalousMonk (Archbishop) on Jul 18, 2022 at 17:04 UTC

    What hippo said++.

    The /$_(\h\.)? fragment still does not compile. If I "fix" it to /$_(\h\.)?/ it doesn't make sense because $_ is the line/record read from the input file (story.rst in the example), so the equivalent expression is
        $_ =~ /$_(\h\.)?/
    The (\h\.)? tacked on at the end is optional and therefore kinda meaningless, so the expression more or less (!) reduces to
        $_ eq $_
    unless $_ contains regex metacharacters, in which case all bets are off.

    A possible way forward would be for you to post a small example of the typical content of the story.rst file and of the output you expect from processing the given content (update: see Short, Self-Contained, Correct Example for this general technique). The best way to do this is usually as a reply to your OP. If you want to update the OP, please see How do I change/delete my post? as suggested by hippo.

    ... is astray to s.t. ...

    I still don't understand what this means.

    N.B: The statement
        print if $seen{ $string }++
    will print every line after the line in which $string (however $string is derived) is first found. Is this what you intend?


    Give a man a fish:  <%-{-{-{-<

Re: Perl using $seen with regex
by ikegami (Patriarch) on Jul 19, 2022 at 14:45 UTC
    print if /...(...).../ && $seen{$1}++;

    (The pattern you had made no sense.)

    The above prints the duplicates. Don't forget to negate if you are trying to remove duplicates.

    print if /...(...).../ && !$seen{$1}++;