2013-11-14 2 views
1

스레드의 공유 참조 정보를 공유 @stories 변수에 저장하고 액세스 할 수 없습니다.hashref에서 해시 값을 가져올 수 없습니다.

my @stories : shared=(); 

    sub blah { 
    my %stories : shared=(); 
    <some code> 
     if ($type=~/comment/) { 
      $stories{"$id"}="$text"; 
      $stories{"$id"}{type}="$type"; 
      lock @stories; 
      push @stories, \%stories; 
     } 
    } 

# @stories is a list of hash references which are shared from the threads;

   foreach my $story (@stories) { 
       my %st=%{$story}; 
       print keys %st;  # <- printed "8462529653954" 
       print Dumper %st;  # <- OK 
       my $st_id = keys %st; 
       print $st_id;   # <- printed "1" 
       print $st{$st_id};  # <- printed "1/8" 
      } 

print keys %st works as expected but when i set in to a variable and print, it returns "1".

Could you please advice what I'm doing wrong. Thanks in advance.

+2

예상되는 결과는 무엇입니까? '$ st_id = keys % st'는'$ st_id = scalar (keys % st)'와 동일합니다. 이것은 해시'% st'의 키 수에'$ st_id '를 설정하는 것을 의미합니다. – mob

+0

$ st_id가 열쇠가 될 것으로 기대합니다. 즉, 8462529653954. –

+0

'my % st = % {$ story} '가 해시 복사본을 만들고 있다는 것을 알고 계십니까? '% st'에 대한 변경 사항은 원래 hashref에 반영되지 않습니다. – cjm

답변

3

In scalar context, keys %st 해시 %st의 요소 수를 반환합니다.

%st = ("8462529653954" => "foo"); 
$st_id = keys %st; 

print keys %st;    # "8462529653954" 
print scalar(keys %st);  # "1" 
print $st_id;    # "1" 

%st 중 하나의 키를 추출리스트 문맥에서 keys %st에서 할당을 확인하십시오.

my ($st_id) = keys %st;  # like @x=keys %st; $st_id=$x[0] 
print $st_id;    # "8462529653954" 
+0

그게 효과가 있습니다, 고마워요. 하지만 사실 키 % st가 왜 목록인지 이해할 수 없습니까? –

+0

@TigranKhachikyan 해시는 일반적으로 둘 이상의 키가 있기 때문에. – cjm

+0

있어 : 고마워. –

관련 문제