FAQ
Hello everybody,

Is there a function to find out if a given value exists in an array?

What I think of is something like &has_element(\@array, $value), which
should return either a boolean value or the index containing the value
(undef if $value is not found).
I'm sure something like this exists. Or do I need to write this myself?


kind regards,

Benjamin

Search Discussions

  • Paul Johnson at Feb 15, 2004 at 7:51 pm

    On Sun, Feb 15, 2004 at 08:26:08PM +0100, Benjamin Walkenhorst wrote:

    Is there a function to find out if a given value exists in an array?

    What I think of is something like &has_element(\@array, $value), which
    should return either a boolean value or the index containing the value
    (undef if $value is not found).
    I'm sure something like this exists. Or do I need to write this myself?
    Something close exists. I think I'd probably code it as:

    use List::Util "first";

    sub has_element
    {
    my ($array, $value) = @_;
    first { $_ eq $value } @$array
    }

    --
    Paul Johnson - paul@pjcj.net
    http://www.pjcj.net
  • Benjamin Walkenhorst at Feb 15, 2004 at 7:57 pm
    Hello,

    On Sun, 15 Feb 2004 20:50:59 +0100
    Paul Johnson wrote:
    I'm sure something like this exists. Or do I need to write this
    myself?
    Something close exists. I think I'd probably code it as:
    Thanks a lot! =)

    Kind regards,

    Benjamin
  • Jan Eden at Feb 16, 2004 at 12:04 am
    Hi,

    Benjamin Walkenhorst wrote:
    Hello,

    On Sun, 15 Feb 2004 20:50:59 +0100
    Paul Johnson wrote:
    I'm sure something like this exists. Or do I need to write this
    myself?
    Something close exists. I think I'd probably code it as:
    Thanks a lot! =)

    Kind regards,

    Benjamin
    And you could read the extensive thread named "Array containment" I started recently. I got several suggestions, including mapping to a hash and using grep.

    - Jan
    --
    There are 10 kinds of people: those who understand binary, and those who don't
  • Rob Dixon at Feb 16, 2004 at 11:57 am

    Paul Johnson wrote:
    Is there a function to find out if a given value exists in an array?

    What I think of is something like &has_element(\@array, $value), which
    should return either a boolean value or the index containing the value
    (undef if $value is not found).
    I'm sure something like this exists. Or do I need to write this myself?
    Something close exists. I think I'd probably code it as:

    use List::Util "first";

    sub has_element
    {
    my ($array, $value) = @_;
    first { $_ eq $value } @$array
    }
    Except that this will return the /value/ of the element
    for which the block returns true, which isn't obvious from the
    call. It's identical to

    sub has_element {

    my ($array, $value) = @_;

    foreach (@$array) {
    return $_ if $_ eq $value;
    }

    return;
    }

    which, to my mind, is preferable as it is clearer what's going on. It's
    certainly no slower.

    Rob
  • Wiggins d'Anconia at Feb 15, 2004 at 11:33 pm

    Benjamin Walkenhorst wrote:

    Hello everybody,

    Is there a function to find out if a given value exists in an array?

    What I think of is something like &has_element(\@array, $value), which
    should return either a boolean value or the index containing the value
    (undef if $value is not found).
    I'm sure something like this exists. Or do I need to write this myself?


    kind regards,

    Benjamin
    This was just discussed in depth, check the archives for the last couple
    of weeks. You may also want to read:

    perldoc -f grep

    http://danconia.org
  • R. Joseph Newton at Feb 16, 2004 at 6:02 am

    Wiggins d'Anconia wrote:

    Benjamin Walkenhorst wrote:
    Hello everybody,

    Is there a function to find out if a given value exists in an array?

    What I think of is something like &has_element(\@array, $value), which
    should return either a boolean value or the index containing the value
    (undef if $value is not found).
    I'm sure something like this exists. Or do I need to write this myself?


    kind regards,

    Benjamin
    This was just discussed in depth,
    True, but this is the first time I've seen Paul's suggestion, or at least the
    first time it has registered. I hope it will be offered more often, because it
    is the most direct I've seen for similar needs.

    Joseph
  • David le Blanc at Feb 17, 2004 at 6:46 am
    use List::Util "first";

    sub has_element($$)
    {
    my( $aref, $value ) = @_;
    first { $aref->[$_] eq $value } 0..$#$aref
    }

    ?


    -----Original Message-----
    From: Rob Dixon
    Sent: Monday, 16 February 2004 10:57 PM
    To: beginners@perl.org
    Subject: Re: Looking up values in arrays?

    Paul Johnson wrote:
    Is there a function to find out if a given value exists in an array?

    What I think of is something like &has_element(\@array, $value),
    which should return either a boolean value or the index containing
    the value (undef if $value is not found).
    I'm sure something like this exists. Or do I need to write this
    myself?
    Something close exists. I think I'd probably code it as:

    use List::Util "first";

    sub has_element
    {
    my ($array, $value) = @_;
    first { $_ eq $value } @$array
    }
    Except that this will return the /value/ of the element for which the
    block returns true, which isn't obvious from the call. It's identical to

    sub has_element {

    my ($array, $value) = @_;

    foreach (@$array) {
    return $_ if $_ eq $value;
    }

    return;
    }

    which, to my mind, is preferable as it is clearer what's going on. It's
    certainly no slower.

    Rob



    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org For additional
    commands, e-mail: beginners-help@perl.org <http://learn.perl.org/>
    <http://learn.perl.org/first-response>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedFeb 15, '04 at 7:32p
activeFeb 17, '04 at 6:46a
posts8
users7
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase