Why do I get “Can't use string as a HASH ref” error when I try to access a hash element?

前端 未结 3 1004
灰色年华
灰色年华 2021-01-04 23:28

How do I fix this error?

foreach (values %{$args{car_models}}) {
   push(@not_sorted_models, UnixDate($_->{\'year\'},\"%o\"));
}

Error:

3条回答
  •  没有蜡笔的小新
    2021-01-04 23:59

    Hi if you have a hash ref variable (like $hash_ref) then code will be

    if ( ref($hash_ref) eq 'HASH' and exists $hash_ref->{year} ) {
        push(@not_sorted_models, UnixDate($hash_ref->{year},"%o")); 
    }
    #instead of below:
    if ( ref eq 'HASH' and exists $_->{year} ) {
        push(@not_sorted_models, UnixDate($_->{year},"%o")); 
    }
    

提交回复
热议问题