Chris Schults wrote:
Thanks to the people who promptly replied.

I have two quick follow-up questions:

1) What does "s/\s*$/\./" do? (see email below)

It effectively puts a period at the end of the string, while deleting any extra spaces at the end.


2) What are the differences between:

$string .= '.' unless $string =~ /[$!.]+$/; (suggested in another email)

literally: append (.=) a period to $string if $string ends with one or more of dollar ($), exclamation (!), or period (.).


and

$title =~ s/\s*$/\./ unless $title =~ /[!?.]\s*$/;

literally: replace zero or more spaces just before the end of $title, along with the psuedo end of line token ($) with a period Unless $title ends with a single exclamation (!), question (?), or a period (.) and zero or more spaces.


Note the escape in the substitution part is unnecessary:

  $title =~ s/\s*$/./

is equivelant. In the substitution part, only escape characters that you would escape in a double quoted string - generally.

#####

Assuming the string is clean (i.e. no extra spaces), appending the period is faster than a substituion regex, so I'd use:

$string .= '.'

to add the period.

For checking puctuation you can use the [:punct:] character class

$string =~ /[[:punct:]]$/

will detect punctuation at the end of the string, as will:

substr( $string, -1 ) =~ /[[:punct:]]/

So any of the following will give you the results you want:

unless ( $string =~ /[[:punct:]]$/ ) {
  $string .= '.';
}

unless ( substr( $string, -1 ) =~ /[[:punct:]]/ ) {
  $string .= '.';
}

or their respective abbreviated forms:

$string .= '.' unless $string =~ /[[:punct:]]$/;

$string .= '.' unless substr( $string, -1 ) =~ /[[:punct:]]/;

Hmm, that's probably more than you wanted...


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to