2016-12-26 62 views
-2
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); 

    } 
+5

你应该[编辑]你的问题到a)修复代码格式和b)添加一个实际的问题 - 这应该做什么?你会收到错误消息吗?发生什么事而不是你认为应该发生的事情? –

+3

这个问题似乎是你不知道Perl([perlsyn](http://perldoc.perl.org/perlsyn.html))。这是一个准确的评估? –

+0

我想你想让'userhash'成为一个哈希数组? –

回答

3

有没有在你的问题,给你最好的答案可能足够的信息(即,该数据结构可能仍然是次优的),但我至少可以告诉你一个比使用20出头的平行阵列更好的办法:

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