Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: lsearch for perl?

by VSarkiss (Monsignor)
on Dec 05, 2005 at 16:16 UTC ( [id://514167]=note: print w/replies, xml ) Need Help??


in reply to lsearch for perl?

You have some excellent examples above of how to achieve what you asked for. But I want to address your second point:

Or do you just program to avoid such cases?

That's really the heart of the matter. In Perl, it's much more convenient to iterate over array or list items directly, rather than trying to refer to them by index. If you find yourself writing:

for my $i (0 .. $#array) { if ($array[$i] eq ... ) {
immediately stop and change that to:
for my $i (@array) { if ($i eq ... ) {
In the long run, it'll help you to avoid off-by-one problems as well.

Of course, there are situations where you really do need array indexes, but that need is much rarer in Perl than in, say, C, or Tcl, or similar languages.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-28 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found