English 中文(简体)
我如何用Perl把文字档案分开,将其储存在2个阵列中?
原标题:How can I split up a text file and store it into a 2d array using Perl?
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

所有的田间都按空间分开,我需要将它储存在2个阵列。 目前有500万条线路。 例如,我想将这笔资金储存到一个阵列,例如[7][11]美元,因为每一行各有7行和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.

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的阵列。

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);
}

现在,让我们制定一项方案,使其符合每一行,并分成一系列:

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);
}

这两个方案只有一个行文。 档案分为一阵,第二行各行各行,分为一阵。

让我们把这两个方案结合起来。 此外,将Im改为将* 参考 代码> @line_array的每个要素列入@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);
}

如果我想谈一下我档案中的第三行,则作为参考储存在<代码>上。 我可以通过在{<><>>>>>> > >t中加入这一编号来回避$file_array ,这将让我回到我的@line_array':

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

现在,如果我想谈谈关于这个项目的第四个项目,我可以说:

my $element = $line_array[3];

但是,我也可以把这两项行动合并为一个单行。 下面,我引述了在<代码>上储存的阵列——file_array ,同时将第四项要素(要素3)考虑在内:

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

不清楚吗? 幸运的是,Perl有一个->的操作员,允许你在不使用<代码>${><>> syntax的情况下参照阵列。 更便于阅读:

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

你们在现代的经历中看到了这种命运。 Perl方案。 这就是我如何谈论我的阵列。 事实上,Perl甚至允许你在各种阵列中完全清除阵列之间的arrow操作员。 你们可以这样说:

my $element = $file_array[2][3];
问题回答

为了学习,你必须这样做。 为此,你必须偶尔尝试,否则,在鱼类市场关闭时,你就可以吃饭。

《经济、社会、文化权利国际公约》涉及的是这些问题,而不是提出问题的人,因此,这里是一个答案,没有任何解释鼓励你研究正在发生的情况。

#!/usr/bin/env perl

use warnings; use strict;

my @data;

while (<DATA>) {
    next unless /S/;
    push @data, [ split ];
}


__DATA__
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

Hints :

while(<FH>)
{
}#iterate line by line through the file pointed by FH

@result = split(m/s+/, $subject); #store each column to array

因此,我只想一提几个阵列,你就是这样做的。

剩下的我留给你。





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签