Как я могу использовать только определенные слова в тексте? - PullRequest
0 голосов
/ 10 мая 2011

У меня есть текст:

Блок текста - это набор строк. В случае 'left', 'right' и 'center', это свойство определяет, как линейные блоки внутри каждого линейного блока выравниваются относительно левой и правой сторон линейного блока; выравнивание не по отношению к окну просмотра. В случае «justify» это свойство указывает, что блоки встроенного уровня должны быть сделаны заподлицо с обеими сторонами линейного блока, если это возможно, путем расширения или сжатия содержимого встроенных блоков, иначе выровненных, как для начального значения. (См. Также «межбуквенный интервал» и «межсимвольный интервал».)

Все слова в тексте с "th" должны быть написаны в верхнем регистре (используя функцию uc).

Вот мой код

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

Что мне написать дальше?

Ответы [ 2 ]

3 голосов
/ 10 мая 2011

Это просто замена.

use strict;
use warnings;

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

$string =~ s/\b(\S*th\S*)\b/uc $1/ieg;
print $string;
2 голосов
/ 10 мая 2011
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";
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...