s/,/','/; 
Author Message
 s/,/','/;

Help!!  I'm trying to replace a

comma

with

apostrophe comma apostrophe

with the following command

s/,/','/;

and no matter what combination of single quotes, double quotes,
brackets, etc., I try to use, it only replaces the comma with two
apostrophes and won't put the comma between them.  I assume it's
interpreting the comma as a list delimiter or something.

This must be something simple.

Please help!!

Thanks.



Fri, 22 Dec 2000 03:00:00 GMT  
 s/,/','/;


Quote:
>Help!!  I'm trying to replace a
>comma
>with
>apostrophe comma apostrophe
>s/,/','/;

>and no matter what combination of single quotes, double quotes,
>brackets, etc., I try to use, it only replaces the comma with two
>apostrophes and won't put the comma between them.  I assume it's
>interpreting the comma as a list delimiter or something.

works for me:

   #!/usr/bin/perl

   $_ = "dogs, cats";

   s/,/','/;

   print;

   __END__

   dogs',' cats

are you sure there isn't another problem in your code?  which
version of Perl are you using?

--

CGI Meta FAQ <URL:http://computerdog.com/CGI_MetaFAQ.html>
Comprehensive Perl Archive Network (CPAN) <URL:http://www.perl.com>
Perl Mongers Travel Deals! <URL:http://www.pm.org/travel.html>



Fri, 22 Dec 2000 03:00:00 GMT  
 s/,/','/;

Aha, I see what's wrong.  Doh!!  Stupid.  Line 5 below is substituting a
comma with among other things, another comma, which is picked up by the
next line of code and changed to another string.  I just didn't see what
was happening.  I've been looking for that for about 5 hours!

Thanks for your help anyway, Brian.

Quote:

>    s|,|</font></td>\n<td><font size=-2>|;
>    s|,|</font></td>\n<td><font size=-2><INPUT TYPE=\"value\" NAME=\"|;
>    s/,/quant\" VALUE=\"1\" SIZE=3>\n<INPUT TYPE=\"button\"\" NAME=\"/;
>    s/,/add\" VALUE=\"Add\" onclick=\"top.menu.buyItem\('/;
>    s/,/','/;
>    s/,/ document.itemsform./;
>    s/,/quant.value\)\">/;
>    s|<td><font size=-1></font></td>|<td>&nbsp;</td>\n|g;
>    print ;
>    print "</font></td></tr>\n";

> $_ initially contains a single comma-delimited database record with a
> dozen or so fields.  I'm creating JavaScript code incorporating the
> contents of the fields.  If I replace the apostrophe-comma-apostrophe
> string in line 5 with any other string, it works fine, just won't work
> with a comma in there.  It returns two apostrophes and omits the comma.

> Thanks for the help, if anything else comes to mind, please post it!

> (Using Perl 5.)



Fri, 22 Dec 2000 03:00:00 GMT  
 s/,/','/;

Quote:


> [snip]

> > are you sure there isn't another problem in your code?  which
> > version of Perl are you using?

> > --

> Hmmm... I thought about that too, maybe I'm not seeing it-- here are the
> surrounding few lines:

>    s|,|</font></td>\n<td><font size=-2>|;
>    s|,|</font></td>\n<td><font size=-2><INPUT TYPE=\"value\" NAME=\"|;
>    s/,/quant\" VALUE=\"1\" SIZE=3>\n<INPUT TYPE=\"button\"\" NAME=\"/;
>    s/,/add\" VALUE=\"Add\" onclick=\"top.menu.buyItem\('/;
> 1  s/,/','/;
> 2  s/,/ document.itemsform./;
> 3  s/,/quant.value\)\">/;
>    s|<td><font size=-1></font></td>|<td>&nbsp;</td>\n|g;
>    print ;
>    print "</font></td></tr>\n";

Look at the lines I labeled "1" and "2", above.  These two lines do
exactly the same thing as ...

   s/,/' document.itemsform.'/;

(This is because line "2" replaces the comma that appears between
apostrophes in line "1").

Does this explain what you're seeing in your output?

If so, then you can replace line "1" with something like this ...

  s/,/'-COMMA-'/;

... and then somewhere after line "3" and before the first `print', you
can do this:

  s/-COMMA-/,/;

Of course, there are other ways to get the same results, as well.

--

 perl -e '$n=170;for($d=2;($d*$d)<=$n;$d+=(1+($d%2))){for($t=0;($n%$d)==0;




Fri, 22 Dec 2000 03:00:00 GMT  
 s/,/','/;

Quote:
>s/,/','/;

>and no matter what combination of single quotes, double quotes,
>brackets, etc., I try to use, it only replaces the comma with two
>apostrophes and won't put the comma between them.  I assume it's
>interpreting the comma as a list delimiter or something.

I tried it with my ActiveState Perl and it worked as expected. The first
comma turned to ','

Which version are you using?

-a



Sat, 23 Dec 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. What is pseudorandommy @chars=('a'..'z','A'..'Z','0'..'9','_');

2. unpack('x10','')

3. split on ':', but not '\:'

4. s''' not behaving as expected

5. Warnings: math with '''' and uninitialized values

6. Passing '+', '=' with query_form in URI::URL

7. Question: nested comment '('...')' removal?

8. wanted: scripts that emulate 'awk','sed','expr', for "Configure"

9. I want sugar: $ref->{'a', 'b', 'c') = qw ( x y z )

10. getservbyname('smtp','tcp') Problem

11. 'do'/'require' + parameters

12. perl -e '$a='*'' What's wrong with it ?

 

 
Powered by phpBB® Forum Software