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

Hi,
my $var = '<date>';
I want to add '/' to $var and make it </date>.
What is the string function to me used

Replies are listed 'Best First'.
Re: Add character in middle
by grizzley (Chaplain) on Aug 12, 2009 at 08:50 UTC
    Just substitution regexp (more in manual - perlop): $var =~ s/</<\//;
      I used like this,
      #!/usr/bin/perl $var='<date>'; $newvar=$var; $sec = substr($newvar,0,1); $sec = '</'; print $sec,"\n"; print "Old value is $var and new value is $sec";
      It doesnot print like this Old value is <date> and new value is </date>
        Your last assignment to $sec is </. Why would you expect it to contain </date>?
Re: Add character in middle
by Marshall (Canon) on Aug 12, 2009 at 09:38 UTC
    One thing to remember when using either substitution or match, is that the deliminator char after the "s" or "m" can be anything you want it to be. Sometimes, like below this can help with forward slash confusion.
    #!/usr/bin/perl -w use strict; my $var = '<date>'; print "$var\n"; $var =~ s|date|/date|; #easier than s/date/\/date/; print "$var\n"; __END__ Prints: <date> </date>
Re: Add character in middle
by rovf (Priest) on Aug 12, 2009 at 08:53 UTC

    substr. Use it as lvalue, specifying 0 as 3rd argument.

    -- 
    Ronald Fischer <ynnor@mm.st>
A reply falls below the community's threshold of quality. You may see it by logging in.