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