Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Numbering list items

by prasadbabu (Prior)
on Aug 01, 2006 at 10:51 UTC ( [id://564962]=note: print w/replies, xml ) Need Help??


in reply to Numbering list items

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://564962]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 14:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found