0

我在数组中有一个字符串,我想将它写入文件。问题是我需要写一些限制在 100 以内的字符,然后我必须回到行并完成句子的内容。然后,如果我再次遇到 100 个字符,我将返回该行,直到我完成表中的链内容。

4

2 回答 2

1

Text::Wrap模块可能会做您想做的事,并且是核心 Perl 的一部分,这意味着您不必安装它。

该程序从DATA伪句柄中读取文本并重新格式化。

use strict;
use warnings;

use Text::Wrap;

my @text = <DATA>;
chomp @text;

$Text::Wrap::columns = 100;
print Text::Wrap::wrap '', '', @text;

__DATA__
"Text::Wrap::wrap()" is a very simple paragraph formatter. It formats a
single paragraph at a time by breaking lines at word boundaries.
Indentation is controlled for the first line ($initial_tab) and all
subsequent lines ($subsequent_tab) independently. Please note:
$initial_tab and $subsequent_tab are the literal strings that will be
used: it is unlikely you would want to pass in a number.

Text::Wrap::fill() is a simple multi-paragraph formatter. It formats
each paragraph separately and then joins them together when it's done.
It will destroy any whitespace in the original text. It breaks text into
paragraphs by looking for whitespace after a newline. In other respects
it acts like wrap().

Both "wrap()" and "fill()" return a single string.

输出

"Text::Wrap::wrap()" is a very simple paragraph formatter. It formats a single paragraph at a time
by breaking lines at word boundaries. Indentation is controlled for the first line ($initial_tab)
and all subsequent lines ($subsequent_tab) independently. Please note: $initial_tab and
$subsequent_tab are the literal strings that will be used: it is unlikely you would want to pass in
a number.  Text::Wrap::fill() is a simple multi-paragraph formatter. It formats each paragraph
separately and then joins them together when it's done. It will destroy any whitespace in the
original text. It breaks text into paragraphs by looking for whitespace after a newline. In other
respects it acts like wrap().  Both "wrap()" and "fill()" return a single string.
于 2012-04-11T15:40:41.383 回答
0

我认为您正在寻找诸如Text::ReformText::Autoformat 之类的东西。

于 2012-04-11T14:23:27.003 回答