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

In a file, I want to insert an element that number of times which is specified in the attribute. Value specified in <csp c="(\d+)"> will be taken and replaced with <colspec colnum="1" colname="col1"/>.

The program line for that is:

$_ =~ s/<csp c="(\d+)">/"<colspec colnum=\"1\" colname=\"col1\"\/>" x $1/egs;

This is working fine, but I need the number mentioned in colnum and colname needs to be incremented by one everytime. If I use a variable and increase it by one with the following code, I am not getting the numbers.

$_ =~ s/<csp c="(\d+)">/"<colspec colnum=\"$num\" colname=\"col$num++\"\/>" x $1/egs;

To be more precise, if the input is <csp c="3">, the output should be:

<colspec colnum="1" colname="col1"/> <colspec colnum="2" colname="col2"/> <colspec colnum="3" colname="col3"/>

Could anyone help me please?

Replies are listed 'Best First'.
Re: Making multiple replaces through Regex
by sathiya.sw (Monk) on Nov 27, 2008 at 06:46 UTC
    If iterations are ok for you, then try the following..
    if(/<csp c="(\d+)">/) { $number= 1; $iteration = $1; $string .= '<colspec colnum="'. $number . '" colname="col' . $number+ ++ . '"/>' while ( $iteration-- ); } print $string;
    code is, first check whether format given is right, if it is then use some temporary variables, and do the string concatenation with the help of it.
Re: Making multiple replaces through Regex
by prasadbabu (Prior) on Nov 27, 2008 at 06:15 UTC

    Hi rsriram,

    It is working fine for me. If I am wrong somewhere, please correct me. Below is a sample code.

    $s = '<csp c="3">'; $s =~ s/<csp c="(\d+)">/"<colspec colnum=\"1\" colname=\"col1\"\/>\n" +x $1/egs; print $s; output: -------- <colspec colnum="1" colname="col1"/> <colspec colnum="1" colname="col1"/> <colspec colnum="1" colname="col1"/>

    Prasad

      Hi Prasad, Upto this its working for me too! But my desired output is
      <colspec colnum="1" colname="col1"/> <colspec colnum="2" colname="col2"/> <colspec colnum="3" colname="col3"/>
      The value in colnum and colname needs to get incremented everytime! Thanks.
Re: Making multiple replaces through Regex
by parv (Parson) on Nov 27, 2008 at 06:55 UTC
    $_ =~ s/<csp c="(\d+)">/"<colspec colnum=\"1\" colname=\"col1\"\/>" +x $1/egs;

    Note that qq// can be substituted for "" for the same effect. That way there will no need to escape a ". Similarly, / delimiter can be substituted with other character(s) which do(es) not appear in your pattern or replacement to avoid escaping of the delimiter character ...

    $_ =~ s[<csp c="(\d+)">][qq(<colspec colnum="1" colname="col1"/>) x $1 +]egsx;
Re: Making multiple replaces through Regex
by jwkrahn (Abbot) on Nov 27, 2008 at 07:06 UTC
    $ perl -le' $_ = q[ <csp c="3"> ]; print; s{<csp c="(\d+)">}{ join "", map qq[<colspec colnum="$_" colname="col$ +_"/>], 1 .. $1 }eg; print; ' <csp c="3"> <colspec colnum="1" colname="col1"/><colspec colnum="2" colname="col2 +"/><colspec colnum="3" colname="col3"/>
Re: Making multiple replaces through Regex
by Punitha (Priest) on Nov 27, 2008 at 06:23 UTC

    Try like this,

    my ($colno) = $_ =~ /<csp c="(\d+)">/; my $csp; for my $i (1..$colno) { $csp = $csp."<colspec colnum=\"$i\" colname=\"col$i\"\/>\n"; } $_=~s/<csp c=\"$colno\">/$csp/g; print "$_";

    Punitha