2016-12-26 6 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と読むのは貴重な時間です。

関連する問題