my $text = "(a + 13) * (b - 10 ) / c"; # select all individual alpha characters my @vars = ($text =~ /[a-zA-Z]/g); # get uniques out of the array my %hash; map { $hash{$_}++ } @vars; my @unique_vars = keys %hash;