从文件中读取多行(作为滑动窗口)的最简单方法

时间:2014-04-16 00:16:04

标签: regex perl

在perl中,最简单的方法是读取一个文件,一次说4行?

如果文件包含随机单词,如

bye
how
are
you
car
bed
dog

我想用大小为4的滑动窗口阅读文件,所以我想读一个字符串

bye how are you
how are you car
are you car bed
you car bed dog

3 个答案:

答案 0 :(得分:3)

这是另一个例子。每一行都被推送到数组@window,如果这导致数组中包含四个元素,则打印数组,并使用shift从头开始删除最旧的元素。

use strict;
use warnings;

my @window;

while (<DATA>) {
  chomp;
  push @window, $_;
  if (@window == 4) {
    print "@window\n";
    shift @window;
  }
}

__DATA__
bye
how
are
you
car
bed
dog

<强>输出

bye how are you
how are you car
are you car bed
you car bed dog

答案 1 :(得分:2)

创建一个缓冲区,然后逐行处理;

use strict;
use warnings;

my @buffer = map {scalar <DATA>} (1..3);

while (<DATA>) {
    push @buffer, $_;

    # Show Buffer on a single line
    print join(' ', map {chomp; $_} @buffer), "\n";

    shift @buffer;
}

__DATA__
Line1
Line2
Line3
Line4
Line5
Lien6
Line7
Line8
Line9

输出:

Line1 Line2 Line3 Line4
Line2 Line3 Line4 Line5
Line3 Line4 Line5 Lien6
Line4 Line5 Lien6 Line7
Line5 Lien6 Line7 Line8
Lien6 Line7 Line8 Line9

<强>更新

要主动删除行结尾,请执行以下操作。输出将与上述相同:

my @buffer = map {$_ = <DATA>; chomp; $_} (1..3);

while (<DATA>) {
    chomp;
    push @buffer, $_;

    # Show Buffer
    print "@buffer\n";

    shift @buffer;
}

答案 2 :(得分:0)

将单词加载到数组中,然后可以使用pushshift循环数组,并打印数组切片:

use strict;
use warnings;

chomp(my @words = <DATA>);

for (1 .. 4) {
    print "@words[0..3]\n";
    push @words, shift @words;
}


__DATA__
bye
how
are
you
car
bed
dog

<强>输出:

how are you car
are you car bed
you car bed dog
car bed dog bye