English 中文(简体)
我看不出一个阵列是正确的。
原标题:I can t seem to dereference an array correctly

我并非非常善于Perl,但我需要能够在多维阵列上某种。 我在玩弄一些试验守则,试图更好地掌握这一概念,我认为我已经结束,但我无法找到神奇的结合。

我似乎无法做的是参考我的阵列并正确印制。 我似乎可以简单地谈谈我需要了解的世界上的一切情况,但所提到的各阵列的价值除外。

I m getting my data from a tab-delimited flat file, so in my sample code, I m mimicking that by creating multiple arrays via splits and then pushing them in to a single array. In practice, I ll be looping through the file, splitting on the tabs and pushing them in to the array as I go.

如果有了更好的途径,我就一耳不闻。 定额档案中的每一条线都是单一记录。 我首先需要获得最老的记录的日期,然后是第二类,按数量分类。 我在网上研究了几个例子,但没有发现任何似乎与我需要收集的数据合作的东西。

my @s1 = split(/:/,  X:Y:Z );
my @s2 = split(/:/,  A:B:C );
my @s3 = split(/:/,  Q:L:P:0 );
my @s4 = split(/:/,  U:E:G );

my @array = ();
push(@array, @s1);
push(@array, @s2);
push(@array, @s3);
push(@array, @s4);

print "@array
";

my @sorted = sort { $a->[0] cmp $b->[0] } @array;

print "
";
foreach $thingy (@sorted)
{
    print @thingy . "
"; #result: number 0
    print $thingy . "
"; #result: reference
    #print ${$thingy} . "
"; #result:  Not a scalar reference  error
    print ${@thingy} . "
"; #result: file name (???)
    print @{$thingy} . "
"; #result: length of the array referenced
}
最佳回答

首先,你应始终将<条码>严格使用;置于你的方案之首。 这将尽早发现许多错误。

页: 1 选择正确参照<代码>thingy。 但是,由于您在<代码>左侧上贴上了<代码>@{$thingy}(>(string concatenation)的操作员,该阵列在星体内,阵列对其大小进行了评估。 仅说:

print "@{$thingy}
";

查阅<代码>@$thingy的内容 按空间或一般情况分列的离职情况

print join( | , @{$thingy}), "
";

如果你想要使用另一个分离器,就像纵向交易。 你也可以说

print @{$thingy}, "
";

a) 完全没有分离器的部件。

问题回答

@thingy为未申报和未界定(没有必要)。

使用两条nes道

  1. iterate over your array iii array-references
  2. and in each loop then iterate over the items in that referenced array.

foreach my $array_ref (@sorted)
{
    foreach my $item (@{$array_ref}) {
        print $item, ",";
    }
    print "
";
}

<代码>@{$array_ref}将参考你的阵列。 然后,它像一个阵列一样使用。

添加:

页: 1

my @s1 = split(/:/,  X:Y:Z );
my @s2 = split(/:/,  A:B:C );
my @s3 = split(/:/,  Q:L:P:0 );
my @s4 = split(/:/,  U:E:G );

my @array = ();
push(@array, @s1);
push(@array, @s2);
push(@array, @s3);
push(@array, @s4);

iii

my @array = ();
push(@array, map { [split(/:/, $_)] } qw(X:Y:Z A:B:C Q:L:P:0 U:E:G));

如果分类需要两个标准(第一个指数和第二个指数的二级标准),可以这样写:

my @sorted = sort { $a->[0] cmp $b->[0] 
                             ||
                    $a->[1] cmp $b->[1] 
                  } @array;

您需要做的第一项工作是,在你的文字中添加:

use strict;
use warnings;

然后,你将发出警告:

Global symbol "@thingy" requires explicit package name

哪一种意思是:@thingy。 <代码>$thingy和@thingy作为单独变量计算。

创建你阵列的另一个办法是使用匿名阵列,例如:

push @array, [ split(/:/,  X:Y:Z ) ];
push @array, [ split(/:/,  A:B:C ) ];
...

Then you won t have to create throwaway variables. Or with a file like the one you describe ( is tab):

while (<>) {
    push @array, [ split /	/, $_ ];
}

a. 从 per子中分几栏:

my @a = ([1,2], [3,4]);
my @b = sort {

    $a->[0] <=> $b->[0] || # the result is -1,0,1 ...
    $a->[1] <=> $b->[1]    # so [1] when [0] is same

} @a;

http://www.perlmonks.org/index.pl?node_id=674374

当然,这在你的领域具有数值。 否则使用<代码>cmp。

印刷:

for my $ref (@array) {
    my $i = 0;
    for my $value (@$ref) {
        print $value; 
        print "," if ($i++ < $#$ref); # comma delimited
    }
    print "
"; # end of record
}




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

热门标签