关闭

迭代哈希

550人阅读 评论(0) 收藏 举报

迭代哈希怎么玩啊。。。数组作为哈希表A的值,哈希表A作为另一哈希表B的值
终于搞定了。。。得记录下 

#!/usr/bin/perl -w


sub gettime
{
 my $line = $_[0];
 my @temptime =split(/timestamp/,$_);
 $temptime[1] =~s/\>//;
 $temptime[1] =~s/\<//;
 $temptime[1] =~s/GMT\///;
 my @temptime2 = split(/\s/,$temptime[1]);
 my @gettime =split(/\:/,$temptime2[1]); 
 my $second = pop(@gettime);
 my @gettime2 = split(/\./,$second);
 push(@gettime,$gettime2[0]); 
 
 return @gettime;
}
sub getid
{
 my $line = $_[0];
 my @temp = split(/submit_id>/,$_);
 my @temp2 =split(/submit/,$temp[1]);
 $temp2[0]=~s/\///;
 $temp2[0]=~s/\<//;
 return $temp2[0];
}
sub main
{
 our @submittime=();
 our @pricetime=() ;
 our $averagetime = 0;
 our $alltime =0;
 our @id =();
 our @priceid =();
 @id = keys our%parsehash;
 our %onehash = values(%parsehash);
 %onehash = ("submittime"=>\@submit,
    "pricetime"=>\@pricetime);
 
 
 our $inputfile = $ARGV[0];
 open(INF,"$inputfile")or die ("can not open $inputfile!\n");
 while(<INF>)
 {
  if(/<submit taker/)
  {
   my @submittime = gettime($_);
   my $tempid = getid($_);
   push(@id,$tempid);
   print "submitid-$tempid $submittime[0]:$submittime[1]:$submittime[2]\n";
   
   $parsehash{$tempid}{"submittime"}=\@submittime;
   $tempid =(); 
   next;
  } 
  elsif(/<price>/)
  {
   my @pricetime = gettime($_);
   my $pid = getid($_);
   print "price---$pid $pricetime[0]:$pricetime[1]:$pricetime[2]\n";
   
   print "111=====we are looking for $pid in hash\n";
   
   if (exists $parsehash{$pid})
   {
    if(exists $parsehash{$pid}{"pricetime"}){print "priceid $pid Exist!!!!!!\n";}
    else
    {
     $parsehash{$pid}{"pricetime"}=\@pricetime;
     print "priceid $pid find match!!!!!!\n";system("pause");
    }
   }
   print "222=====we are looking for $pid in price \n"; 
   my $get = grep(/^$pid$/,@priceid);
   if(!$get)
   {
    push(@priceid,$pid);
    print "response id :$pid ^^^^^^\n";system("pause");
   }
   
   next;
  } 
 }
 close(INF); 
 
 
 my $onetime=0;
 my $number =0;
 foreach my $i(@priceid)
 {
  print "AAAAA $i\n";system ("pause");
  my @a =values ($parsehash{$i}{"submittime"});
  my @b =values ($parsehash{$i}{"pricetime"});
  $onetime =($b[0]-$a[0])*3600+($b[1]-$a[1])*60+($b[2]-$a[2]);
  foreach my$x (@a){print "$x ";}
  print "\n";
  foreach my$y (@b){print "$y ";}
  print "\n";
  
  print "the deal id=$i,time between request and response is $onetime\n";
  system ("pause");
  $alltime +=$onetime;
  $number++;
 }
 $averagetime = $alltime/$number;
 print "averagetime:$averagetime\n";
}
main();

 

向一个函数传递多个参数 我还没搞定。。。。待续。。。。。 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:63884次
    • 积分:951
    • 等级:
    • 排名:千里之外
    • 原创:22篇
    • 转载:17篇
    • 译文:0篇
    • 评论:12条
    文章分类
    最新评论