Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.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的错误_Perl - Fatal编程技术网

&引用;“内存不足”;处理文件时来自Perl的错误

&引用;“内存不足”;处理文件时来自Perl的错误,perl,Perl,我正在使用Perl处理大量文本文件。我需要计算所有以某些后缀结尾的单词的出现次数 运行此代码会不断返回内存不足错误消息,例如,超过40个文件的批处理 有没有什么方法可以比我下面所做的更有效地完成这项任务(更少的内存使用) #!/software/perl512/bin/perl use strict; use warnings; use autodie; use Mojo::DOM; my $path = "/data/10K/2012"; chdir($path) or die "Can

我正在使用Perl处理大量文本文件。我需要计算所有以某些后缀结尾的单词的出现次数

运行此代码会不断返回
内存不足
错误消息,例如,超过40个文件的批处理

有没有什么方法可以比我下面所做的更有效地完成这项任务(更少的内存使用)

#!/software/perl512/bin/perl

use strict;
use warnings;
use autodie;

use Mojo::DOM;

my $path = "/data/10K/2012";
chdir($path) or die "Cant chdir to $path $!";

# This program counts the total number of suffixes of a form in a given document.

my @sequence;
my %sequences;
my $file;
my $fh;
my @output;

# Reading in the data.
for my $file (<*.txt>) {

   my %affixes;
   my %word_count;

   my $data = do {
      open my $fh, '<', $file;
      local $/;    # Slurp mode
      <$fh>;
   };

   my $dom  = Mojo::DOM->new($data);
   my $text = $dom->all_text();

   for (split /\s+/, $text) {
      if ($_ =~ /[a-zA-Z]+(ness|ship|dom|ance|ence|age|cy|tion|hood|ism|ment|ure|tude|ery|ity|ial)\b/ ) {
         ++$affixes{"affix_count"};
      }
      ++$word_count{"word_count"};
   }

   my $output = join ",", $file, $affixes{"affix_count"}, $word_count{"word_count"};

   push @output, ($output);
}

@output = sort @output;

open(my $fh3, '>', '/home/usr16/rcazier/PerlCode/affix_count.txt');
foreach (@output) {
   print $fh3 "$_\n ";
}
close $fh3;
#/软件/perl512/bin/perl
严格使用;
使用警告;
使用自动模具;
使用Mojo::DOM;
my$path=“/data/10K/2012”;
chdir($path)或die“Cant chdir to$path$!”;
#此程序统计给定文档中表单的后缀总数。
我的@序列;
我的%序列;
我的$file;
我的$fh;
我的@output;
#读取数据。
对于我的$file(){
我的%词缀;
我的%字数;
我的$data=do{
打开我的$fh,','/home/usr16/rcazier/PerlCode/affix_count.txt');
foreach(@output){
打印$fh3“$\n”;
}
收盘价$fh3;

这是我能找到的最接近的解决方案。它包含了注释中的所有要点,并通过保留所有HTML标记不变来解决“内存不足”错误。它还保留未排序的结果,因为原始代码实际上没有做任何有用的排序

由于您查找后缀词的方式,我认为在文本文件中保留HTML标记不太可能显著地扭曲结果

#!/software/perl512/bin/perl

use strict;
use warnings;
use 5.010;
use autodie;

# Build and compile a regex that will match any of the suffixes that interest
# us, for later use in testing each "word" in the input file
#
my $suffix_re = do {
   my @suffixes  = qw/ ness ship dom ance ence age cy tion hood ism ment ure tude ery ity ial /;
   my $alternation = join '|', @suffixes;
   qr/ (?: $alternation ) /xi;
};

# Set the directory that we want to examine. `autodie` will check the success
# of `chdir` for us
#
my $path = '/data/10K/2012';  
chdir $path;

# Process every file with a `txt` file type
#
for my $filename ( grep -f, glob('*.txt') ) {

   warn qq{Processing "$filename"\n};

   open my ($fh), '<', $filename;

   my ($suffixes, $word_count) = (0, 0);

   while (<$fh>) {
      for (split) {
         ++$word_count;
         ++$suffixes if /\A[a-z]+$suffix_re\z/i;
      }
   }

   say join ',', $filename, $suffixes, $word_count if $suffixes;
}
!/software/perl512/bin/perl
严格使用;
使用警告;
使用5.010;
使用自动模具;
#构建并编译一个正则表达式,该正则表达式将匹配任何感兴趣的后缀
#供以后在测试输入文件中的每个“单词”时使用
#
我的$suffix\u re=do{
我的@suffix=qw/ness-ship-domance-age-cy-hood-ism-tude-ery-icial/;
my$alternation=join'|',@后缀;
qr/(?:$alternation)/xi;
};
#设置要检查的目录。`autodie`将检查是否成功
#为我们准备的'chdir'
#
我的$path='/data/10K/2012';
chdir$path;
#使用“txt”文件类型处理每个文件
#
对于我的$filename(grep-f,glob('*.txt')){
警告qq{正在处理“$filename”\n};

打开我的($fh),“标题与问题有什么关系?”“内存不足”不是语法错误。我对你的问题做了很大的修改,以便我自己能更好地理解它,希望它能帮助其他人帮助你。请检查它是否准确。
Mojo::DOM
用于处理HTML文件。请编辑“文本”文件包含HTML?HTML文件具有
txt
文件类型是不常见的。我关于哈希与标量的评论不会影响内存使用,这只是代码中几个没有太多意义的例子之一。基于此和你之前的问题,你似乎在一块一块地把事情拼凑起来基于堆栈溢出的答案,但可能对每一部分以及它们如何组合缺乏基本的理解。您是否已经完成了一个很好的现代Perl教程?如果您正在使用学习Perl(只要它是相对较新的版本),那就太好了。我想指出您的代码中有许多地方可以改进,其中许多与您的内存问题无关。唉,评论中没有足够的空间,所以最突出的地方是:1)正如Borodin所说,对常规文本文件使用
Mojo::DOM
没有意义。2)逐行处理文件不要将它们存储在内存中。3)要复制文件,请使用,而不要将其读入数组,然后再将其写入。@Borodin您的代码工作得很好!我可以使用您的代码运行数据,而不会耗尽我的内存。谢谢!@Rick:不客气,我很高兴我能够提供帮助。今后,请您尽量描述一下仔细考虑你的代码应该解决的问题,以及那些不适合你的代码。我不得不从Perl反向工作,以对原始问题可能是什么进行最佳猜测,然后编写一些代码来解决我的猜测。我意识到指定问题可能很难,但我们经常遇到这样的情况,这令人沮丧提供了一个建议的解决方案和一个不起作用的程序。原始问题对我们帮助您至关重要。