Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Perl程序中将行包装为45个字符?_Perl - Fatal编程技术网

如何在Perl程序中将行包装为45个字符?

如何在Perl程序中将行包装为45个字符?,perl,Perl,我在Perl CGI程序中编写了以下文本: $text = $message; @lines = split(/\n/, $text); $lCnt .= $#lines+1; $lineStart = 80; $lineHeight = 24; 我想在45个字符后强制返回。我在这里怎么做 提前感谢您的帮助。退房。它会完全满足你的需要 看看核心模块: 由于某种原因,Text::Wrap对OP不起作用,下面是一个使用正则表达式的解决方案: my $longstring = "lots of te

我在Perl CGI程序中编写了以下文本:

$text = $message;
@lines = split(/\n/, $text);
$lCnt .= $#lines+1;
$lineStart = 80;
$lineHeight = 24;
我想在45个字符后强制返回。我在这里怎么做

提前感谢您的帮助。

退房。它会完全满足你的需要

看看核心模块:


由于某种原因,
Text::Wrap
对OP不起作用,下面是一个使用正则表达式的解决方案:

my $longstring = "lots of text to wrap, and some more text, and more "
               . "still.  thats right, even more. lots of text to wrap, "
               . "and some more text.";

my $wrap_at = 45;

(my $wrapped = $longstring) =~ s/(.{0,$wrap_at}(?:\s|$))/$1\n/g;

print $wrapped;
其中打印:

lots of text to wrap, and some more text, and 
more still.  thats right, even more. lots of 
text to wrap, and some more text.
text::Wrap
相比,该模块可以对非英语文本(尤其是东亚脚本)进行更复杂的包装,并具有一些很好的功能,比如可以选择性地识别URI并避免拆分它们

例如:

#/usr/bin/env perl
使用警告;
严格使用;
使用Unicode::LineBreak;
my$longstring=“要包装的文本很多,还有更多的文本,等等”
. “仍然如此。没错,甚至更多。有很多文本需要包装,”
. “还有一些文字。”;
我的$wrapper=Unicode::LineBreak->new(ColMax=>45,Format=>NEWLINE);
打印$wrapper->break($longstring);

谢谢。很抱歉,我执行此操作时收到一条内部服务器错误消息。您的内部服务器错误为500,这是您的问题。这并不会降低答案的正确性。请看。请看。需要考虑的几个边缘情况:(1)如果您有一个超过45个字母的字符串,但没有空格,会发生什么情况?(2) 给定一系列跨越边界的空格,位置45及以上的空格是应该放在新行的开头还是旧行的结尾,还是应该全部删除?可能重复的答案是“如何在perl中模拟unix fmt?”我认为,这个明确的问题应该在本文档的某个地方提到,所以有这个问题的人会被带到这份文件。我搜索了这个问题,没有找到任何相关的内容,用示例将问题发布到stackoverflow,我的问题很快被“关闭”并引用到这里。确切的标题是“在perl中,我如何模拟unix fmt?”幸运的是,这个问题现在将读者引向了这篇文章。只是有点晚了,但另一个问题刚刚结束,作为这个问题的复制品,所以。。。
lots of text to wrap, and some more text, and 
more still.  thats right, even more. lots of 
text to wrap, and some more text.