in reply to Re: Not getting proper output value
in thread Not getting proper output value

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re^3: Not getting proper output value
by AnomalousMonk (Archbishop) on Apr 06, 2021 at 18:30 UTC

    As I understand it, the matching problem you're posing is to detect a pattern with duplicates like .abc(abc_p) and then make multiple changes to the duplicated alphabetic substrings, so that the string above becomes the two strings .abc_1(abc_p_1) and .abc_2(abc_p_2). The abc and abc_p substrings may occur in any order, as may the dotted and parenthetic fields.

    I would approach the problem in two steps: detect duplication; transform all duplicated substrings.

    From previous postings, I think the elements the detection regex might be
        my $rx_pre_dup = qr{ [.(] }xms;
    and
        my $rx_str = qr{ \b (?: abc_p | abc) \b }xms;
    which can be combined into the regex
        my $rx_dup_str = qr{ $rx_pre_dup $rx_str }xms;

    Then a match for duplicates might look like
        m{ $rx_dup_str .*? $rx_dup_str }xms
    and one can exercise this with

    Win8 Strawberry 5.8.9.5 (32) Tue 04/06/2021 13:52:38 C:\@Work\Perl\monks\suvendra123 >perl use strict; use warnings; my $rx_pre_dup = qr{ [.(] }xms; my $rx_str = qr{ \b (?: abc_p | abc) \b }xms; my $rx_dup_str = qr{ $rx_pre_dup $rx_str }xms; for my $string ( '.abc', '.abc(pqr)', '.abcfoo(abc)', '.abc.abc', '.abc(abc_p)', '.abc_p.abc', '.abc_p.abc(abc_p)', '.abc_p (abc_p)', ) { my $got_dups = $string =~ m{ $rx_dup_str .*? $rx_dup_str }xms; printf "'$string' has %sdups \n", $got_dups ? '' : 'NO '; } ^Z '.abc' has NO dups '.abc(pqr)' has NO dups '.abcfoo(abc)' has NO dups '.abc.abc' has dups '.abc(abc_p)' has dups '.abc_p.abc' has dups '.abc_p.abc(abc_p)' has dups '.abc_p (abc_p)' has dups

    Some questions:

    • '.abcfoo(abc)' has no dups, but '.abc_p (abc_p)' has. Why is this? Is this what you want?
    • '.abc_p.abc(abc_p)' has dups, but more than two. Is this what you want?

    Once you know that a string has dups, you can find all $rx_dup_str matches and make substitutions as needed. The int abc; transformations should be fairly easy, and I think you already have those in hand.


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