从Perl中的矩阵中获取对

时间:2015-11-09 13:35:42

标签: perl matrix hash

我有几个文件,下面的三角矩阵有不同的尺寸,我试图获得每个值的行/列对。

File:

2  0.0158           
3  0.0079 0.0079           
4  0.0000 0.0158 0.0079           
5  0.0158 0.0317 0.0238 0.0158           
6  0.0079 0.0238 0.0158 0.0079 0.0238           
7  0.0079 0.0079 0.0158 0.0079 0.0238 0.0158           

Expected output:

2  1  0.0158           
3  1  0.0079
3  2  0.0079           
4  1  0.0000
4  2  0.0158
4  3  0.0079           
5  1  0.0158
5  2  0.0317
5  3  0.0238
5  4  0.0158           
6  1  0.0079
6  2  0.0238
6  3  0.0158
6  4  0.0079
6  5  0.0238           
7  1  0.0079
7  2  0.0079
7  3  0.0158
7  4  0.0079
7  5  0.0238
7  6  0.0158

这是我尝试过的,但我无法获得正确的输出。 非常感谢任何帮助。

    use strict;
    use warnings;

    open (my $IN , '<', $ARGV[0]) or die "$!";

    # declare variables

while (my $line = <$IN>) {
    foreach ($line) {
    push @file, $line;                                                                                                                                                                                      
                }
        }
    $line = '';

    foreach $line (@file) {
             if ($line =~ /(\d{1,2})/)   {
                     $index = $1; 
                     @values = split( /\s{2,2}/, $line );
                     foreach $value (@values) {
                     if ($value =~ /\d\.\d+/)   {
                     @sub_values = split( /\s/, $value );
                             foreach $sub_value (@sub_values) {
                             push @{$Matrix{$index}}, $sub_value;   
                             }
                         }
                     }               
                 }   
             }

    foreach $group (keys %Matrix) {          
                    @sequence = (1..$group-1);
                            foreach $seq (@sequence) {
                                    foreach (@{$Matrix{$group}}) {
                                    push @{$Extra{$group}{$seq}}, $_;                       
                                    }
                                }
                            }    

    foreach $group1 (keys %Extra) {
            foreach $seq1 (keys %{$Extra{$group1}}) {
                    foreach $innermost (@{$Extra{$group1}{$seq1}}) {
                    if ($innermost =~ /\d\.\d+/)   {
                    print $group1,"\t",$seq1,"\t",$innermost,"\n";
                                            }
                                        }
                                    }
                                }

1 个答案:

答案 0 :(得分:1)

根据输出猜测你的算法 - 这似乎可以解决问题。我不确定我是否遗漏了某些东西,或者你是否在房子周围走了一段路。您似乎并不需要复杂的数据结构。

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

#magic filehandle - reads piped STDIN or files on command line. 
while (<>) {
    #split current line on whitespace
    my ( $key, @fields ) = split;
    my $count = 1;
    #iterate each field on the current line, printing a key and a count. 
    foreach my $field (@fields) {
        print join( " ", $key, $count++, $field ), "\n";
    }
}