second substitution to work only on a found pattern

I

I.M. Postor

Hello,

I have some xml which is formatted bij a xsl processor:


<c02 level="group">
<did>
<unitid>13-16</unitid>
<unittitle>several weeklies</unittitle>
<unitdate normal="1928/1931">1928-1931</unitdate>
<unitid>13-16</unitid>
</did>
<c03 level="file">
<unitid>13</unitid>
<unittitle>
<unitdate>1928</unitdate>
</unittitle>
</c03>
<c03 level="file">
<unitid>14</unitid>
<unittitle>
<unitdate>1929</unitdate>
</unittitle>
</c03>
<c03 level="file">
<unitid>15</unitid>
<unittitle>
<unitdate>1930</unitdate>
</unittitle>
</c03>
</c02>



but whenever there is a <c0X level="file"> element, for working
purposese i'd rather have flattened components:


<c02 level="group">
<did>
<unitid>13-16</unitid>
<unittitle>several weeklies</unittitle>
<unitdate normal="1928/1931">1928-1931</unitdate>
<unitid>13-16</unitid>
</did>
<c03 level="file"><unitid>13</unitid><unittitle><unitdate>1928</unitdate></unittitle></c03>
<c03 level="file"><unitid>14</unitid><unittitle><unitdate>1929</unitdate></unittitle></c03>
<c03 level="file"><unitid>15</unitid><unittitle><unitdate>1930</unitdate></unittitle></c03>
</c02>


<XXX level="file"> could be from <c01 level="file"> to <c12 level="file">, therefore:


while ($slurped_text =~ /(<(c0[1-9]|c1[012]) level="file">.*?<\/\2>)/sg)
{ print "hello"; } #OK

while ($slurped_text =~ /(<(c0[1-9]|c1[012]) level="file">.*?<\/\2>)/sg)
{$1 =~ s/\n *//g; }
ERROR: Modification of a read-only value attempted


How can I get the secondary substitution to work only on a found regex?
Or should I try another approach?


Cheers
 
J

John W. Krahn

I.M. Postor said:
I have some xml which is formatted bij a xsl processor:


<c02 level="group">
<did>
<unitid>13-16</unitid>
<unittitle>several weeklies</unittitle>
<unitdate normal="1928/1931">1928-1931</unitdate>
<unitid>13-16</unitid>
</did>
<c03 level="file">
<unitid>13</unitid>
<unittitle>
<unitdate>1928</unitdate>
</unittitle>
</c03>
<c03 level="file">
<unitid>14</unitid>
<unittitle>
<unitdate>1929</unitdate>
</unittitle>
</c03>
<c03 level="file">
<unitid>15</unitid>
<unittitle>
<unitdate>1930</unitdate>
</unittitle>
</c03>
</c02>



but whenever there is a <c0X level="file"> element, for working
purposese i'd rather have flattened components:


<c02 level="group">
<did>
<unitid>13-16</unitid>
<unittitle>several weeklies</unittitle>
<unitdate normal="1928/1931">1928-1931</unitdate>
<unitid>13-16</unitid>
</did>
<c03 level="file"><unitid>13</unitid><unittitle><unitdate>1928</unitdate></unittitle></c03>
<c03 level="file"><unitid>14</unitid><unittitle><unitdate>1929</unitdate></unittitle></c03>
<c03 level="file"><unitid>15</unitid><unittitle><unitdate>1930</unitdate></unittitle></c03>
</c02>


<XXX level="file"> could be from <c01 level="file"> to <c12 level="file">, therefore:


while ($slurped_text =~ /(<(c0[1-9]|c1[012]) level="file">.*?<\/\2>)/sg)
{ print "hello"; } #OK

while ($slurped_text =~ /(<(c0[1-9]|c1[012]) level="file">.*?<\/\2>)/sg)
{$1 =~ s/\n *//g; }
ERROR: Modification of a read-only value attempted


How can I get the secondary substitution to work only on a found regex?
Or should I try another approach?

$ perl -e'
my $slurped_text = <<XML;

<c02 level="group">
<did>
<unitid>13-16</unitid>
<unittitle>several weeklies</unittitle>
<unitdate normal="1928/1931">1928-1931</unitdate>
<unitid>13-16</unitid>
</did>
<c03 level="file">
<unitid>13</unitid>
<unittitle>
<unitdate>1928</unitdate>
</unittitle>
</c03>
<c03 level="file">
<unitid>14</unitid>
<unittitle>
<unitdate>1929</unitdate>
</unittitle>
</c03>
<c03 level="file">
<unitid>15</unitid>
<unittitle>
<unitdate>1930</unitdate>
</unittitle>
</c03>
</c02>

XML


print $slurped_text;
$slurped_text =~ s{(<(c0[1-9]|c1[012]) level="file">.*?</\2>)}{ ( my $x = $1 )
=~ s!\n *!!g; $x }seg;
print $slurped_text;
'

<c02 level="group">
<did>
<unitid>13-16</unitid>
<unittitle>several weeklies</unittitle>
<unitdate normal="1928/1931">1928-1931</unitdate>
<unitid>13-16</unitid>
</did>
<c03 level="file">
<unitid>13</unitid>
<unittitle>
<unitdate>1928</unitdate>
</unittitle>
</c03>
<c03 level="file">
<unitid>14</unitid>
<unittitle>
<unitdate>1929</unitdate>
</unittitle>
</c03>
<c03 level="file">
<unitid>15</unitid>
<unittitle>
<unitdate>1930</unitdate>
</unittitle>
</c03>
</c02>


<c02 level="group">
<did>
<unitid>13-16</unitid>
<unittitle>several weeklies</unittitle>
<unitdate normal="1928/1931">1928-1931</unitdate>
<unitid>13-16</unitid>
</did>
<c03
level="file"><unitid>13</unitid><unittitle><unitdate>1928</unitdate></unittitle></c03>
<c03
level="file"><unitid>14</unitid><unittitle><unitdate>1929</unitdate></unittitle></c03>
<c03
level="file"><unitid>15</unitid><unittitle><unitdate>1930</unitdate></unittitle></c03>
</c02>




John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top