Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

How to check whether my array contains element x

by sandeepda (Novice)
on Jan 17, 2013 at 12:51 UTC ( #1013770=perlquestion: print w/ replies, xml ) Need Help??
sandeepda has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I am new to perl I am stuck in simple problem I need your help I want to define a subroutine. sub check_if_entity_exists(@array_to_be_checked,$entityName) I have array as
http-listener-1 http-listener-2 http-listener-3 http-listener-4
If i send http-listener-3 my subroutine should return me true and false if the array doesn't contain the element

Comment on How to check whether my array contains element x
Download Code
Replies are listed 'Best First'.
Re: How to check whether my array contains element x
by choroba (Canon) on Jan 17, 2013 at 13:07 UTC
    TIMTOWTDI: you can use the Smartmatch Operator, grep for the value, or just use the old plain loop:
    #!/usr/bin/perl use warnings; use strict; use Test::More; my @checks = ( sub { my ($entity, @array) = @_; $entity ~~ @array; }, sub { my ($entity, @array) = @_; grep $entity eq $_, @array; }, sub { my ($entity, @array) = @_; for (@array) { return 1 if $entity eq $_; } return; }, ); for my $check (@checks) { ok $check->('b', qw(a b c)); ok not $check->('z', qw(a b c)); } done_testing();
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      You forgot to mention hashes! =)

      DB<101> sub in { my ($entity, @array) = @_; my %hash; @hash{@array}=(); return exists $hash{$entity}; } DB<102> in(5,0..10) => 1 DB<103> in(42,0..10) => ""

      but I wouldn't pass @array as a flat list.

      Cheers Rolf

Re: How to check whether my array contains element x
by LanX (Canon) on Jan 17, 2013 at 13:03 UTC
    Recent Perl versions have the smart-match-operator '~~'.

    Try

    $entityName ~~ @array_to_be_checked

    Update
    DB<104> @array= qw/http-listener-1 http-listener-2 http-listener-3 h +ttp-listener-4/ DB<106> 'http-listener-2' ~~ @array => 1 DB<108> 'nonsense' ~~ @array => ""

    Cheers Rolf

Re: How to check whether my array contains element x
by vinoth.ree (Prior) on Jan 17, 2013 at 13:04 UTC

      Works nicely until $value = "http+listener+1".

      Make sure you \Q...\E or quotemeta your value.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
        ++ tobyink

        Thanks for the info!

      You might want to change /^$value$/ to /^\Q$value\E$/, especially if $value may contain re metacharacters, but it depends on the application.

      while (<>) { chomp; print qr(^$_$), "\t", qr(^\Q$_\E$), "\n"; } __END__ foo (?-xism:^foo$) (?-xism:^foo$) foo[bar] (?-xism:^foo[bar]$) (?-xism:^foo\[bar\]$)

      --MidLifeXis

      What they already said except that since you're not actually using regex, you shouldn't use regex. This will be faster and have fewer caveats (I inverted the statement as a matter of stylistic preference, not because it's necessary)–

      print "Found it.\n" if grep { $_ eq $value } @array;
Re: How to check whether my array contains element x
by BillKSmith (Deacon) on Jan 17, 2013 at 19:16 UTC
    See the function "any" in List::MoreUtils. It does exactly what you ask.
    Bill
Re: How to check whether my array contains element x
by Anonymous Monk on Jan 18, 2013 at 01:17 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1013770]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (15)
As of 2015-07-28 13:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (254 votes), past polls