Is In Array?
I was searching if there was a function like in_array of PHP in Perl, but there is not such function, but, it is still very easy to check!
This was useful for me so i hope it is for you
In PHP:
<?php
$string = ‘fin_helm’;
$array = array(‘full_plate’,’manteau’,’boots’,’two_handed_sword’,’fin_helm’);
if(in_array($string, $array))
{
print “$string is in the array”;
}
?>
In Perl:
#!/usr/bin/perl
my $string = ‘fin_helm’;
my @array = qw/full_plate manteau boots two_handed_sword fin_helm/;
if(grep $_ eq $string, @array)
{
print “$string is in the array”;
}
So, those codes do exactly the same.
If you don’t know much perl you will be wondering what does the grep function, the grep function examines each of an element of an array (represented with $_) and then we make a comparison or something with it, in this case $_ eq $string, grep, returns the elements with where true, in this case we are not requesting the elements, we are requesting a sacalar value, so, in perl 0 is false, and every other number is true, that will return 1, so its true, and the element is in the array π
I hope you all find this useful, bye
on September 13, 2007 on 4:33 am
Found this useful, cheers.
on October 16, 2007 on 4:19 pm
This helped a lot for me coming from PHP.
on December 28, 2007 on 9:45 am
Thanks a lot.
on January 9, 2008 on 6:31 am
Thanks – this helped me as a Perl programmer learning PHP.
BTW, if you are just searching for a string in the array, you could slightly simplify the Perl by replacing
if(grep $_ eq $string, @array)
with
if(grep /$string/, @array)
on February 3, 2008 on 3:32 pm
Great, exactly what I was looking for… helped me a lot when digging out perl code and need to extend it.
on April 13, 2008 on 11:41 am
Thanks for this! I’ve used grep before, but I didn’t think of it used like this.
In response to Andy:
grep /$string/, @array
uses regular expressions, which is perfect if you need the full complexity of searching the array against a regular expression pattern. If you are only searching for a string in the array,
grep ($_ eq $string, @array)
is much faster and CPU friendly.
on April 18, 2008 on 4:15 am
[…] Taken from: https://assasiner.wordpress.com/2006/12/03/is-in-array/trackback/ […]
on April 21, 2008 on 1:38 pm
One question… if I have an array of hashes, this routine can be usefull as well? How should I use it?
For example:
$array = [ {id=>1,name=>’aaa’},
{id=>2,name=>’bbb’},
{id=>3,name=>’ccc’},
]
How should I wirte the routine to find inside the array the id=>2 using the grep?
TIA
on April 21, 2008 on 2:18 pm
#!/usr/bin/perl
use strict;
my @array = ({id=>’1′,name=>’aaa’},{id=>’2′,name=>’bbb’},{id=>’3′,name=>’ccc’});
if(grep{ $$_{‘id’} eq ‘2’ } @array)
{
print ‘I found 2’;
}
on April 21, 2008 on 4:28 pm
Thnx a lot.
on April 21, 2008 on 4:38 pm
no problem :]
on April 21, 2008 on 4:46 pm
Sorry to bother you more… I got how to use it but… if I want to retrieve the complete hash when the grep founds the occurrence… how do I assign it to a variable?
TIA
on April 21, 2008 on 4:50 pm
if(grep{ $$_{βidβ} eq β2β² } @array)
{
print ‘My id is: ‘.$$_{‘id’}.’ and my name is ‘.$$_{‘name’];
}
that should work :]
on April 21, 2008 on 4:52 pm
ok i’ll try it and let you know how well it was π
And Thnx again.
on September 2, 2010 on 6:17 am
hi
on September 2, 2010 on 6:17 am
hellloo
on April 21, 2008 on 5:25 pm
Ok this is my new problem now… each element in the array is a hash. I know with that if that I can access to any element inside the hash that is found. but how can I have access to the complete hash as a whole.
Is that posible? I’ve tried with $$_ or $_ but those are not working to give me the complete hash.
on April 21, 2008 on 5:33 pm
I think $_ is a reference to the hash… But apart from $_ and $$_ i dont know really, im sorry i dont know much perl hehe i’m still learning, slow but learning π
on June 2, 2008 on 10:35 am
Found this very handy, ty. One thing I found was grep is a regex based function, so if you’re not careful it will match things you might think it shouldn’t.
eg
my @haystack = (‘fifteen’,’fish’);
my $needle = ‘fi.’;
my $result = grep(/$needle/,@haystack);
At this point $result will equal 2, because the regex ‘fi.’ matches both of the elements. You need to escape the needle before you test it.
sub in_array {
my $needle = shift(@_); # needle is the first argument
$needle =~ s/([^0-9a-zA-Z])/\\$1/g; #swap anything that could need escaping to escaped form
if(grep(/$needle/,@_)>0){return 1;} # check the array
return 0; # else it didn’t match.
}
on June 19, 2008 on 8:41 am
Somehow i missed the point. Probably lost in translation π Anyway … nice blog to visit.
cheers, Gomorrah!
on July 3, 2008 on 5:03 pm
Still handy. π
on July 11, 2008 on 12:28 pm
[…] was looking for this useful function I always use in php and found this simple article. Basically, the function that permit you to do that is grep used in this way: my $string = […]
on August 6, 2008 on 8:12 am
Very useful, thanks a lot! This was surprisingly hard to find for being such a basic trick.. Cheers
on October 16, 2008 on 11:03 am
Thanks a lot!
on October 16, 2008 on 11:50 am
worked great! thanks :]
on February 16, 2009 on 3:24 pm
regarding andy’s post:
”
BTW, if you are just searching for a string in the array, you could slightly simplify the Perl by replacing
if(grep $_ eq $string, @array)
with
if(grep /$string/, @array)
”
please note that the second line will match $string to _substrings_ of @array elements, eg $string=12 matches @array=(“september 12th”) as well as @array=(12).
on April 15, 2009 on 4:32 pm
Check cpan
http://search.cpan.org/~vparseval/List-MoreUtils-0.22/lib/List/MoreUtils.pm
use List::MoreUtils(qw);
if( any{$_ eq $string} @array)
{
print β$string is in the arrayβ;
}
on April 25, 2009 on 1:22 pm
hello , am biggener in perl can you help me in this array:
@array=();
print “enter (1) to determine the length of array\n”;
print “enter (2) to enter the element of array\n”;
print “enter (3) to find zeroes\n”;
print “enter (4) to find the max\n”;
print “enter (5)to find the even number\n”;
print “enter (6) to find the sum of odd number\n”;
print “enter (7) to find the prime numbers\n”;
print “enter (8) to to add to array \n”;
print “enter (9) to sort array descending \n”;
print “enter (10) to creat associated array\n”;
print “enter (11) to print a key & values of associated array\n”;
print “enter (12) t0 exit \n”;
please ..
on May 23, 2009 on 7:26 pm
Thanks – just what I was looking for π
on June 18, 2009 on 8:25 pm
Thaks for posting this, it’s a great option.
on August 28, 2009 on 6:32 am
Thanks, very helpfull
on March 23, 2010 on 6:53 pm
Dude!
YOu rock…
if(grep $_ eq $string, @array)
and
The reverse
if(!grep $_ eq $string, @array)
Made my day…
Such an easy way to calculate if a value is in an array…
Everything should be this easy.
on November 18, 2010 on 12:10 am
For what it’s worth:
If you intend to search the array more than once, it’s actually faster to hash it.
my @array = ( ‘a’ .. ‘z’ );
my %hash = map { $_ => 1 } (@array);
if ($hash{ $something }) { print “$something is in the array” }
on June 16, 2012 on 2:26 am
This is very good. I didn’t think about it this way.
on May 15, 2011 on 12:57 am
Thanks very useful!
on September 5, 2011 on 1:13 am
How to code perl to php
sub distinct{
my(@temps,@arrays,$size,$step1,$step2,$exit);
@temps=();
@arrays=();
$size = 0;
$step2=0;
@arrays = @_;
$size = @arrays;
if($size > 0){
for($step1=0;$step1<$size;$step1++){
$exit=0;
for(@temps){
if($_ eq $arrays[$step1]){ $exit = 1;}
}
if($exit == 0 && $arrays[$step1] ne ""){
$temps[$step2++] = $arrays[$step1];
}
}
}
$size = @temps;
return @temps;
}
Help me. please
on October 24, 2012 on 9:59 am
Thanks a lot!
on August 14, 2013 on 3:27 pm
sub in_array {
my ($string, $array) = @_;
my $found = 0;
foreach (@$array) {
if ($_ == $string) {
$found = 1;
}
}
if ($found) {
return 1;
}
return 0;
}
usage: in_array($string, \@array)