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

Below I mentioned the Code. I need how to replace it using id numbers

Need to replace <xref ref-type="bibr" rid="b0001">Ewald <italic>et al.</italic>, 1993</xref> as <xref ref-type="bibr" rid="Ewald1">Ewald <italic>et al.</italic>, 1993</xref>

To Take the text from the ref-list id and replace in call outs

Example:

<xref ref-type="bibr" rid="b0001">Ewald <italic>et al.</italic>, 1993</xref>

<bib id="CIT-Ewald-b0001"> take the text found in id inbetween the text CIT- and -b0001 i.e. "Ewald" and find the id found in body text i.e. rid="b0001" and replace as rid="Ewald1"

<bib id="CIT-Kasten-b0002"> take the text found in id inbetween the text CIT- and -b0002 i.e. "Kasten" and find the id found in body text i.e. rid="b0002" and replace as rid="Kasten2"

Now u onderstand what I am asking

<body><p>Approximately 125&#x2009;231 joint replacements and 442 total + elbow replacements were performed in England and Wales in 2013 accor +ding to data collected by the <xref ref-type="bibr" rid="b0004">Natio +nal Joint Registry (2014)</xref>. Elbow replacements have a deep infe +ction rate ranging from 1 to 12&#x2009;% (<xref ref-type="bibr" rid=" +b0001">Ewald <italic>et al.</italic>, 1993</xref>; <xref ref-type="bi +br" rid="b0002">Kasten &#x0026; Skinner, 1993</xref>), with an estima +ted mean of 5&#x2009;% (<xref ref-type="bibr" rid="b0003">Little <ita +lic>et al.</italic>, 2005</xref>).</p><p><italic>Staphylococcus aureu +s</italic> and <italic>Staphylococcus epidermidis</italic> together a +ccount for approximately 50&#x2009;% of the agents isolated from infe +cted joint prostheses, making them the most common causative bacteria + (<xref ref-type="bibr" rid="b0005">Tattevin <italic>et al.</italic>, + 1999</xref>). Bacteria responsible for elbow prostheses infections i +nclude <italic>Staphylococcus aureus</italic>, <italic>Staphylococcus + epidermidis</italic>, <italic>Klebsiella</italic> spp. and <italic>E +nterobacter cloacae</italic> (<xref ref-type="bibr" rid="b0006">Yamag +uchi <italic>et al.</italic>, 1998</xref>).</p></body> <back><ref-list> <bib id="CIT-Ewald-b0001"><citation citation-type="journal"><article-t +itle>Capitellocondylar total elbow replacement in rheumatoid arthriti +s. Long-term results</article-title> <source>J Bone and Joint Surg</s +ource> <volume>75</volume> <fpage>498</fpage>&#x2013;<lpage>507</lpag +e>.</citation></ref> <bib id="CIT-Kasten-b0002"><citation citation-type="journal"><article- +title>Total elbow arthroplasty. An 18-year experience</article-title> +<source>Clin Orthop</source><volume>290</volume><fpage>177</fpage>&#x +2013;<lpage>188</lpage>.</citation> <bib id="CIT-Little-b0003"><article-title>Total elbow arthroplasty: a +systematic review of the literature in the English language until the + end of 2003</article-title><source>J Bone Joint Surg Br</source><vol +ume>87</volume><fpage>437</fpage>&#x2013;<lpage>444</lpage>.</citatio +n></ref> <bib id="CIT-National-b0004"><citation citation-type="book"><inpress>< +ext-link ext-link-type="url" xlink:href="http://www.njrcentre.org.uk/ +njrcentre/healthcareproviders/accessingthedata/statsonline/njrstatson +line/tabid/179/default.aspx">http://www.njrcentre.org.uk/njrcentre/He +althcareproviders/Accessingthedata/StatsOnline/NJRStatsOnline/tabid/1 +79/Default.aspx</ext-link>. Accessed 25 January 2014</inpress>.</cita +tion></ref> <bib id="CIT-Tattevin-b0005"><article-title>Prosthetic joint infection +: when can prosthesis salvage be considered?</article-title><source>C +lin Infect Dis</source><volume>29</volume><fpage>292</fpage>&#x2013;< +lpage>295</lpage>.</citation> <bib id="CIT-Yamaguchi-b0006"><article-title>Infection after total elb +ow arthroplasty</article-title><source>J Bone Joint Surg Am</source>< +volume>80</volume><fpage>481</fpage>&#x2013;<lpage>491</lpage>.</cita +tion> </ref-list> </back>

Replies are listed 'Best First'.
Re: Search and replace the Text using id numbers in perl using hash
by LanX (Saint) on Feb 09, 2015 at 12:41 UTC
Re: Search and replace the Text using id numbers in perl using hash
by kroach (Pilgrim) on Feb 09, 2015 at 12:48 UTC

    I'm having a hard time trying to understand your question. Could you be more specific?

    What do you want to replace that string with?

      err, I understood he is asking for either a literal...

      s[<xref ref-type="bibr" rid="b0001">Ewald <italic>et al.</italic>, 199 +3</xref>]{<xref ref-type="bibr" rid="Ewald1">Ewald <italic>et al.</it +alic>, 1993</xref>}

      ... or something along that line that maybe needs to handle the ocurrences of things resembling the strings "b0001" and "Ewald" in the example in a more dynamic fashion, if I am on to the right idea.

      Cheers, Sören

      Créateur des bugs mobiles - let loose once, run everywhere.
      (hooked on the Perl Programming language)

Re: Search and replace the Text using id numbers in perl using hash
by kroach (Pilgrim) on Feb 09, 2015 at 20:58 UTC
    I came up with the following solution. We capture all relevant information from the pieces you mentioned and add them to a hash. Then it's just a matter of simple replacement of matches with hash values.
    use warnings; use strict; my ($fname) = @ARGV; open my $fh, '<', $fname or die $!; my %cit_ids; # Fill the hash with relevant ids while (<$fh>) { while (m/<bib id="CIT-(?<name>\w+)-(?<id>b0*(?<num>\d+))">/g) { $cit_ids{$+{id}} = $+{name} . $+{num}; } } seek $fh, 0, 0; # Rewind the file while (<$fh>) { s/rid="(b0*\d+)"/rid="$cit_ids{$1}"/g; print; }