Re: Last line issue



Dear Jonh,

many, many thanks for your quick answer.

I modified your script a bit:
$line .= $_ if /Id|To|From/;
print $OUT "$id\t$line\n" if m!/Note!;

to:
$line .= $_ if m!<Note>! .. m!</Note>!;
print $OUT "$id\t$line\n" if m!</Note>!;


but some problem still persists with the output:
001 <Id>001</Id><To>Thomas</To><From>Joana</From><Message>foo</Message></Note>
002 <Id>002</Id><To>John</To><From>Paula</From><Message>foo</Message></Note>
003 <Id>003</Id><To>Andrew</To><From>Maria</From><Message>foo</Message></Note>

Note that there is no opening <Note> tag at the beginning.

Best, Andrej






John W. Krahn wrote:
Andrej Kastrin wrote:
Dear all,

Hello,

to pre-process my XML dataset in run simple Perl script on it, which extract Id identifier from XML data and paste the whole XML record to it. For example, the input data looks like:

<NoteSet>
<Note>
<Id>001</Id>
<To>Thomas</To>
<From>Joana</From>
</Note>
<Note>
<Id>002</Id>
<To>John</To>
<From>Paula</From>
</Note>
<Note>
<Id>003</Id>
<To>Andrew</To>
<From>Maria</From>
</Note>
</NoteSet>

and the desire output using the script should be:

001 <Note><Id>001</Id><To>Thomas</To><From>Joana</From></Note>
002 <Note><Id>002</Id><To>John</To><From>Paula</From></Note>
003 <Note><Id>003</Id><To>Andrew</To><From>Maria</From></Note>

This should do what you want:

#!/usr/bin/perl
use warnings;
use strict;

my $FNI = shift;
my $FNO = "$FNI.dat";

open my $OUT, '>', $FNO or die "Cannot open '$FNO' $!";
open my $IN, '<', $FNI or die "Cannot open '$FNI' $!";

my ( $id, $line );
while ( <$IN> ) {
if ( m!<Note>! .. m!</Note>! ) {
( $id, $line ) = ( $1, '' ) if m!<Id>(\d+)</Id>!;
s/\A\s+//;
s/\s+\z//;
tr/\t/ /s; # more efficient than s/\t+/ /g
$line .= $_ if /Id|To|From/;
print $OUT "$id\t$line\n" if m!/Note!;
}
}

close $IN;
close $OUT;



But I can't figure why the script below omit the last record in the input dataset, e.g.:

Your second while loop is eating up the third record without outputting anything.



John
.



Relevant Pages

  • Re: Last line issue
    ... extract Id identifier from XML data and paste the whole XML record to it. ... the input data looks like: ... and the desire output using the script should be: ...
    (perl.beginners)
  • RE: Moving Data
    ... * I need to collect the input data on exampl1.aspx by hitting the review button. ... <script runat="server"> ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Secure Form Script?
    ... The only input data on the header is the subject line, ... but what do you see as the issues with calling sendmail ... > Or you can write your own script in Perl ...
    (Focus-Linux)
  • Re: Passing Variables from One Perlscript to Another
    ... > i normally use one script to do it all. ... > in the script, if the form has been submitted, the script validates the ... > section where the input form is displayed, populated with input data, if ... But what I want to do is to display the ...
    (comp.lang.perl.misc)
  • Re: Using ASP script to connect to SQL Server
    ... script. ... I've made a form to input data. ... Do you know anything about Html? ... You do put the file name of the ASP script in the input Form. ...
    (microsoft.public.frontpage)