Perl,计算字符并在单词末尾中断

标签 perl

我试图在 300 个字符处打断,效果很好,但我不希望它在单词中间打断。我想找到一个词的结尾或下一个可用的空格。任何帮助将不胜感激。

foreach (@$comp) {
    my $c = $_;
    $c->{reviews} = ($c->{reviews} - 1);
    my $stars;
    if($c->{reviews} == 0) {
        $stars = 0;
    } else {
        $stars = int($c->{reviews_total} / $c->{reviews});
    } 
    $c->{stars} = $stars; 

    if($c->{title}) { 
        $c->{name} = $c->{title}; 
        $c->{event} = "FOO"; 
    }
    $c->{description} =~ s/BREAK//g; 

    my (@desc) = split(//, $c->{description});   

    my $adesc;
    my $rowc = 0;
    my $count = 0;

    while($count < 300 ) {
         $adesc .= @desc[$count];
         $rowc++;
         $count++;
    }

    $c->{description} = $adesc;= '...';
}

最佳答案

让我们看看是什么perldoc perlfaq4不得不说:

How do I reformat a paragraph?

Use Text::Wrap (part of the standard Perl distribution):

use Text::Wrap;
print wrap("\t", '  ', @paragraphs);

The paragraphs you give to Text::Wrap should not contain embedded newlines. Text::Wrap doesn't justify the lines (flush-right).


如果你不喜欢怎么办?

访问CPAN

关于Perl,计算字符并在单词末尾中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8928331/

相关文章:

perl selenium:Test::Exception、正确用法和好的代码示例?

c++ - perl 正则表达式比 c++/boost 更快

perl - 如何在 mod_perl 中限制 HTTP POST 请求的大小?

perl - 在 Perl 中识别空字符串

perl - 在 perl 中使用 lib 和 if pragma

c++ - 如何将多维映射从 c++ 转换为 perl 中的哈希

linux - 安装 DBD::Oracle 时出错

perl - $x->{a}{b} 和 $x->{a}->{b} 的区别

bash - 将 stdout 通过管道传输到 Perl 时,如何让它打印换行符?

c# - 在 C# 中从自定义 GUI 运行外部命令