use strict; use warnings; my $string="fsdfsdfsdf fsdfsdfsdf"; my ($a,$b)=split(/\s+/,$string); my $new_string=$a."_".$b; print $new_string."\n";
Regards Anirban Adhikary. On Mon, Nov 3, 2008 at 2:15 PM, John W. Krahn <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > >> Hi all, >> > > Hello, > > I have a string which contains spaces. I need to replace those spaces >> with underscore, so I have written command like this >> >> $string="fsdfsdfsdf fsdfsdfsdf"; >> chomp($string1 = ($string =~ s/\s+$/_/g)); >> > > \s+$ matches one or more of any whitespace characters (' ', "\t", "\n", > "\r" or "\f") at the *end* of the string, but your string does not contain > any whitespace at the end. The substitution operator is bound to the > $string variable so only the $string variable is modified. The results of > the substitution is then assigned to $string1 which will be either '' if no > substitution was made or the number of substitutions made. In any case, and > even if you had done it correctly, there is nothing for chomp() to do so it > is superfluous. > > You need to first assign the contents of $string to $string1 and then > modify $string1. > > > print "$string1\n"; >> >> but still $string1 is not printing proper result. Result should be >> "fsdfsdfsdf_fsdfsdfsdf" >> > > > John > -- > Perl isn't a toolbox, but a small machine shop where you > can special-order certain sorts of tools at low cost and > in short order. -- Larry Wall > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > http://learn.perl.org/ > > >