#!/usr/local/bin/perl -w use strict; my @strings=("~cake,pastry","pastry,~cake","cake,pastry"); foreach my $i ( 0..2 ) { for ( 0 .. 2) { next if ( $i == $_ ); my %left=map { $_ => 'nevermind' } split /,/,$strings[$i]; my %right=map { $_ => 'nevermind' } split /,/,$strings[$_]; for (keys( %left )) { delete $left{$_} if exists $right{$_}; } print "String $i matches $_!\n" unless ( keys %left || keys %right ); } } __END__ Output: String 0 matches 1! String 1 matches 0!