Changing keys in a “for ( keys %hash ) {}”-loop

后端 未结 5 449
终归单人心
终归单人心 2021-01-14 08:12

I remember something about not changing the keys in a

for my $key ( keys %hash ) { ...

for example

for my $key ( keys %hash         


        
5条回答
  •  攒了一身酷
    2021-01-14 08:43

    See the keys function manpage:

    The returned values are copies of the original keys in the hash, so modifying them will not affect the original hash. Compare "values".

    You can delete or change hash elements by subscripting, e.g.

    delete $hash{$key};
    $hash{$key} = "foo";
    

提交回复
热议问题