求助:hash数组的输出问题?

求助:hash数组的输出问题?

求助:hash数组的输出问题?
my %hash = (
      '10' => 'one',
      '2A' => 'two',
      '30' => 'three',
      );
(@key,@value) = each %hash;

@str=(" ","a a","FIS 0x10 s","FIS 0x2A=2","cc");
for($i=0;$i<@str;$i++)
  {if($str[$i]=~/^\s+/)
    { print "unuse"."\n"; }
  if($str[$i]=~/(FIS.*0x)((\d|\w)(\d|\w))/)
    { print $2."\n";
     if ($2=~/ foreach $key (keys %hash)/)
       { print $value (values %hash);}
    }
  else {$str[$i]="$str[$i] \n"; }
# print $str[$i]."\n";
 }
想要将文本内容(数组)匹配hash里面的key 10&2A来得到one&two,怎样可以实现?

现在可以输出:
unuse
10
2A
想要输出如下内容,该怎样实现呢?
one
two
each 只能返回 --[quote.
each 只能返回

($key, $value) = each %hash


想要将文本内容(数组)匹配hash里面的key 10&2A来得到one&two,怎样可以实现?


for my $key (keys %hash) {
  for my $str (@str) {
    if ($str =~ /$key/) {
      print $hash{$key};
    }
  }
}
帮我好看下程序好吗?――两个hash数组的调用问题
my %hash = (
      '10' =>['two',0,2],
      '20' =>['one',1,3],
      '30' =>['three',4,5],
      );
my %ext = (
      '1A' => ['four',1],
      '2A' => ['fine',2],
      '30' => ['six',3],
      );
 my $key; my $value; my $key1; my $key1;
($key, $value) = each %hash;
  ($key1, $value1) = each %ext;
@str=(" ","a a","FIS 0x10 s","FIS 0x2A=2","cc");

for $str (@str) {
  if($str=~/(FIS.*0x)((\d|\w)(\d|\w))/)
     { print $2."\n";
        for $key (keys %hash)
         { #print $2."\n";
          if ($2 =~ /$key/)
            { print $key; print $hash{$key}[0]."\n"; }
          else { last ;}
        }
        for $key1 (keys %ext) {
          if ($2 =~ /$key1/)
         { print $key1; print $ext{$key1}[0]."\n"; }
          else {print"error";}
        }
  }
}

结果不对,所得正确结果应是:
10 two
2A fine
问题已解决。thanks Longbow!
问题已解决。thanks Longbow!
不很明白,能否说一下你些.
不很明白,能否说一下你些这个脚本的用途?

1、务必使用 use strict; 和 use warnings; 对于避免脚本中的输入错误很有效;
2、为什么要在 hash 里定义对匿名数组的引用?
调试成功的程序。
这是我调好的程序,结果也是对的,但是加上use strict结果就不对了,都是Others。用得不好,还待研究研究。
%cmd=(


      '00' => ['NOP',0,0],                              # ATA-7 Checked
      '03' => ['CFA REQUEST EXTENDED ERROR',0,0],     # ATA-7 Checked
      '08' => ['DEVICE RESET',0,0],                    # ATA-7 Checked

      '10' => ['RECALIBRATE',0,0],
      '11' => ['RECALIBRATE',0,0],
      '12' => ['RECALIBRATE',0,0],
      '13' => ['RECALIBRATE',0,0],
      '14' => ['RECALIBRATE',0,0],
      '15' => ['RECALIBRATE',0,0],
      '16' => ['RECALIBRATE',0,0],
      '17' => ['RECALIBRATE',0,0],
      '18' => ['RECALIBRATE',0,0],
      '19' => ['RECALIBRATE',0,0],
      '1A' => ['RECALIBRATE',0,0],
      '1B' => ['RECALIBRATE',0,0],
      '1C' => ['RECALIBRATE',0,0],
      '1D' => ['RECALIBRATE',0,0],
      '1E' => ['RECALIBRATE',0,0],
      '1F' => ['RECALIBRATE',0,0],


      '20' => ['READ SECTORS',2,0],
      '21' => ['READ SECTORS (NO RETRY)',2,0],
      '22' => ['READ LONG',2,0],
      '23' => ['READ LONG (NO RETRY)',2,0],
      '24' => ['READ SECTORS EXT',2,0],
      '25' => ['READ DMA EXT',2,0],
      '26' => ['READ DMA QUEUED EXT',5,0],
      '27' => ['READ NATIVE MAX ADDRESS EXT',0,0],
      '29' => ['READ MULTIPLE EXT',2,0],
      '2A' => ['READ STREAM DMA',2,0],
      '2B' => ['READ STREAM',2,0],
      '2F' => ['READ LOG EXT',0,0],

      '30' => ['WRITE SECTORS',3,0],
      '31' => ['WRITE SECTORS (NO RETRY)',3,0],
      '32' => ['WRITE LONG',3,0],
      '33' => ['WRITE LONG (NO RETRY)',3,0],
      '34' => ['WRITE SECTORS EXT',3,0],
      '35' => ['WRITE DMA EXT',3,0],
      '36' => ['WRITE DMA QUEUED EXT',6,0],
      '37' => ['SET NATIVE MAX ADDRESS EXT',0,0],
      '38' => ['CFA WRITE SECTORS WITHOUT ERASE',3,0],
      '39' => ['WRITE MULTIPLE EXT',3,0],
      '3A' => ['WRITE STREAM DMA',3,0],
      '3B' => ['WRITE STREAM',3,0],
      '3C' => ['WRITE VERIFY',4,0],
      '3D' => ['WRITE DMA FUA EXT',3,0],
      '3E' => ['WRITE DMA QUEUED FUA EXT',6,0],
      '3F' => ['WRITE LOG EXT',0,0],
      '50' => ['FORMAT TRACK',0,0],

      '40' => ['READ VERIFY SECTORS',4,0],                    # Scrub command ATA-7 Checked
      '41' => ['READ VERIFY SECTORS (NO RETRY)',4,0],          # Scrub command
      '42' => ['READ VERIFY SECTORS EXT',4,0],               # Scrub command ATA-7 Checked
               '45' => ['WRITE UNCORRECTABLE EXT',0,0],
               '47' => ['READ LOG DMA EXT',4,0],                         # Scrub command

      # '50' => ['FORMAT TRACK',0,0],
      '51' => ['CONFIGURE STREAM',0,0],          # ATA-7 Checked, non-data
               '57' => ['WRITE LOG DMA EXT',3,0],
               '5B' => ['TRUSTED NON-DATA',0,0],
               '5C' => ['TRUSTED RECEIVE',0,0],     # ATA 8
               '5D' => ['TRUSTED RECEIVE (DMA)',0,0],     # ATA 8
               '5E' => ['TRUSTED SEND',0,0],          # ATA 8
               '5F' => ['TRUSTED SEND (DMA)',0,0],          # ATA 8

               '60' => ['READ FPDMA QUEUED',5,0],          # ATA 8
               '61' => ['WRITE FPDMA QUEUED',6,0],          # ATA 8
               '62' => ['SATA RESERVED',0,0],               # ATA 8
               '63' => ['SATA RESERVED',0,0],               # ATA 8
               '64' => ['SATA RESERVED',0,0],               # ATA 8
               '65' => ['SATA RESERVED',0,0],               # ATA 8
               '66' => ['SATA RESERVED',0,0],               # ATA 8
               '67' => ['SATA RESERVED',0,0],               # ATA 8

      '70' => ['SEEK',1,0],                         # ATA 8
      '71' => ['SEEK',1,0],                         # ATA 8
      '72' => ['SEEK',1,0],                         # ATA 8
      '73' => ['SEEK',1,0],                         # ATA 8
      '74' => ['SEEK',1,0],                         # ATA 8
      '75' => ['SEEK',1,0],                         # ATA 8
      '76' => ['SEEK',1,0],                         # ATA 8
      '77' => ['SEEK',1,0],                         # ATA 8
      '78' => ['SEEK',1,0],                         # ATA 8
      '79' => ['SEEK',1,0],                         # ATA 8
      '7A' => ['SEEK',1,0],                         # ATA 8
      '7B' => ['SEEK',1,0],                         # ATA 8
      '7C' => ['SEEK',1,0],                         # ATA 8
      '7D' => ['SEEK',1,0],                         # ATA 8
      '7E' => ['SEEK',1,0],                         # ATA 8
      '7F' => ['SEEK',1,0],                         # ATA 8

               '80' => ['VENDOR SPECIFIC',0,0],          # ATA 8
               '81' => ['VENDOR SPECIFIC',0,0],          # ATA 8
               '82' => ['VENDOR SPECIFIC',0,0],          # ATA 8
               '83' => ['VENDOR SPECIFIC',0,0],          # ATA 8
               '84' => ['VENDOR SPECIFIC',0,0],          # ATA 8
               '85' => ['VENDOR SPECIFIC',0,0],          # ATA 8
               '86' => ['VENDOR SPECIFIC',0,0],          # ATA 8

      '87' => ['CFA TRANSLATE SECTOR',1,0],     # ATA-7 Checked

               '89' => ['VENDOR SPECIFIC',0,0],          # ATA 8
               '8A' => ['VENDOR SPECIFIC',0,0],          # ATA 8
               '8B' => ['VENDOR SPECIFIC',0,0],          # ATA 8
               '8C' => ['VENDOR SPECIFIC',0,0],          # ATA 8
               '8D' => ['VENDOR SPECIFIC',0,0],          # ATA 8
               '8E' => ['VENDOR SPECIFIC',0,0],          # ATA 8
               '8F' => ['VENDOR SPECIFIC',0,0],          # ATA 8

      '90' => ['EXECUTE DEVICE DIAGNOSTICS',0,0],     # ATA-7 Checked
      '91' => ['INITIALIZE DEVICE PARAMETERS',0,0],
      '92' => ['DOWNLOAD MIRCOCODE',0,0],          # ATA-7 Checked
               '94' => ['STANDBY IMMEDIATE',0,0],          # ATA 8
               '95' => ['IDLE IMMEDIATE',0,0],               # ATA 8
      '96' => ['STANDBY',0,0],                    # ATA 8
      '97' => ['IDLE',0,0],                         # ATA 8
      '98' => ['CHECK POWER MODE',0,0],          # ATA 8
      '99' => ['SLEEP',0,0],                         # ATA 8
               '9A' => ['VENDOR SPECIFIC',0,0],          # ATA 8

      'A0' => ['PACKET',0,0],                              # ATA-7 Checked
      'A1' => ['IDENTIFY PACKET DEVICE',0,0],          # ATA-7 Checked
               'A2' => ['SERVICE',0,0],                         # ATA-7 Checked

      'B0' => ['SMART',0,0],                              # ATA 8
               'B1' => ['CFA DEVICE CONFIGURATION',0,0],     # ATA 8
               'B6' => ['NV CACHE',0,0],                         # ATA 8
               'B7' => ['RESERVED (COMPACT FLASH)',0,0],     # ATA 8
               'B8' => ['RESERVED (COMPACT FLASH)',0,0],     # ATA 8
               'B9' => ['RESERVED (COMPACT FLASH)',0,0],     # ATA 8
               'BA' => ['RESERVED (COMPACT FLASH)',0,0],     # ATA 8
               'BB' => ['RESERVED (COMPACT FLASH)',0,0],     # ATA 8
               'BC' => ['RESERVED)',0,0],                         # ATA 8
               'BD' => ['RESERVED)',0,0],                         # ATA 8
               'BE' => ['RESERVED)',0,0],                         # ATA 8
               'BF' => ['RESERVED)',0,0],                         # ATA 8


      'C0' => ['CFA ERASE SECTORS',1,0],               # ATA-7 Checked
               'C1' => ['VENDOR SPECIFIC',0,0],               # ATA 8
               'C2' => ['VENDOR SPECIFIC',0,0],               # ATA 8
               'C3' => ['VENDOR SPECIFIC',0,0],               # ATA 8
      'C4' => ['READ MULTIPLE',2,0],                    # ATA-7 Checked
      'C5' => ['WRITE MULTIPLE',3,0],
      'C6' => ['SET MULTIPLE MODE',0,0],               # ATA-7 Checked
      'C7' => ['READ DMA QUEUED',5,0],               # ATA-7 Checked
      'C8' => ['READ DMA',2,0],                         # ATA-7 Checked
      'C9' => ['READ DMA (NO RETRY)',2,0],          # ATA 8
      'CA' => ['WRITE DMA',3,0],
      'CB' => ['WRITE DMA (NO RETRY)',3,0],
               'CC' => ['WRITE DMA QUEUED',6,0],
               'CD' => ['CFA WRITE MULTIPLE WITHOUT ERASE',3,0],     # ATA-7 Checked
               'CE' => ['WRITE MULTIPLE FUA EXT',3,0],

               'D1' => ['CHECK MEDIA CARD TYPE',0,0],          # ATA-7 Checked, non-data
               'D2' => ['RESERVED (MEDIA CARD)',0,0],          # ATA 8
               'D3' => ['RESERVED (MEDIA CARD)',0,0],          # ATA 8
               'D4' => ['RESERVED (MEDIA CARD)',0,0],          # ATA 8

               'DA' => ['GET MEDIA STATUS',0,0],               # ATA-7 Checked
               'DB' => ['ACKNOWLEDGE MEDIA CHANGE',0,0],     # ATA 8
               'DC' => ['BOOT POST BOOT',0,0],                    # ATA 8
               'DD' => ['BOOT PRE BOOT',0,0],                    # ATA 8
               'DE' => ['MEDIA LOCK',0,0],                         # ATA-7 Checked
               'DF' => ['MEDIA UNLOCK',0,0],                    # ATA-7 Checked

               'E0' => ['STANDBY IMMEDIATE',0,0],               # ATA-7 Checked
      'E1' => ['IDLE IMMEDIATE',0,0],                    # ATA-7 Checked
               'E2' => ['STANDBY',0,0],
      'E3' => ['IDLE',0,0],                              # ATA-7 Checked
      'E4' => ['READ BUFFER',0,0],                    # ATA-7 Checked
               'E5' => ['CHECK POWER MODE',0,0],               # ATA-7 Checked, non-data
               'E6' => ['SLEEP',0,0],                              # ATA-7 Checked
      'E7' => ['FLUSH CACHE',0,0],                    # ATA-7 Checked
               'E8' => ['WRITE BUFFER',0,0],
      'E9' => ['WRITE SAME',0,0],                         # ATA 8
      'EA' => ['FLUSH CACHE EXT',0,0],               # ATA-7 Checked
      'EC' => ['IDENTIFY DEVICE',0,0],               # ATA-7 Checked
               'ED' => ['MEDIA EJECT',0,0],                    # ATA-7 Checked
               'EE' => ['IDENTIFY DEVICE DMA',0,0],          # ATA 8
      'EF' => ['SET FEATURES',0,0],

               'F0' => ['VENDOR SPECIFIC',0,0],               # ATA 8
               'F1' => ["SECURITY SET PASSWORD",0,0],          # ATA-7 Checked
               'F2' => ["SECURITY UNLOCK",0,0],               # ATA-7 Checked
               'F3' => ["SECURITY ERASE PREPARE",0,0],          # ATA-7 Checked
               'F4' => ["SECURITY ERASE UNIT",0,0],          # ATA-7 Checked
               'F5' => ["SECURITY FREEZE LOCK",0,0],          # ATA-7 Checked
               'F6' => ["SECURITY DISABLE PASSWORD",0,0],     # ATA-7 Checked
               'F7' => ['VENDOR SPECIFIC',0,0],               # ATA 8
      'F8' => ['READ NATIVE MAX ADDRESS',0,0],     # ATA-7 Checked
      'F9' => ['SET MAX ADDRESS',0,0],

               'FA' => ['VENDOR SPECIFIC',0,0],               # ATA 8
               'FB' => ['VENDOR SPECIFIC',0,0],               # ATA 8
               'FC' => ['VENDOR SPECIFIC',0,0],               # ATA 8
               'FD' => ['VENDOR SPECIFIC',0,0],               # ATA 8
               'FE' => ['VENDOR SPECIFIC',0,0],               # ATA 8
               'FF' => ['VENDOR SPECIFIC',0,0],               # ATA 8


#      'FA' => ['IDLE & SET IDLE TIMER',0,0],
#      'FB' => ['ACTIVE & SET IDLE TIMER',0,0],
#      'FD' => ['CHECK IDLE MODE',0,0],


      'XX' => ['NOT FOUND',0,0]);




# use strict;
open(FH, "d:/1.txt") or die "Error!$!\n";

my @lines;   my @rows;
while (<FH>) {
 @rows = split(/\s+\|/);
  push @lines, @rows;

}
  foreach(@lines)
    {s/ //;
     redo if(/ /) ;}
print "@lines\n";

#my %cmd;
 my @index; my @speed; my @commands ; my $direction; my @tag; my @start_LBA;
my @transfer_length; my @check_commands;  my @space;  my $label_time;
my @delta_time; my @start_time;   my @end_time;  my $label;
my $j=0; my $i;

   for($i=0;$i<@lines;$i++)
     { if($lines[$i]=~/(^\s+$)|Speed|Tag|Direction|Gbps|(D->H)|(H->D)/)   {$space[$j]="";}
      elsif($lines[$i]=~/Store/)  {$index[$j]="Index";}
      elsif($lines[$i]=~/FISType/)   {$commands[$j]="Commands";}
      elsif($lines[$i]=~/Description/)  {$start_LBA[$j]="Start_LBA";$transfer_length[$j]="transfer_length";}
      elsif($lines[$i]=~/Timestamp/)   {$delta_time[$j]="Delta Time";$start_time[$j]="Start Time";$end_time[$j]="End Time";$label_time=0;}

      elsif($lines[$i]=~/^\d+$/)
         {  print "$start_time[$j] $delta_time[$j] $end_time[$j] $commands[$j] $start_LBA[$j] $transfer_length[$j]\n";
         $j=$j+1;
        #  if($j==1) {$index[$j]=1;}
         # else {$index[$j]=$index[$j]+1;}
         }

      elsif($lines[$i]=~/FIS.*0x([0-9A-F]+)/)
        { if(exists $cmd{$1})
         { $commands[$j] = $cmd{$1}[0];
           # $check_commands[$j]=$lines[$i];
          }
         else {$commands[$j]="Others";}
        }

      elsif ($lines[$i]=~/LBA.*0x([0-9A-Z]+)SecCnt=0x([0-9A-Z]+)/)
          {$start_LBA[$j]=hex($1); $transfer_length[$j]=hex($2);}

      elsif ($lines[$i]=~/(\d+)(ns|us|ms)/)
          {  if ($lines[$i]=~/(\d+)ns/)
                  {$delta_time[$j]=$1*1e-9;
             }

             elsif ($lines[$i]=~/(\d+)us/)
             {
              $delta_time[$j]=$1*1e-6;
             }

             elsif ($lines[$i]=~/(\d+)ms/)
             {
               $delta_time[$j]=$1*1e-3;
             }

             else
             {
              $delta_time[$j]=$lines[$i];
             }
           # $delta_time[$j]=$1; print "$1";
            if ($j==1) { $start_time[$j]=$label_time;}
            else {$start_time[$j]=$end_time[$j-1];}
            $end_time[$j]=$delta_time[$j]+$start_time[$j];


         }


     }
能否提供一个 1.txt 的样本作调试用?
不要太相信散列里的顺序,.
不要太相信散列里的顺序,为了能快速定位到一个值,散列里的key值是经过排序的,运行下下面这段代码就可以看到效果

#!/usr/bin/perl -W

use strict;
use warnings;

my %hs = (
  "1A" => "one A",
  "2A" => "two A",
  "20" => "three zero",
);

while ((my $key, my $value) = each %hs) {

  print $key." ".$value."\n";
}




   

Thanks for help!
luoyong 辛苦了!`` ````