简体   繁体   English

Perl:增加2D数组单元格?

[英]Perl: increment 2d array cell?

I have a set of numerical data for which is important to me to know what pairs of numbers occurred together, and how many times. 我有一组数字数据,对于我来说,了解哪些数字对一起出现以及多少次对我很重要。 Each set of data contain 7 numbers betwen 1 and 20. There are several hundred sets of data. 每组数据在1和20之间包含7个数字。有几百个数据集。

Essentially, by parsing each set of my data, I want to create a 20 x 20 array that I can use to keep a count of when pairs of numbers occurred together. 本质上,通过解析我的每组数据,我想创建一个20 x 20的数组,该数组可用于计数何时出现数字对。

I have done a lot of searching, but maybe I've used the wrong key words. 我做了很多搜索,但是也许我使用了错误的关键词。 I've seen loads of examples how to create a "2D array" - I know perl doesn't actually do that, and that it's really an array of references - and to print the values contained therein, but nothing really on how to work with one particular cell by number and alter it. 我已经看到了很多有关如何创建“ 2D数组”的示例-我知道perl实际上并没有做到这一点,并且它实际上是一个引用数组-并打印其中包含的值,但实际上却没有任何意义用一个特定的单元格按数字进行更改。

Below is my conceptual code. 下面是我的概念代码。 The commented lines don't work, but illustrate what I want to achieve. 带注释的行不起作用,但是说明了我想要实现的目标。 I'm reasonably new to coding perl, and this just seems to advanced for me to understand the examples I've seen and translate it into something I can actually use. 我对Perl编码是相当陌生的,这对我理解我已经看到的示例并将其转换为我可以实际使用的东西来说似乎是一种高级。

my @datapairs;
while (<DATAFILE>)
{
  chomp;
  my @data = split(",",$_);
  for ($prcount=0; $prcount <=5; $prcount++) 
  {
    for ($othcount=($prcount+1); $othcount<=6; $othcount++)
    {
        @data[$prcount]=@data[$prcount]+1;
        @data[$othcount]=@data[$othcount]+1;
        @data[$prcount]=@data[$prcount]-1;
        @data[$othcount]=@data[$othcount]-1;
        print @data[$prcount]." ".@data[$othcount]."; ";
        #@datapairs[@data[$prcount]][@data[$othcount]]++;
        #@datapairs[@data[$othcount]][@data[$prcount]]++;
    }
  }
}

Any input or suggestions would be much appreciated. 任何意见或建议将不胜感激。

To access a "cell" in a "2-d array" in Perl (as you alredy figured out, it's an array of arrayrefs), is simple: 要在Perl中访问“ 2-d数组”中的“单元”(您已经知道,它是arrayrefs的数组),很简单:

my @datapairs;
# Add 1 for a pair with indexes $i and $j 
$datapairs[$i]->[$j]++;
print that value
print "$datapairs[$i]->[$j]\n";

It's not clear what you mean by "occur together" - if you mean "in the same length-7 array", it's easy: 不清楚“一起出现”是什么意思-如果您的意思是“在相同长度7数组中”,这很容易:

my @datapairs;
while (<DATAFILE>) {
    chomp;
    my @data = split(",", $_);
    for (my $prcount = 0; $prcount <= 5; $prcount++) {
        for (my $othcount = $prcount + 1; $othcount <=6 ; $othcount++) {
            $datapairs[ $data[$prcount] ]->[ $data[$othcount] ]++;
        }
    }
}
# Print
for (my $i = 0; $i < 20; $i++) {
    for (my $j = 0; $j < 20; $j++) {
        print "$datapairs[$i]->[$j], ";
    }
    print "\n";
}

As a side note, personally, just for stylistic reasons, I strongly prefer to reference EVERYTHING, eg use arrayref of arrayrefs instead of array of arrays. 作为附带说明,就个人而言,仅出于风格原因,我强烈希望引用所有内容,例如,使用arrayrefs的arrayref而不是array数组。 Eg 例如

my $datapairs;
# Add 1 for a pair with indexes $i and $j 
$datapairs->[$i]->[$j]++;
print that value
print "$datapairs->[$i]->[$j]\n";

The second (and third...) arrow dereference operator is optional in Perl but I personally find it significantly more readable to enforce its usage - it spaces out the index expressions. 在Perl中,第二个(和第三个...)箭头取消引用运算符是可选的,但我个人认为强制执行它的可读性要高得多-它隔开了索引表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM