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

Dear Perl Experts,
I am not sure how to loop through tr.../tr and produce this required output:
<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>
<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>
My present code produces wrong output:
#!/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);
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.

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
    Please give me some tips
    1. use strict; use warnings;. Always. Doing so will help make your life easier when writing code.

    2. When posting code on PM, using the __DATA__ handle makes it easier for others to reproduce what your seeing. One file, one click. This in turn makes it easier for others to respond and assist you.

    in the 2nd row, I am not sure how to increment the column Number cNum based on the previous column span's value That's one way you could do it, though in my opinion, not the best approach...
    (Since you asked for tips, I'll give you a hint and leave you to work it out.) How about this: (see comment)

    #!/usr/bin/perl use strict; use warnings; { local $/='<tr>'; while (my $line = <DATA>){ my $i=0; while ($line =~ m/<tc>/) { $line =~ s!<tc>!'<tc cNum="'.++$i.'">'!ie; #why not insert another substitution in here that handles +the case <tc (colspan="\d+")> ... } print $line; } } __DATA__ <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>
      Thanks for your reply.
Re: how to loop through?
by rovf (Priest) on Feb 23, 2010 at 09:33 UTC
    while(/<tc>/)

    If you write the regexp as

    /<tc ( [^>]+)? >/x
    you can distinguish: If $1 afterwards is undef, you had only a <tc>. If $1 is defined, it contains what comes after the <tc.

    -- 
    Ronald Fischer <ynnor@mm.st>