codewalker 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: To replace the occurence string in perl
by Corion (Patriarch) on Feb 23, 2015 at 11:58 UTC

    You're a long-time member of this site, so you should know to post the code you have already written and also explain how your current code fails to do what you need.

    Also, you should know to format your code and data using <code>...</code> tags so they can be downloaded easily.

    Please do both to help us help you better.

Re: To replace the occurence string in perl
by roboticus (Chancellor) on Feb 23, 2015 at 12:03 UTC

    codewalker:

    I'd suggest installing XML::Twig and reading the docs. There are examples that show how to step through a document chunk by chunk editing as you go along.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: To replace the occurence string in perl
by kroach (Pilgrim) on Feb 23, 2015 at 16:11 UTC

    You can count the occurrences of each name with a hash and use eval substitution to increment on the go. Like this:

    use strict; use warnings; my %count; while (<>) { s{<bib id="CIT-(\w+)-b(0*\d+)">} {"<bib id=\"CIT-$1" . ++$count{$1} . "-b$2\">"}ge; print; }

    Incrementing undef will treat it as 0, so when a key is first encountered it will have 1 appended to the name.