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

    Hi rsriram,

    Here is one way to do it. But If you have proper xml file it is always good to use XML modules.

    use strict; use warnings; my $string = '<nl><item>asdfasdf</item><item>asdfasdf</item></nl><nl>< +item>asdfasdf</item><item>asdfasdf</item></nl>'; $string =~ s|<nl>((?:(?!</nl>).)*)</nl>|'<nl>'.&item($1).'</nl>'|egs; print $string; sub item { my ($item) = @_; my $no =1; $item =~ s/<item>/'<item num="'.$no++.'">'/egs; return $item; } output: ------- <nl><item num="1">asdfasdf</item><item num="2">asdfasdf</item></nl><nl +><item num ="1">asdfasdf</item><item num="2">asdfasdf</item></nl>

    or even shorter than the above

    $string =~ s|<nl>((?:(?!</nl>).)*)</nl>|my $item = $1; my $no = 1; $it +em =~ s/<item>/'<item num="'.$no++.'">'/egs;"<nl>$item</nl>"|egs;

    Prasad

Re: Numbering list items
by rodion (Chaplain) on Aug 01, 2006 at 11:00 UTC
    I think you just forgot to reset "$no". Adding the single line
    $no = 0 if /<n1>/;
    at the end of the while loop might do it, if you can rely on the "<n1>" tag not being in the same F1 record as one of your "<item>" tags. If not, you will have to parse things a little finer, as in hsriram's example.