perl

How can I uppercase only certain words in some text?


I have some text:

A block of text is a stack of line boxes. In the case of 'left', 'right' and 'center', this property specifies how the inline-level boxes within each line box align with respect to the line box's left and right sides; alignment is not with respect to the viewport. In the case of 'justify', this property specifies that the inline-level boxes are to be made flush with both sides of the line box if possible, by expanding or contracting the contents of inline boxes, else aligned as for the initial value. (See also 'letter-spacing' and 'word-spacing'.)

All the words in the text with "th" should be written in uppercase (using function uc).

Here is my code:

@tykeldatud = split(/ /, $string);
$j = @tykeldatud;
for ($i=1; $i<=$j; $i++) {
    ...

What should I write next?


Solution

  • use warnings;
    use strict;
    
    my $string = <<EOF;
    A block of text is a stack of line boxes.  In the case of 'left',
    'right' and 'center', this property specifies how the inline-level
    boxes within each line box align with respect to the line box's left
    and right sides; alignment is not with respect to the viewport.  In
    the case of 'justify', this property specifies that the inline-level
    boxes are to be made flush with both sides of the line box if
    possible, by expanding or contracting the contents of inline boxes,
    else aligned as for the initial value.  (See also 'letter-spacing' and
    'word-spacing'.)
    EOF
    
    my $string2;
    for (split / /, $string) {
        $_ = uc if /th/i;
        $string2 .= "$_ ";
    }
    print "$string2\n";