#!/usr/bin/perl -w use strict; my $str = "1b22a3d3cab"; my @letters = qw (a b 2 c d); my @order_found =(); my %seen =(); #UPDATE: don't need the $found variable, oops! #while (my $found = $str =~ m/([join("",@letters)])/go) #should have been: while ($str =~ m/([join("",@letters)])/go) { push (@order_found, $1) unless $seen{$1}; $seen{$1}++; } if (keys %seen != @letters) { print "not all letters found\n"; } else { print @order_found, "\n"; # prints b2adc }