我需要处理数组的哈希。这仅适用于1次迭代。我如何才能进行N次迭代?

时间:2016-12-26 21:28:51

标签: perl

use Data::Dumper;
print "enter number of orders";
$count=<STDIN>;
//declaring arrays
my @arr;
my @order;
my @protocol;
my @message_length;
my @logon;
my @value_send;
my @value_receive;
my @seq_number;
my @time;
my @retransmission;
my @account;
my @destination;
my @qty_order;
my @ticker;
my @instruction;
my @order;
my @locate;
my @duration;
my @market;
my @id;
my @timeexecution;
my @userid;
my @checksum;

for ($i=1; $i<=$count; $i++) {

// taking input from user

    print "enter the value of tag8";
    $protocol[$i]=<STDIN>;
    chomp($protocol[$i]);
    print "enter the value of tag9";
    $message_length[$i]=<STDIN>;
    chomp($message_length[$i]);
    print "enter the value of tag35";
    $logon[$i]=<STDIN>;
    chomp($logon[$i]);
    print "enter the value of tag49";
    $value_send[$i]=<STDIN>;
    chomp($value_send[$i]);
    print "enter the value of tag56";
    $value_recieve[$i]=<STDIN>;
    chomp($value_recieve[$i]);
    print "enter the value of tag34";
    $seq_number[$i]=<STDIN>;
    chomp($seq_number[$i]);
    print "enter the value of tag52";
    $time[$i]=<STDIN>;
    chomp($time[$i]);
    print "enter the value of tag43";
    $retransmission[$i]=<STDIN>;
    chomp($retransmission[$i]);
    print "enter the value of tag1";
    $account[$i]=<STDIN>;
    chomp($account[$i]);
    print "enter the value of tag100";
    $destination[$i]=<STDIN>;
    chomp($destination[$i]);
    print "enter the value of tag38";
    $qty_order[$i]=<STDIN>;
    chomp($qty_order[$i]);
    print "enter the value of tag55";
    $ticker[$i]=<STDIN>;
    chomp($ticker[$i]);
    print "enter the value of tag21";
    $instruction[$i]=<STDIN>;
    chomp($instruction[$i]);
    print "enter the value of tag54";
    $order[$i]=<STDIN>;
    chomp($order[$i]);
    print "enter the value of tag114";
    $locate[$i]=<STDIN>;
    chomp($locate[$i]);
    print "enter the value of tag59";
    $duration[$i]=<STDIN>;
    chomp($duration[$i]);
    print "enter the value of tag40";
    $market[$i]=<STDIN>;
    chomp($market[$i]);
    print "enter the value of tag11";
    $id[$i]=<STDIN>;
    chomp($id[$i]);
    print "enter the value of tag60";
    $timeexecution[$i]=<STDIN>;
    chomp($timeexecution[$i]);
    print "enter the value of tag553";
    $userid[$i]=<STDIN>;
    chomp($userid[$i]);
    print "enter the value of tag10";
    $checksum[$i]=<STDIN>;
    chomp($checksum[$i]);

    // I need to make hash dynamically for more than 1 iterations 

    my %userhash1 = (
    "8" => $protocol[$i],
    "9" => $message_length[$i],
    "35" => $logon[$i],
    "49" => $value_send[$i],
    "56" => $value_recieve[$i],
    "34" => $seq_number[$i],
    "52" => $time[$i],
    "43" => $retransmission[$i],
    "1" => $account[$i],
    "100" => $destination[$i],
    "38" => $qty_order[$i],
    "55" => $ticker[$i],
    "21" => $instruction[$i],
    "54" => $order[$i],
    "114" => $locate[$i],
    "59" => $duration[$i],
    "40" => $market[$i],
    "11" => $id[$i],
    "60" => $timeexecution[$i],
    "553" => $userid[$i],
    "10" => $checksum[$i],

       );
    //using hash of hash
    $userhash2{"KEY"}={%userhash1};

    // printing the hash  
    print Dumper(\%userhash2);

    }

1 个答案:

答案 0 :(得分:3)

你的问题中没有足够的信息可以为你提供最好的答案(也就是说,这个数据结构可能仍然不是最理想的),但我至少可以向你展示比使用20-something并行数组更好的方法:

use strict;
use warnings;

use Data::Dumper;

print "enter number of orders: ";
my $count = <STDIN>;
chomp($count);
die unless $count =~ /^\d+$/ && $count > 0;

my @data;

while ($count) {
    my %record;

    for my $tag (qw(1 8 9 10 11 21 34 35 38 40 43 49 52 54 55 56 59 60 100 114 553)) {
        print "enter the value of tag$tag: ";
        $record{$tag} = <STDIN>;
    }

    chomp(%record);
    push(@data, \%record);
    $count--;
}

print Dumper(\@data);

这是一个简单的array of hashes。我不会从头到尾解释整个事情,但你可以点击链接了解更多信息。阅读perlreftut也是值得的。