【问题标题】:truncate all lines in a file while preserving whole words截断文件中的所有行,同时保留整个单词
【发布时间】:2017-06-19 15:23:02
【问题描述】:

我正在尝试将文件的每一行缩短为 96 个字符,同时保留整个单词。如果一行小于或等于 96 个字符,我不想对该行执行任何操作。如果它超过 96 个字符,我希望它减少到最接近的小于 96 的数量,同时保留整个单词。当我运行这段代码时,我得到一个空白文件。

use Text::Autoformat;

use strict;
use warnings;

#open the file
my $filename = $ARGV[0]; # store the 1st argument into the variable
open my $file, '<', $filename;
open my $fileout, '>>', $filename.96;

my @file = <$file>;  #each line of the file into an array

while (my $line = <$file>) {
  chomp $line;
  foreach (@file) {
#######
sub truncate($$) {
    my ( $line, $max ) = @_;

    # always do nothing if already short enough 
    ( length( $line ) <= $max ) and return $line;

    # forced to chop a word anyway
    if ( $line =~ /\s/ ) {
       return substr( $line, 0, $max );
    }
    # otherwise truncate on word boundary 
    $line =~ s/\S+$// and return $line;

    die; # unreachable
}
####### 

my $truncated  = &truncate($line,96);

print $fileout "$truncated\n";

  }
}       
close($file);
close($fileout);

【问题讨论】:

标签: perl foreach while-loop truncate truncated


【解决方案1】:

你没有输出,因为你没有输入。

1. my @file = <$file>;  #each line of the file into an array
2. while (my $line = <$file>) { ...

&lt;$file&gt; 操作第 1 行在列表上下文中“消耗”所有输入并将其加载到 @file。第 2 行中的 &lt;$file&gt; 操作没有更多输入要读取,因此 while 循环不会执行。

你要么想从文件句柄流式传输

# don't call @file = <$file>
while (my $line = <$file>) {
    chomp $line; 
    my $truncated = &truncate($line, 96);
    ...
}

或者从文件内容数组中读取

my @file = <$file>;
foreach my $line (@file) {
    chomp $line; 
    my $truncated = &truncate($line, 96);
    ...
}

如果输入很大,前一种格式的优点是一次只将一行加载到内存中。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2010-12-09
  • 2011-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-09
相关资源
最近更新 更多