Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use warnings;
- use strict;
- open (XIN, '<', 'driftingprog-in.xml') or die "$!\n";
- open (XOUT, '>', 'driftingprog-out.xml') or die "$!\n";
- my %content;
- ## stores 3 lines from file, used by second loop
- my @block;
- while (<XIN>) {
- if (/<content ID="(.*?)">(.*?)<\/content>/) {
- my ($id, $text) = ($1, $2);
- $content{$id} = $text;
- } elsif (/<text>/) {
- ## keep this line for next loop
- push @block, $_;
- ## when we start seeing <text> tags, go to next loop for these
- last;
- }
- print XOUT $_;
- }
- while (1) {
- ## read up to 3 lines into @block
- for (scalar(@block)+1..3) { my $l = <XIN>; last if (!defined $l); push @block, $l; }
- ## if we've read nothing, we are at EOF
- last if (scalar(@block) == 0);
- my $concat = join '', @block;
- if ( ($concat =~ s/(<text>\s*)<reference value="#(.*?)" \/>(\s*<\/text>)/$1.$content{$2}.$3/es) > 0) {
- print XOUT $concat;
- @block = ();
- } else {
- print XOUT shift @block;
- }
- }
- close XOUT;
- close XIN;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement