I am looking for some help in one senario.
Requirement:
Through some validation, I have strored following kinds of value in one array.
@array_name = ("Rajesh","Raju","Ram","John","peter");
Now I know from some background that “Rajesh”, “Ram”, “peter”, are duplicate entries so I would expect my output to be:
@array_name = ("Rajesh","Raju","John");
# or
@array_name = ("Ram","Raju","John");
# or
@array_name = ("peter","Raju","John");
I have done example program such as below but it does not satisfy me…
my $spcific_output ="";
my $output ="";
foreach my $name (@array_name)
{
if($name eq "Rajesh" || $name eq "Ram" || $name eq "peter")
{
$spcific_output = "Rajesh and Ram and peter");
}
else
{
$output .= "My Name is $name";
}
}
$output .= $spcific_output;
Any best way to achieve this?
If you’re using v5.10 or higher you can use smart matching on an array with your duplicate names:
Output:
The condition in
grepevaluates!$seen++if a name from@namesis in@dupl, and keeps$_only when$seenis 0. Otherwise1(true) is evaluated and$_is kept.