sub slow_match { my ( $hash_ref_1, $hash_ref_2 ) = @_; # %texts, %patterns coming from global (was: %hash1, %hash2) my %matches; foreach my $pattern ( keys %$patterns_ref ) { $matches{$pattern} = qr/\b$pattern\b/; } while ( my ( $text_id, $text ) = each %texts ) { study $text; while ( my ( $pattern, $high_lvl_id ) = each %patterns ) { if ( $text =~ $matches{$pattern} ) { $$hash_ref_1{$text_id} .= ':'.$high_lvl_id; foreach my $part (split(/\s/,$pattern)) { $$hash_ref_2{$text_id} -> {$part} = 0; } } } } }