English 中文(简体)
我怎么能打印一个双维阵列?
原标题:How can I print a Perl two-dimensional array?
  • 时间:2010-06-10 21:36:44
  •  标签:
  • perl
  • arrays

我正试图写一个简单的“Perl”文字,读到*.csv,把“Esv”文件放在两维阵列中,在阵列中打印一个物品,并打印阵列中的一行。

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

open(CSV, $ARGV[0]) || die("Cannot open the $ARGV[0] file: $!");
my @row;
my @table;

while(<CSV>) {
    @row = split(/s*,s*/, $_);
    push(@table, @row);
}
close CSV || die $!;

foreach my $element ( @{ $table[0] } ) {
    print $element, "
";
}

print "$table[0][1]
";

在我管理这一文字时,我收到以下错误和没有印本:

Can t use string ("1”) as an ARRAY ref while “strict refs” in use at ./scripts. 插线16。

我已经研究了其他一些论坛,仍然无法确定如何解决这一问题。 如何确定这一点?

最佳回答

你们是创造两维阵的灯塔(AoA或Perl-parlance的Arrays)。 本条:

push(@table, @row);

页: 1 你们需要转而提及,并每次通过假装创造新的变数,以便你不再重复提及:

my @table;
while(<CSV>) {
    my @row = split(/s*,s*/, $_);
    push(@table, @row);
}

虽然使用<代码>split对CSV的三维档案来说是oka的,但对于任何其他东西来说,这种说法是有害的。 http://search.cpan.org/perldoc? 文本:CSV_XS” rel=“noreferer” 案文:CSV_XS改为:

use strict;
use warnings;
use Text::CSV_XS;

my $csv  = Text::CSV_XS->new() or die "Can t create CSV parser.
";
my $file = shift @ARGV         or die "No input file.
";
open my $fh,  < , $file        or die "Can t read file  $file  [$!]
";

my @table;
while (my $row = $csv->getline($fh)) {
    push @table, $row;
}
close $fh;

foreach my $row (@table) {
    foreach my $element (@$row) {
        print $element, "
";
    }
}

print $table[0][1], "
";
问题回答
my @arr = ( [a, b, c],
            [d, e, f],
            [g, h, i],
          );

for my $row (@arr) {
    print join(",", @{$row}), "
";
}

印刷

a,b,c
d,e,f
g,h,i

如果你提出清单论点,你会以 wise智的方式把第一个名单附在其余名单之后。 阅读。 因此,请你打电话push(@table,@row);正在制作一个更长的<代码>@table清单,而不是两个层面阵列。

您收到了若干职位,这些职位把名单的参考资料推到@row,作为@row,将编制一个行文清单,实际上,该行将制作。 我倾向于这样做。 当然,与Perl一样,这样做总是有另一种方式。

顺便提一下,你还可以把匿名阵列引入名单的缩略部分,以编制多层面清单。 了解Perl提及情况的最重要方面是:1)它们是小数,2)它们可以提及Perl——代码、阵列、洗衣——中的任何内容。 Spend some time with the Perl Ref Tutorial , 这将变得更加明确。 根据您的代码,仅添加[ ],围绕您希望成为名单上第2个方面的内容,即push(table,@row);应为push(table, [@row]);。 在同一意义上,你将<代码>[ ]围绕您的分部分排列,使之成为<编码>push(@table,[分成/s*,s*/,_美元]);。 这将同时进行分裂,并由此形成一个匿名阵列。

如何制作和查阅多方面名单,在Tom Christensen s perllol tutorial也得到了很好的处理。 本文直接阐述了你在法典中解决具体问题的办法。

改写《你法典》与《手法》中的手法,成为:

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

my (@row, @table, $n, $rowref);

while(<DATA>) {
        chomp;
        # regex to separate CSV (use of a cpan module for CSV STRONGLY advised...
        @row = /(?:^|,)("(?:[^"]+|"")*"|[^,]*)/g;
        for (@row) {
            if (s/^"//) { s/"$//; s/""/"/g; }
        }
        push(@table, [ @row ]); #Note the [ ] around the list
}

# Now the table is created, print it:
my $rowcnt=0;
foreach $rowref (@table) {
    print "row $rowcnt:
";
    $rowcnt++;
    print "  [ @$rowref ], 
";
}   

# You can access the table in the classic [i][j] form:
for my $i ( 0 .. $#table ) {
    $rowref = $table[$i];
    $n = @$rowref - 1;
    for my $j ( 0 .. $n ) {
        print "element $i, $j of table is $table[$i][$j]
";
    }
}

# You can format it:
for my $i ( 0 .. $#table ) {
    print "$table[$i][0] $table[$i][1]
";
    print "$table[$i][2]
";
    print "$table[$i][3], $table[$i][4] $table[$i][5]

";
}


__DATA__
Mac,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
,Blankman,,SomeTown, SD, 00298
"Joan ""Joan, the bone""",Jett,"9th, at Terrace plc",Desert City,CO,00123

你们需要两个变化:

  1. use a local variable for row
  2. use references for the array you put into @table

因此,您的方案应当研究:

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

open(CSV, $ARGV[0]) || die("Cannot open the $ARGV[0] file: $!");
my @table;

while(<CSV>) {
    my @row = split(/s*,s*/, $_);
    push(@table, @row);
}
close CSV || die $!;

foreach my $element ( @{ $table[0] } ) {
    print $element, "
";
}

print "$table[0][1]
";

或许这是你实际想要的:

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

open(CSV, $ARGV[0]) || die("Cannot open the $ARGV[0] file: $!");
my @table;

while(<CSV>) {
        my @row = split(/s*,s*/, $_);
        push(@table, @row);
}
close CSV || die $!;

foreach my $element ( @{ $table[0] } ) {
    print $element, "
";
}

print "$table[0][1]
";

变化

#push(@table, @row);
push(@table, @row);  #push a reference to the array into each cell in @table.

然后,它用ok印。





相关问题
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 ...

热门标签