Looking up values in arrays?

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Looking up values in arrays?

    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
    Benjamin Walkenhorst Guest

  2. Similar Questions and Discussions

    1. Nesting Arrays within arrays.
      How do i nest an array inside another array? something like array(i).aray2(j); Can I do that?
    2. Matching values of an 2 arrays then move
      i am pulling data via the param tags in to 2 different arrays. Here is what I want to happen: when value1 of the first array matches pullDate of...
    3. Why don?t the location values in the Sprite Overlay agree with the values
      I would expect the left,top,right bottom values for a sprite that appear underneath it on the stage when you click on it to agree with the l,t,r,b...
    4. Sort a hash based on values in the hash stored as arrays of hashes
      Hmm. I'm not quite sure if I got the subject right, but I'll try to explain. :-) I've got a hash of elements stored like this: $VAR1 = {...
    5. Adding custom values and database values to DropDwonList
      The other way is to create a DataTable, put the --None-- as the first Row, then read the datareader into the DataTable, and append the --other-- at...
  3. #2

    Default Re: Looking up values in arrays?

    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 - [email]paul@pjcj.net[/email]
    [url]http://www.pjcj.net[/url]
    Paul Johnson Guest

  4. #3

    Default Re: Looking up values in arrays?

    Hello,

    On Sun, 15 Feb 2004 20:50:59 +0100
    Paul Johnson <paul@pjcj.net> 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
    Benjamin Walkenhorst Guest

  5. #4

    Default Re: Looking up values in arrays?

    Benjamin Walkenhorst wrote:
     

    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
    Wiggins Guest

  6. #5

    Default Re: Looking up values in arrays?

    Hi,

    Benjamin Walkenhorst wrote:
     
    >>
    >> Something close exists. I think I'd probably code it as:[/ref]
    >
    >Thanks a lot! =)
    >
    >Kind regards,
    >
    >Benjamin
    >[/ref]

    And you could read the extensive thread named "Array containment" I startedrecently. I got several suggestions, including mapping to a hash and usinggrep.

    - Jan
    --
    There are 10 kinds of people: those who understand binary, and those who don't
    Jan Guest

  7. #6

    Default Re: Looking up values in arrays?

    Wiggins d'Anconia wrote:
     
    >
    > This was just discussed in depth,[/ref]

    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



    R. Guest

  8. #7

    Default Re: Looking up values in arrays?

    Paul Johnson wrote: 
    >
    > 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
    > }[/ref]

    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


    Rob Guest

  9. #8

    Default RE: Looking up values in arrays?


    use List::Util "first";

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

    ?


    -----Original Message-----
    From: Rob Dixon [mailto:port995.com]
    Sent: Monday, 16 February 2004 10:57 PM
    To: org
    Subject: Re: Looking up values in arrays?

    Paul Johnson wrote: [/ref]
    myself? 

    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: org For additional
    commands, e-mail: org <http://learn.perl.org/>
    <http://learn.perl.org/first-response>



    David Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139