To find the characters in the first string that are not present in the second string in Perl, you can use arrays and array subtraction.
In this example,
$str1
and $str2
with different values.split
function.$str1
array that are not present in $str2
array.# Find Characters Not Present in Second String
my $str1 = 'Hello World';
my $str2 = 'apple';
my @chars1 = split('', $str1);
my @chars2 = split('', $str2);
my %count = ( );
foreach my $char (@chars2) {
$count{$char}++;
}
my @diff = grep { $count{$_}-- <= 0 } @chars1;
my $result = join('', @diff);
print "Characters not present in second string: $result\n";
Characters not present in second string: HWrd
In this tutorial, we learned How to find the Characters in the First String that are not Present in the Second String in Perl language with well detailed examples.