On Wed, Feb 22, 2012 at 4:45 PM, Jim Gibson wrote:
fails to substitute only the first instance of the matched pattern?
So, if John suggestion doesn't work as it should, then you may have to
Setting $/ will not affect the results of the substitution. It will affect
reading a file, but you are not reading a file within the scope of the
modified $/ variable.
Why Not? If your while(<>){...} is within the scope of { local $/;At 9:02 AM +0100 2/22/12, timothy adigun wrote:
with some very funny text files.
Could you please provide an example of where the given regular expressionOn Wed, Feb 22, 2012 at 3:43 AM, John W. Krahn wrote:
sb@missionstclare.com wrote:
The line
does not use the /g option so it will only replace the first 'george' it
finds.
Correct, but sometimes this doesn't work all of the time, especiallysb@missionstclare.com wrote:
The line
$text=~s/george/tim/;
causes a global substituion of "george" with "tim"
How can I limit the substituion to the first instance only?
Global substitution only works if you use the /g option but your examplecauses a global substituion of "george" with "tim"
How can I limit the substituion to the first instance only?
does not use the /g option so it will only replace the first 'george' it
finds.
with some very funny text files.
fails to substitute only the first instance of the matched pattern?
So, if John suggestion doesn't work as it should, then you may have to
enable slurp mode like this:
$/=undef or local $/;
So your code could read:
{
.....
$/=undef; ## or use local $/;
$text=~s/george/tim/;
........
}
$/=undef or local $/;
So your code could read:
{
.....
$/=undef; ## or use local $/;
$text=~s/george/tim/;
........
}
Setting $/ will not affect the results of the substitution. It will affect
reading a file, but you are not reading a file within the scope of the
modified $/ variable.
....}, atleast that is what am suggesting.
You can use the File::Slurp module to read a file into a scalar variable.
Also check out 'perldoc -q entire' "How can I read an entire file all at
once?"
Please, I don't mean to sound arrogant, but 'perldoc -q entire' "How
can I read an entire file all at once?" add nothing to me, because allAlso check out 'perldoc -q entire' "How can I read an entire file all at
once?"
Please, I don't mean to sound arrogant, but 'perldoc -q entire' "How
brian d foy mentioned is what I think any serious Perl programmer should
know.
Agreed File::Slurp will be faster and better as Uri mentioned.
You could check *perldoc perlvar* for more information.
We don't know if the original poster was applying the substitution to anentire file or to each line in a file. We don't even know if sb was even
working with files at all.
problem he won't be asking. Will he?
And if I haven't seen something 'like' that before, I won't be stating it.
All I did, when **s/college/SCHOOL/;** wouldn't work was:
{
local $/;
while(<>){
.......
s/college/SCHOOL/;
........
}
}
Bingo! The job was done!
--
Tim