my %fragment_exists; sanitize_name(sub {map { $_ . "_" . $fragment_exists{ $_ }++} @_ }, $title, ...); #### my %fragment_exists; my $fragment; my $duplicate = ''; do { $fragment = sanitize_name( "$title_" . $duplicate++ ); while( $fragment_exists{ $fragment }++ );