It might be possible that I am putting the part to focus on the ISBN correction in the wrong place in the code

Consider using XML::Twig instead of regexes to process the file. Easier to apply changes to a data element before creating the flat file rather a complete line afterwards.

#!/usr/bin/perl use strict; use XML::Twig; my $xml = join '',<DATA>; my $twig = XML::Twig->new( twig_handlers => {'datafield' => \&datafield} ); $twig->parse( $xml ); sub datafield { my( $t, $e ) = @_; my %subfield = (); for my $elem ($e->children('subfield')){ $subfield{$elem->att('code')} = $elem->text; } my @f = (); $f[0] = $e->att('tag'); $f[1] = $e->att('ind1').$e->att('ind2'); my @tmp; for (sort keys %subfield){ push @tmp,$_.$subfield{$_}; } $f[2] = join '|',@tmp; # change if ($subfield{'x'} =~ /^(isbn13|isbn)$/){ $f[0] =~ s/024/020/; } # flat format for output printf ".%s. %s|%s\n",@f if ($f[2]); # skip blanks } #.245. 14|aThe solar system|cChris Oxlade [Author] #.024. 3#|a9780750247092|xisbn13 #.024. 3#|a0750247096|xisbn __DATA__ <collection> <record> <datafield ind1="1" ind2="4" tag="245"> <subfield code="a">The solar system</subfield> <subfield code="c">Chris Oxlade [Author]</subfield> </datafield> </record> <record> <datafield ind1="3" ind2="#" tag="024"> <subfield code="a">a9780750247092</subfield> <subfield code="x">isbn13</subfield> </datafield> <record> </record> <datafield ind1="3" ind2="#" tag="024"> <subfield code="a">a0750247096</subfield> <subfield code="x">isbn</subfield> </datafield> </record> <record> <datafield ind1="3" ind2="#" tag="024"> </datafield> </record> </collection>
poj

In reply to Re^2: Remove line and modify another by poj
in thread Remove line and modify another by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.