tej has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Problem in String Replacement
by moritz (Cardinal) on Oct 07, 2011 at 12:26 UTC | |
Re: Problem in String Replacement
by DanielSpaniel (Scribe) on Oct 07, 2011 at 12:13 UTC | |
by Anonymous Monk on Oct 07, 2011 at 12:20 UTC | |
by tej (Scribe) on Oct 07, 2011 at 12:34 UTC | |
Re: Problem in String Replacement
by choroba (Cardinal) on Oct 07, 2011 at 13:15 UTC |
Back to
Seekers of Perl Wisdom