my @array = ( 'north#library' , 'south#kitchen', 'east#hallway'); my $cmd = 'south'; (my $location) = join('|', @array) =~ m[$cmd\#(.*?)\|]; print $location; kitchen