Thursday, 3 October 2019

How to check perticular value is available in array using perl?


Method1:

if( grep( /^$value$/, @a ) ) {print "found\n"}


Method2:

for(@a) {if ($_ eq $value) {print "found\n"}}

Method3:

my %hash = map {$_ => 1} @a;
print "found\n" if(exists $hash{$value});

Method4:

print "found\n" if($value ~~ @a);


Method 5:

             foreach(split(/ /,join(" ",@array)))
             {
                  print "found"  if $_ eq 6;
             }


Method 6:[Take the Copy of Original Array]

$len=scalar(@a);

for($i=0;$i<=$len;$i++)
{
          for(shift @array){ print "found\n" if($_ == 11)}    
}


Method 7:[Take the Copy of Original Array]

$len=scalar(@a);
for($i=0;$i<=$len;$i++)
{
          for(pop @array){ print "found\n" if($_ == 11)}    
}


Method 8:[Take the Copy of Original Array]

$len=scalar(@array);
for($i=0;$i<=$len;$i++)
{
 print "found\n" if(splice(@array ,$i,1) eq 3);   
}

Method 9:

use List::MoreUtils (uniq);
@a = (1,2,3,9);
$value = 9;
print "found\n" if any {$_ eq $value} @a;



For More Methods Reach Me:@ letscrackperlinterviewblogspot@gmail.com  

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home