gowrisub has asked for the wisdom of the Perl Monks concerning the following question:

How would you print the last three letters of each word? Given the string "The quick brown fox jumped over the lazy dog"

Replies are listed 'Best First'.
Re: Array of words
by jwkrahn (Abbot) on Oct 21, 2008 at 06:49 UTC
    $ perl -le' $_ = "The quick brown fox jumped over the lazy dog"; print; s/[[:alpha:]]+(?=[[:alpha:]]{3})//g; print; ' The quick brown fox jumped over the lazy dog The ick own fox ped ver the azy dog
      Thanks all for your valuable time to give the solution. Regards, Gowri
Re: Array of words
by ccn (Vicar) on Oct 21, 2008 at 06:01 UTC
    my $str = 'The quick brown fox jumped over the lazy dog'; print $1, "\n" while $str =~ /(\w\w\w)\b/g;
    What should be done if tho-letter word appears?
Re: Array of words
by ww (Archbishop) on Oct 21, 2008 at 11:56 UTC
    Note that jwkrahn's excellent solution above works well if the string is:
    This is my homework?
Re: Array of words
by luckypower (Beadle) on Oct 21, 2008 at 05:58 UTC
    split the string by space
    split(" ", $str); now
    foreach (split(" ",$str)) { print substr($_, -3); }
    substr will give you the substring.
      that does not work with punctuation marks in a sentence
Re: Array of words
by cdarke (Prior) on Oct 21, 2008 at 12:38 UTC
    TMTOWTDI
    $_ = 'The quick brown fox jumped over the lazy dog'; local $,=' '; print /(\w{1,3}\b)/g,"\n";
Re: Array of words
by MidLifeXis (Monsignor) on Oct 21, 2008 at 14:56 UTC

    A little late for this response, but is this homework?

    --MidLifeXis

Re: Array of words
by kyle (Abbot) on Oct 21, 2008 at 15:26 UTC

    Just for the TIMTOWTDI of it all...

    $\ = "\n"; $_ = "The quick brown fox jumped over the lazy dog"; print join q{ } map { substr $_, -3 } split /[^a-z]*\s+[^a-z]*/i; __END__ The ick own fox ped ver the azy dog