#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11137405 use warnings; my $random = join '', map qw(A C G T)[rand 4], 1 .. 50; for my $seq ( qw( AGCAGC AATGCAATCGCAGCAGCA AGCTACCCAGCTAGGGAGCTA AAA_x_AAA_x_BBB_x_AAA_x_AAA_x_BBB ), $random ) { my %found; $seq =~ /([A-Z]{3,}) .* \1 (?{ $found{$1}++ }) (*FAIL)/x; my %counts = map { $_, scalar(() = $seq =~ /$_/g) } keys %found; use Data::Dump 'dd'; dd "for sequence $seq", \%counts; }