in reply to Array location?

A gazillion ways:). Here's one

my $idx=0; $idx++ while $array[$idx] ne 'entry' and $idx < $#array; # Added! See [272470]. [cees]++ die 'Entry not found' if $idx == $#array and $array[-1] ne 'entry'; ## $array[$idx] is (the first!!) matching entry.

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Replies are listed 'Best First'.
Re: Re: Array location?
by cees (Curate) on Jul 08, 2003 at 21:15 UTC

    You will get stuck in an endless loop with that one if the match is not in the array. I'm sure you meant:

    my $idx=0; $idx++ while $array[$idx] ne 'entry' && $idx < $#array; die 'Entry not found' if $idx == $#array and $array[-1] ne 'entry';

      cees++ Updated, thanks.

      Yes! I'm sure that's what I meant :)


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller