$1 does not seem to work in the replacement.
because $1 from the m// is reset by the subsequent s///. You have to capture $1 in some variable:
$_ =~ /<FN1>([^<].+)<\/FN1>/;
$found = $1;
$_ =~ s/<FN1ref>/<FN1>$found<\/FN1>/g;
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
| [reply] [d/l] [select] |
use strict;
use warnings;
my $data = do{local $/; <DATA>};
my $i = 1;
while ($data =~ s/(<FN$i>(?:(?:(?!<\/FN$i>).)*)<\/FN$i>)\n*//gsi){
my $foot = $1;
$data=~ s/<FN${i}ref>/$foot/gi;
$i++;
}
print $data;
__DATA__
This is my text<FN1ref>. This is my text
This is my text. This is my text<FN2ref>. This is my text
This is my text. This is my text<FN3ref>, this is my text
This is my text. This is my text<FN4ref>, this is my text
This is my text. This is my text<FN5ref>, this is my text
This is my text.
<FN1>Footnote1</FN1>
<FN2>Footnote2</FN2>
<FN3>Footnote3</FN3>
<FN4>Footnote4</FN4>
<FN5>Footnote5</FN5>
Output:
-------
This is my text<FN1>Footnote1</FN1>. This is my text
This is my text. This is my text<FN2>Footnote2</FN2>. This is my text
This is my text. This is my text<FN3>Footnote3</FN3>, this is my text
This is my text. This is my text<FN4>Footnote4</FN4>, this is my text
This is my text. This is my text<FN5>Footnote5</FN5>, this is my text
This is my text.
Regards, Velusamy R. eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';
| [reply] [d/l] [select] |
Hi rsriram,
Here is one way to do it.
use strict;
use warnings;
my $str = do { local $/, <DATA> };
my @fnote = qw(<FN1>footnote1</FN1> <FN2>footnote1</FN2> <FN3>footnote
+1</FN3>); #Already retrived footnotes
my %hash;
for (@fnote)
{
$hash{$1} = $_ if ($_ =~ /<(FN\d+)>/);
}
$str =~ s|<(FN\d+)ref>|$hash{$1}|g;
print $str;
__DATA__
<p>
Here some text <FN1ref> and some text
Here some text <FN2ref> and some text
Here some text <FN3ref> and some text
</p>
<FN1>afsdfsaf</FN1>
<FN2>afsdfsaf</FN2>
<FN3>afsdfsaf</FN3>
outputs:
--------
<p>
Here some text <FN1>footnote1</FN1> and some text
Here some text <FN2>footnote1</FN2> and some text
Here some text <FN3>footnote1</FN3> and some text
</p>
<FN1>afsdfsaf</FN1>
<FN2>afsdfsaf</FN2>
<FN3>afsdfsaf</FN3>
* My 200th post :)
| [reply] [d/l] [select] |
Also, if what you're doing is parsing an XML file, you should consider using a proper XML parser, which will handle properly all the strange things that can show up in an XML file.
| [reply] |