是否可以将数组元素放入perl中哈希?

时间:2016-05-28 14:55:39

标签: arrays perl hash

文件内容示例:

>random sequence 1 consisting of 500 residues.
VILVWRISEMNPTHEIYPEVSYEDRQPFRCFDEGINMQMGQKSCRNCLIFTRNAFAYGIV
HFLEWGILLTHIIHCCHQIQGGCDCTRHPVRFYPQHRNDDVDKPCQTKSPMQVRYGDDSD;

>random sequence 2 consisting of 500 residues.
KAAATKKPWADTIPYLLCTFMQTSGLEWLHTDYNNFSSVVCVRYFEQFWVQCQDHVFVKN
KNWHQVLWEEYAVIDSMNFAWPPLYQSVSSNLDSTERMMWWWVYYQFEDNIQIRMEWCNI
YSGFLSREKLELTHNKCEVCVDKFVRLVFKQTKWVRTMNNRRRVRFRGIYQQTAIQEYHV
HQKIIRYPCHVMQFHDPSAPCDMTRQGKRMNFCFIIFLYTLYEVKYWMHFLTYLNCLEHR;

>random sequence 3 consisting of 500 residues.
AYCSCWRIHNVVFQKDVVLGYWGHCWMSWGSMNQPFHRQPYNKYFCMAPDWCNIGTYAWK

我需要一个算法来构建哈希$hash{$key} = $value;,其中以>开头的行是值,后面的行是键。

我尝试过:

open (DATA, "seq-at.txt") or die "blabla";
@data = <DATA>;
%result = ();
$k = 0;
$i = 0;

while($k != @data) {
    $info = @data[$k]; #istrina pirma elementa
    if(@data[$i] !=~ ">") {
        $key .= @data[$i]; $i++;
    } else {
        $k = $i;
    }
    $result{$key} = $value;
}

但它没有用。

2 个答案:

答案 0 :(得分:1)

您不必先前使用数组,您可以直接构建哈希:

"PeerRole": {
    "Type": "AWS::IAM::Role",
    "Properties": {
        "AssumeRolePolicyDocument": {
            "Statement": [ {
                "Effect": "Allow",
                "Principal": { "Service": [ "ec2.amazonaws.com" ] },
                "Action": [ "sts:AssumeRole" ]
            } ]
        },
        "Path": "/",
        "Policies": [ {
            "PolicyName" : "PeerPrivs",
            "PolicyDocument" : {
                "Statement" : [{
                    "Effect" : "Allow",
                    "Action": [ "cloudwatch:PutMetricData", "cloudwatch:PutMetricDataBatch" ],
                    "Resource" : "*"
                },{
                    "Effect" : "Allow",
                    "Action" : [ "dynamodb:GetItem", "dynamodb:BatchGetItem", "dynamodb:Scan", "dynamodb:Query" ],
                    "Resource" : { "Fn::Join" : [ "", [ "arn:aws:dynamodb:*:", { "Ref" : "AWS::AccountId" }, ":table/", { "Fn::FindInMap" : [ "stage", { "Ref" : "Stage" }, "ddbTable" ] } ] ]}
                }]
            }
        }]
    }
}

    "PeerInstanceProfile": {
        "Type": "AWS::IAM::InstanceProfile",
        "Properties": {
            "Path": "/",
            "Roles": [{ "Ref": "PeerRole" }]
        }
    },

答案 1 :(得分:0)

目前还不清楚你想要什么,数组似乎是随机序列号之后的行...如果文件test.txt的最强者是:

Line 1:">"random sequence 1 consisting of 500 residues.
Line 2:VILVWRISEMNPTHEIYPEVSYEDRQPFRCFDEGINMQMGQKSCRNCLIFTRNAFAYGIV
Line 3:HFLEWGILLTHIIHCCHQIQGGCDCTRHPVRFYPQHRNDDVDKPCQTKSPMQVRYGDDSD;

您可以尝试以下方式:

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my $contentFile = $ARGV[0];
my %testHash = ();
my $currentKey = "";

open(my $contentFH,"<",$contentFile);
while(my $contentLine = <$contentFH>){
    chomp($contentLine);
    next if($contentLine eq ''); # Empty lines.
    if($contentLine =~ /^"\>"(.*)/){
            $currentKey= $1;
    }else{
            push(@{$testHash{$currentKey}},$contentLine);
    }
}
print Dumper(\%testHash);

结果是这样的结构:

seb@amon:[~]$ perl test.pl test.txt
$VAR1 = {
          'random sequence 3 consisting of 500 residues.' => [
                                                               'AYCSCWRIHNVVFQKDVVLGYWGHCWMSWGSMNQPFHRQPYNKYFCMAPDWCNIGTYAWK'
                                                             ],
          'random sequence 1 consisting of 500 residues.' => [
                                                               'VILVWRISEMNPTHEIYPEVSYEDRQPFRCFDEGINMQMGQKSCRNCLIFTRNAFAYGIV',
                                                               'HFLEWGILLTHIIHCCHQIQGGCDCTRHPVRFYPQHRNDDVDKPCQTKSPMQVRYGDDSD;'
                                                             ],
          'random sequence 2 consisting of 500 residues.' => [
                                                               'KAAATKKPWADTIPYLLCTFMQTSGLEWLHTDYNNFSSVVCVRYFEQFWVQCQDHVFVKN',
                                                               'KNWHQVLWEEYAVIDSMNFAWPPLYQSVSSNLDSTERMMWWWVYYQFEDNIQIRMEWCNI',
                                                               'YSGFLSREKLELTHNKCEVCVDKFVRLVFKQTKWVRTMNNRRRVRFRGIYQQTAIQEYHV',
                                                               'HQKIIRYPCHVMQFHDPSAPCDMTRQGKRMNFCFIIFLYTLYEVKYWMHFLTYLNCLEHR;'
                                                             ]
        };

你基本上会使用每个哈希&#34;值&#34;作为一个数组结构,@{ $变量}具有魔力。