Find Missing Column and Extra Column

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Byomokesh
    New Member
    • Jul 2006
    • 6

    Find Missing Column and Extra Column

    Hi All,

    This XML file is 2 errors. My table is 3 columns. One is extra column and another is missing one columne. How i will find those error through perl script.

    XML file
    ------------
    <table>
    <tgroup cols="3">
    <colspec colnum="1" colname="col1"/>
    <colspec colnum="2" colname="col2"/>
    <colspec colnum="3" colname="col3"/>
    <tbody>
    <row>
    <entry valign="top" align="left"><p >nada</p></entry>
    <entry valign="top" align="left"><p >nothing</p></entry>
    <entry valign="top" align="left"><p >nada.</p></entry>
    <entry valign="top" align="left"><p ></p></entry> <!-- One Row Extra Here -->
    </row>
    <row>
    <entry valign="top" align="left" namest="col1" nameend="col2"> <p>centra</p></entry>
    <!-- One row is missing here -->
    </row>
    </tbody>
    </tgroup>
    </table>

    Thanks for any help.
    Byomokesh
  • docsnyder
    New Member
    • Dec 2006
    • 88

    #2
    @Byomokesh

    Assuming @lines contains the lines of your xml file, you could do something like this:

    Code:
    for ( $i=0 ; $i<=$#lines ; $i++ ) {
      if ( $lines[$i] =~ m(<tgroup\s+cols="(\d+)") ) {
        $numCols = $1;
      }
      elsif ( $lines[$i] =~ m(<row>) ) {
        $colCnt = 0;
        $start  = $i + 1;
      }
      elsif ( $lines[$i] =~ m(</row>) ) {
        if ( $colCnt != $numCols ) {
          printf("lines %d-%d: ERROR: got $colCnt rows (expected: $numCols)\n", $start, $i+1);
          $colCnt = 0;
        }
      }
      elsif ( $lines[$i] =~ m(<entry\s+valign) ) {
        $colCnt++;
      }
    }
    Greetz, Doc

    Comment

    Working...