Prev: FAQ 9.14 How do I make sure users can't enter values into a form that cause my CGI script to do bad things?
Next: FAQ 9.17 How do I check a valid mail address?
From: Phred Phungus on 31 Mar 2010 15:56 J�rgen Exner wrote: > Phred Phungus <Phred(a)example.invalid> wrote: >> J�rgen Exner wrote: >>> Phred Phungus <Phred(a)example.invalid> wrote: >>>> PerlFAQ Server wrote: >>>>> $string = "Placido P. Octopus"; >>>>> $regex = "P."; >>>>> >>>>> $string =~ s/$regex/Polyp/; >>>>> # $string is now "Polypacido P. Octopus" >>>>> >>>>> Because "." is special in regular expressions, and can match any single >>>>> character, the regex "P." here has matched the <Pl> in the original >>>>> string. >>>>> >>>> Am I then correct that a period is not a character? >>> How did you come to that conclusion? >> Because the regex would then match the middle initial and the string >> would be "Polypacido Polyp Octopus". > > No, it wouldn't because there is no /g modifier in the s///-operation. $ perl perl1.pl Placido P. Octopus Polypacido P. Octopus Placido P. Octopus Placido P. Octopus $ cat perl1.pl #!/usr/bin/perl use strict; use warnings; my $string = "Placido P. Octopus\n"; my $regex = "P."; print $string; $string =~ s/$regex/Polyp/; print $string; my $string2 = "Placido P. Octopus\n"; print $string2; $string =~ s/$regex/Polyp/g; print $string2; $ I don't know what I'm doing wrong here. Semi-colons are there. Strings have newlines. Syntax looks like pp. 147 & 153 in Programming Perl. ?? -- fred
From: J�rgen Exner on 31 Mar 2010 16:08 Phred Phungus <Phred(a)example.invalid> wrote: >J�rgen Exner wrote: >> Phred Phungus <Phred(a)example.invalid> wrote: >>> J�rgen Exner wrote: >>>> Phred Phungus <Phred(a)example.invalid> wrote: >>>>> PerlFAQ Server wrote: >>>>>> $string = "Placido P. Octopus"; >>>>>> $regex = "P."; >>>>>> >>>>>> $string =~ s/$regex/Polyp/; >>>>>> # $string is now "Polypacido P. Octopus" >>>>>> >>>>>> Because "." is special in regular expressions, and can match any single >>>>>> character, the regex "P." here has matched the <Pl> in the original >>>>>> string. >>>>>> >>>>> Am I then correct that a period is not a character? >>>> How did you come to that conclusion? >>> Because the regex would then match the middle initial and the string >>> would be "Polypacido Polyp Octopus". >> >> No, it wouldn't because there is no /g modifier in the s///-operation. > >$ perl perl1.pl >Placido P. Octopus >Polypacido P. Octopus >Placido P. Octopus >Placido P. Octopus >$ cat perl1.pl >#!/usr/bin/perl > >use strict; >use warnings; > >my $string = "Placido P. Octopus\n"; >my $regex = "P."; >print $string; >$string =~ s/$regex/Polyp/; >print $string; > >my $string2 = "Placido P. Octopus\n"; >print $string2; >$string =~ s/$regex/Polyp/g; >print $string2; >$ > >I don't know what I'm doing wrong here. You showed us your code: good. You showed us your output: good. You did not tell us what output you expected nor how the actual output is different from your expected output: very bad. An error description should always include an "expected" section. To me your program produces exactly the output I would have expect: First the content of $string, then the same string again after a single substitution, then the content of $string2 then the identical content of $string2 again. >Semi-colons are there. Strings have newlines. Well, fine, but not really relevant. >Syntax looks like pp. 147 & 153 in Programming Perl. Well, yeah, perl didn't report a syntax error, so in all probability you don't have one. jue
From: Willem on 31 Mar 2010 16:09 Phred Phungus wrote: ) $ perl perl1.pl ) Placido P. Octopus ) Polypacido P. Octopus ) Placido P. Octopus ) Placido P. Octopus ) $ cat perl1.pl ) #!/usr/bin/perl ) ) use strict; ) use warnings; ) ) my $string = "Placido P. Octopus\n"; ) my $regex = "P."; ) print $string; ) $string =~ s/$regex/Polyp/; ) print $string; ) ) my $string2 = "Placido P. Octopus\n"; ) print $string2; ) $string =~ s/$regex/Polyp/g; ) print $string2; ) $ ) ) I don't know what I'm doing wrong here. Semi-colons are there. Strings ) have newlines. Syntax looks like pp. 147 & 153 in Programming Perl. You made a typo, that's what. For education, I'll let you figure it out yourself. PS: Good thing that you copy-pasted the code. That shows very nicely why copy-pasting is the only way to go when asking usenet questions. SaSW, Willem -- Disclaimer: I am in no way responsible for any of the statements made in the above text. For all I know I might be drugged or something.. No I'm not paranoid. You all think I'm paranoid, don't you ! #EOT
From: sln on 31 Mar 2010 16:11 On Wed, 31 Mar 2010 13:56:17 -0600, Phred Phungus <Phred(a)example.invalid> wrote: >I don't know what I'm doing wrong here. >>>>>> Because "." is special in regular expressions, and can match any single >>>>>> character What is it you are doing? $hhh = "ABCDE"; $hhh =~ s/././g; print $hhh;
From: Phred Phungus on 31 Mar 2010 16:45
Willem wrote: > Phred Phungus wrote: > ) my $string2 = "Placido P. Octopus\n"; > ) print $string2; > ) $string =~ s/$regex/Polyp/g; > ) print $string2; > ) $ > ) > ) I don't know what I'm doing wrong here. Semi-colons are there. Strings > ) have newlines. Syntax looks like pp. 147 & 153 in Programming Perl. > > You made a typo, that's what. > For education, I'll let you figure it out yourself. > > PS: Good thing that you copy-pasted the code. That shows very nicely > why copy-pasting is the only way to go when asking usenet questions. Uff. $ perl perl1.pl Placido P. Octopus Polypacido P. Octopus Placido P. Octopus Polypacido Polyp Octopus $ cat perl1.pl #!/usr/bin/perl use strict; use warnings; my $string = "Placido P. Octopus\n"; my $regex = "P."; print $string; $string =~ s/$regex/Polyp/; print $string; my $string2 = "Placido P. Octopus\n"; print $string2; $string2 =~ s/$regex/Polyp/g; print $string2; $ perl1.pl bash: perl1.pl: command not found $ Ok the regex part of it works, now, but I've got one more little thing that doesn't really have to do with perl properly, but since the context is here ... I thought the idea for the shebang line was that my OS was going to interpret the script with perl. I do have a /usr/bin/perl, so I thought that typing perl1.pl at the prompt would be the same as typing perl perl1.pl, but manifestly, it is not. What gives? -- fred |