texuser74 has asked for the wisdom of the Perl Monks concerning the following question:
<input> <tr><tc>1</tc><tc>2</tc><tc>3</tc></tr> <tr><tc colspan="2">1and2</tc><tc>3</tc></tr> <tr><tc>1</tc><tc colspan="2">2and3</tc></tr> <tr><tc>1</tc><tc>2</tc><tc>3</tc></tr>
My present code produces wrong output:<RequiredOutput> <tr><tc cNum="1">1</tc><tc cNum="2">2</tc><tc cNum="3">3</tc></tr> <tr><tc cNum="1" colspan="2">1and2</tc><tc cNum="3">3</tc></tr> <tr><tc cNum="1">1</tc><tc cNum="2" colspan="2">2and3</tc></tr> <tr><tc cNum="1">1</tc><tc cNum="2">2</tc><tc cNum="3">3</tc></tr>
e.g. in the 2nd row, I am not sure how to increment the column Number cNum based on the previous column span's value.#!/usr/bin/perl open(IN, "<data.in") or die ("inFile not found -- $!\n"); open(OUT, ">data.out") or die ("outFile not found -- $!\n"); while(<IN>) { local $/='<tr>'; my $cnt=0; while(/<tc>/){ s!<tc>!'<tc cNum="'.++$cnt.'">'!ie; } print OUT; } close(IN); close(OUT);
Please give me some tips.
Thanks in advance.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how to loop through?
by desemondo (Hermit) on Feb 23, 2010 at 11:39 UTC | |
by texuser74 (Monk) on Feb 26, 2010 at 08:38 UTC | |
|
Re: how to loop through?
by rovf (Priest) on Feb 23, 2010 at 09:33 UTC |