Re: substition in perl



dennisha...@xxxxxxxxx wrote:

Trying to figure out the substitution operator in Perl.

The documentation shows the syntax as

s/PATTERN/REPLALCEMENT/

and explains that the operator searches a string for PATTERN and
replaces that match with the REPLACEMENT text.

My question then is this. What string is being searched? The
documention goes on to say that if no string is specified, the $_
variable is search.

PATTERN is searched and replaced by REPLACEMENT.

Example:
$s = 'ABC'; # assign value ABC to variable $s
$s =~ s/B/D/; # replace B into D in variable $s
print $s; # print the result (ADC)

What is the $_ variable?

This is the default variable. "where no parameter name is given for"

Example:
for (1..5) {
print $_;
}
# this prints 1 2 3 4 5 (each time the loop is being passed)

It would be the same as
for $number (1..5) {
print $number;
}

See http://learn.perl.org/

--
Bart

.



Relevant Pages

  • Re: [PHP] ereg_replace with user defined function?
    ... With a preg_replace_callback I am able to look for a pattern like: a number followed by whitespace followed by one, two, three or more characters, followed by a closing character. ... In that function I evaluate the unit, see if it is in my array containing the conversion "table". ... The metric / imperial calculations / replacements take between 0.00054 and 0.005 seconds per table cell / text string. ... replacement with preg_replace_callback compared to the function I wrote? ...
    (php.general)
  • multiple pattern replacement using regular expressions
    ... I needed to write an efficient pattern replacement class which could use ... Replaces multiple patterns within given string based on regular ... @param pattern a simple string or a regular expression ...
    (comp.lang.java.programmer)
  • Re: substition in perl
    ... Bart Van der Donck wrote: ... What string is being searched? ... PATTERN is searched and replaced by REPLACEMENT. ...
    (comp.lang.perl.misc)
  • Re: search/replace in Python
    ... I'm having some problems understanding Regexps in Python. ... Whether you are doing a substitution programatically in Python, or in any other language, or manually using a text editor, you need to specify some input text, a pattern, and a replacement. ... If not, then you shouldn't expect to learn how to use them from the documentation, which does adequately _document_ the provided functionality. ...
    (comp.lang.python)
  • TERM setting? strange chars in man page
    ... Suddenly I'm seeing strange characters in some documentation. ... It seems like it's always the same string. ... I see no pattern for where they appear other than they seem equally spaced. ...
    (comp.unix.programmer)