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?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.