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


In reply to Numbering list items by rsriram

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.