Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

(ar0n) Re: Idiomatic Array Index Search?

by ar0n (Priest)
on May 27, 2001 at 01:14 UTC ( [id://83548]=note: print w/replies, xml ) Need Help??


in reply to Idiomatic Array Index Search?

I'd probably write it as:
sub arrayindex { my $value = shift; for (0..$#_) { return $_ if $_[$_] eq $value; } return -1; }

Update code changed to reflect MeowChow's comment.

The wrong way to do it would be:
my $index; for $index (0..$#values) { last if $values[$index] eq $value; }

ar0n ]

Replies are listed 'Best First'.
Re: Re: Idiomatic Array Index Search?
by MeowChow (Vicar) on May 27, 2001 at 04:13 UTC
    What if there's no match? That would be indistinguishible from matching the last element.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://83548]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-19 12:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found