#!/usr/bin/perl use strict; ### show what we're looping over (a,1,b,2,c,3) my %hash = (a=>1, b=>2, c=>3); for (%hash) { print "OK: $_\n"; } ### check the value *before* we attempt to delete the key ### i.e. 1 before a, 2 before b, 3 before c %hash = (a=>1, b=>2, c=>3); for (reverse %hash) { substr $_, 0, 1; delete($hash{$_}) if /[a-z]/; print "Still-OK: $_\n"; } ### emulate your structure where we can potentially delete ### a key first, then loop over the value associated with it %hash = (a=>1, b=>2, c=>3); for (%hash) { substr $_, 0, 1; # <== segfaults here when testing value associated with deleted key delete($hash{$_}) if /[a-z]/; print "Not-OK: $_\n"; } __END__ =head1 OUTPUT OK: a OK: 1 OK: b OK: 2 OK: c OK: 3 Still-OK: 3 Still-OK: c Still-OK: 2 Still-OK: b Still-OK: 1 Still-OK: a Not-OK: a Not-OK: Segmentation fault (core dumped)