在 Perl 中逐字而不是逐行读取文本文件 [英] Read text file in Perl word by word instead of line by line

查看:48
本文介绍了在 Perl 中逐字而不是逐行读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的 (300 kB) 文本文件,其中包含以空格分隔的单词.现在我想打开这个文件,把里面的每一个字都一一处理.

I have a big (300 kB) text file containing words delimited by spaces. Now I want to open this file and process every word in it one by one.

问题是 perl 一次一行地读取文件(即整个文件),这给了我奇怪的结果.我知道正常的方法是做类似

The problem is that perl reads the file line by line (i.e) the entire file at once which gives me strange results. I know the normal way is to do something like

open($inFile, 'tagged.txt') or die $!;
$_ = <$inFile>;
@splitted = split(' ',$_);
print $#splitted;

但这给了我错误的字数统计(数组太大?).

But this gives me a faulty word count (too large array?).

是否可以逐字读取文本文件?

Is it possible to read the text file word by word instead?

推荐答案

要一次读取文件一个词,请将输入记录分隔符 ($/) 更改为空格:

To read the file one word at a time, change the input record separator ($/) to a space:

local $/ = ' ';

示例:

#!/usr/bin/perl
use strict;
use warnings;

use feature 'say';

{
    local $/ = ' ';

    while (<DATA>) {
        say;
    }
}

__DATA__
one two three four five

输出:

one 
two 
three 
four 
five

这篇关于在 Perl 中逐字而不是逐行读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆