in reply to Function over all letters in a string

For this simple task I would use the s/// solution, too. But if you have to have more control over what you're doing:
use strict; use warnings; for (1..5) { my $string = q{It's a cat and mouse game.}; my $i = length($string) + 1; CHAR: while ($i--) { for my $c (substr($string, $i, 1)) { next CHAR unless $c =~ /^[a-zA-ZäöüßÄÖÜ]$/; # do whatever you want here $c .= $c x rand(7); } } print $string, "\n"; }

Search, Ask, Know

Replies are listed 'Best First'.
Re: Re: Function over all letters in a string
by allolex (Curate) on Dec 01, 2003 at 07:48 UTC

    Here's a little trick you can use in Latin1 character encoding (instead of locales) to avoid having to name all non-US word characters explicitly: just define the character set as [A-Za-zÀ-ÿ]. If you look at a Latin1 character table, you'll see what it does--and it works for more than just German. :)

    --
    Allolex

      Good point. But I think even more dangerous that using only [a-z] (my example, too). I think this should work quite well:
      next CHAR if $c =~ /^[[:punct:][:cntrl:][:blank:]]$/;
      Add [:digit:] to exclude numbers, too.

      Search, Ask, Know