gube has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, i have a file which contains the below mentioned input, if the file has to follow <main> tag follow <sub1> follow <sub2> and <sub3>. If the order changes, the error message has to display. Assume the file may contain more than 1000 lines like this. Please help me.

<main>Ability-oriented motivation, 00206.p0105 <main>Ability test <sub1>in conjunction with achievement batteries, 00226.p0085 <sub3>definition, 00226.g0005 <main>Aboriginal athletes, 00954.p0155 <sub2>Absolutism, 00688.p0010

Thanks in advance,
Regards,
Gubendran

Retitled by davido from 'Problem in my file'.

Replies are listed 'Best First'.
Re: How to validate file format
by holli (Abbot) on Feb 03, 2005 at 14:46 UTC
    use strict; my $count = 1; my $line = 0; while (<DATA>) { $line++; $count = 1, next if /^<main>/; die "out of order at line $line!" if /<sub(\d+)>/ && $1 != $count++; } __DATA__ <main>Ability-oriented motivation, 00206.p0105 <main>Ability test <sub1>in conjunction with achievement batteries, 00226.p0085 <sub3>definition, 00226.g0005 <main>Aboriginal athletes, 00954.p0155 <sub2>Absolutism, 00688.p0010

    holli, regexed monk
Re: How to validate file format
by borisz (Canon) on Feb 03, 2005 at 15:05 UTC
    perl script.pl filename
    #!/usr/bin/perl my $s; while (<>) { my ( $tag, $value ) = /^<(main|sub(\d+))>/ or print STDERR "wrong tag on line $.\n" and next; $s = 0, next if $tag eq 'main'; next if $value == ++$s; print STDERR "wrong sub expect $s got $value in line $.\n"; $s = $value; } __OUTPUT__ wrong sub expect 2 got 3 in line 4 wrong sub expect 1 got 2 in line 6
    Boris
Re: How to validate file format
by gopalr (Priest) on Feb 04, 2005 at 01:20 UTC

    Hi,

    Pls. also try out this

    while(<DATA>) { my $a=''; next if $_=~m#^\s*$#; $val=0, $a=0 if $_=~m#<main>#; ++$val, $a=$1 if $_=~m#<sub([0-9])>#; print STDERR "Check the Order!! $a comes instead of $val\n" if ($a + != $val); } __DATA__ <main>Ability-oriented motivation, 00206.p0105 <main>Ability test <sub1>in conjunction with achievement batteries, 00226.p0085 <sub3>definition, 00226.g0005 <main>Aboriginal athletes, 00954.p0155 <sub2>Absolutism, 00688.p0010

    Thanks,

    Gopal.R

Re: How to validate file format
by Fletch (Bishop) on Feb 03, 2005 at 14:49 UTC

    Apologies to Tom Cruise, but: SHOW ME THE CODE! Show what you've already tried. Explain what output you're getting and how it differs from what you expect.