rsriram has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I have a numbered list marked up with <nl>...</nl>. All items inside it are marked as <item>...</item>. I want to number these items sequentially, i.e., change the item tag to <item num="1">, <item num="2"> and so on. My code to do this is:
$no++;
$_ =~ s/<item>/'<item num="'.$no.'">'/egs;
In the output file I get the desired numbering but from the start to the end of file, the <item> tags are sequentially numbered. I want the count to restart everytime a new list <nl> is encountered. Any help!!!
Here is my complete code:
if (!@ARGV) { print "\t Usage: perl $0 <filename>\n\n"; exit(0); } open (F1, "<$ARGV[0]") || die ("Can't open the file $ARGV[0]. $!\n"); $out = $ARGV[0]; $out =~ s/(.+)\.(.+)/$1/; $out = $out.".xml"; open (F2, ">$out") || die ("Can't open the file $out. $!\n"); while(<F1>) { $no++; $_ =~ s/<item>/'<item num="'.$no.'">'/egs; print F2 $_; } close F1; close F2;
20060801 Janitored by Corion: Removed by-line <code> tags and enclosed code within one set of <code>...</code> tags
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Numbering list items
by prasadbabu (Prior) on Aug 01, 2006 at 10:51 UTC | |
|
Re: Numbering list items
by rodion (Chaplain) on Aug 01, 2006 at 11:00 UTC |