Hello monks,

I have data like

<i><ce:bib-reference id="bib1"> <ce:other-ref> <ce:textref>Adolophs R (2002), Trust in the brain. Nat Neurosci 5(3):1 +92-193.</ce:textref></ce:other-ref> </ce:bib-reference> <ce:bib-reference id="bib2"> <ce:other-ref> <ce:textref>American Psychiatric Association. Diagnostic and Statistic +al Manual of Mental Disorders, (4<ce:sup>th</ce:sup> ed., text revisi +on). Washington, DC: Author, 2000.</ce:textref></ce:other-ref> </ce:bib-reference> <ce:bib-reference id="bib3"> <ce:other-ref> <ce:textref>Amici R, Zamboni G, Perez E, Jones CA, Parmeggiani PL (199 +8), The influence of a heavy thermal load on REM sleep in the rat. Br +ain Res 781:252-258.</ce:textref></ce:other-ref> </ce:bib-reference> </i>

What I want to do is to add "<ce:label>Data before first comma</ce:label> after each "<ce:bib-reference id="bib\d">"

I have written a script for this, but finally when i try to replace the data in file script freezes

my code is like:
use warnings; use strict; use Cwd; my $dir=getcwd; print "$dir\n"; opendir DIR, $dir or die "cant open dir"; my @files = grep /mlx/,(readdir DIR); closedir DIR; foreach my $file (@files) { open(INF, $file) or die "cannot open $file: $!\n"; my $fdata=join("", <INF>); close INF; ##<ce:bib-reference id="bib2">... </ce:bib-reference> while ($fdata=~m{(<ce:bib\-reference id\=\"bib[0-9]+\">\n)(.*?)(<\ +/ce:bib\-reference>)}sg){ my $bib=$1; my $label=ExtractAuth($&); $fdata=~s/(\Q$bib\E)/$1$label/; } print "$fdata"; } sub ExtractAuth{ my $bib=shift; my $auth=""; my $label=""; ##<ce:textref>...</ce:textref> while ($bib=~m{<ce:textref>(.*?)<\/ce:textref>}sg){ $auth=$1; if ($auth=~m{\(\d{4}[A-z]?\)}){ my $count= ($auth =~ tr/\,//); $label=AddLabel($auth,$count); #$bib=~s/(<ce:bib\-reference id\=\"bib[0-9]+\">\n)/$1$labe +l/; }else { $label="Year Not Found\n"; #$bib=~s/(<ce:bib\-reference id\=\"bib[0-9]+\">\n)/$1$labe +l/; } } #$bib=~s/(<ce:bib\-reference id\=\"bib[0-9]+\">\n)/$1$label/g; return ($label); } sub AddLabel{ my $auth=shift; my $count=shift; my $fauth=""; my $year; my $labelst=""; if ($count<=1){ if ($auth=~m{(.*?)(\(\d{4}[A-z]?\))}){ $fauth=$1; $year=$2; $fauth=~s/\s$//; $labelst="<ce:label>$fauth\, $year<\/ce:label>\n"; } } return ($labelst); }

Whats going wrong in this code?


In reply to Problem in String Replacement by tej

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.