如何使用Perl拆分文本文件并将其存储到2d数组中? [英] How can I split up a text file and store it into a 2d array using Perl?

查看:119
本文介绍了如何使用Perl拆分文本文件并将其存储到2d数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

230215 01/16/2000 57533 "" 0 1045403 "" ? 0 0 217623

230215 01/18/2000 77659 "" 0 1045403 "" ? 0 0 217624

230215 01/25/2000 76583 "" 0 1045403 "" ? 0 0 217625

230215 01/29/2000 58082 "" 0 1045403 "" ? 0 0 217626

230216 01/14/2000 50020 "" 0 1045403 "" ? 0 0 217627

230216 01/18/2000 66444 "" 0 1045403 "" ? 0 0 217628

230216 01/19/2000 51330 "" 0 1045403 "" ? 0 0 217629

所有字段均由空格分隔,我需要将其存储到2d数组中.实际上有500万条这样的线路.例如,我想将其存储到$ arr [7] [11]这样的数组中,因为每行中有7行和11个值.

All of the fields are separated by spaces, I need to store this into a 2d array. There are actually 5 million lines like these. For example, i would want to store this into an array like $arr[7][11], because there are 7 lines and 11 values in each line.

推荐答案

我认为您遇到的问题是,Perl数组只能是单个数据的数组.您可以将每一行都放在一个数组中,但是您不想存储单个数据,而是想要存储11个数据.

The problem I believe you're having is the fact that Perl arrays can only be an array of a single piece of data. You can have each line in an array, but you don't want to store a single piece of data, you want to store 11 pieces of data.

幸运的是,Perl允许您将引用作为数据存储在数组中.该 reference 可以指向...例如...另一个数组.看一下 Perl参考教程,它应该可以帮助您了解如何完成此操作.

Fortunately, Perl allows you to store references as a piece of data in your array. That reference can point to ...say... another array. Take a look at the Perl Reference Tutorial, and that should help you understand how this can be done.

这确实是一个非常简单的问题.首先,让我们做一个循环,读取每一行并将每一行放入名为@file_array的数组中.

This is really a fairly simple problem. First, let's make a loop that reads each line and puts each line into an array called @file_array.

use strict;
use warnings;
use autodie;  #So I dont have to worry about my file

open (my $fh, "<", "dataFile.txt");
my @file_array;
while (my $line = <$fh>) {
    chomp $line;
    push (@file_array, $line);
}

现在,让我们创建一个程序,将每一行取为一行,并将其拆分为一个数组:

Now, let's make a program that takes each line, and splits it into an array:

use strict;
use warnings;
use autodie;  #So I dont have to worry about my file

open (my $fh, "<", "dataFile.txt");
my @file_array;
while (my $line = <$fh>) {
    chomp $line;
    my @line_array = split (/\s+/, $line);
}

这两个程序只有一行不同.第一个将文件拆分为一个数组,第二个读取每行并将其拆分为一个数组.

The two programs only differ in one line. The first splits the file into an array, and the second reads each line and splits it into an array.

让我们结合两个程序.而且,不是将$line放入@file_array的每个元素中,而是将@line_array * reference 放入我的@file_array的每个元素中:

Let's combine the two programs. And, instead of putting $line into each element of my @file_array, I'm going to be putting the *reference of @line_array into each element of my @file_array:

use strict;
use warnings;
use autodie;  #So I dont have to worry about my file

open (my $fh, "<", "dataFile.txt");
my @file_array;
while (my $line = <$fh>) {
    chomp $line;
    my @line_array = split(/\s+/, $line);
    push (@file_array, \@line_array);
}

如果我想谈谈文件中的第三行,它将作为参考存储在$file_array[2]中.我可以取消引用$file_array[2] by putting it in $ {} , and this would get me back my @ line_array`:

If I want to talk about the third line in my file, it's stored as a reference in $file_array[2]. I can dereference $file_array[2] by putting it in${}, and this would get me back my@line_array`:

my @line_array = ${$file_array[2]};

现在,如果我想谈该行的第四项,我可以说:

Now, if I want to talk about the fourth item on that line, I can say:

my $element = $line_array[3];

但是,我也可以将两个操作合并到一行.下面,我取消引用存储在$file_array[2]中的数组,并同时获取第四个元素(元素#3):

But, I can also combine the two operations onto a single line. Below, I am dereferencing the array stored in $file_array[2] and taking the fourth element (element #3) at the same time:

my $element = ${$file_array[2]}[3];

不清楚吗?幸运的是,Perl具有->运算符,该运算符使您可以取消引用数组而无需使用${}语法.这是更容易阅读的方式:

Not to clear? Fortunately, Perl has an -> operator which allows you to dereference the array without using the ${} syntax. This is way easier to read:

my $element = $file_array[2]->[3];

在现代Perl程序中,您会看到很多.这就是我谈论数组数组的方式.实际上,Perl甚至允许您在数组数组中完全删除数组之间的箭头运算符.您可以通过以下方式讨论此元素:

You'll see this a lot in modern Perl programs. This is how I talk about my array of arrays. In fact, Perl even allows you in arrays of arrays to completely remove the arrow operator between arrays. You can talk about this element this way:

my $element = $file_array[2][3];

这篇关于如何使用Perl拆分文本文件并将其存储到2d数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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