in reply to Re: find position of element of array
in thread find position of element of array

Hi Guys, I tried this code. At first I thought it was working fine. But now I see that there is a small problem with this code. The problem is : the index is circular (semi circular I guess). That means, if my array is @a = {"a", "b", "c", "d", "e"} and I want to find the element before "a", it should be undefined. But when I print the element before "a" using the above method and (defined $array$index -2), I get "e". Also, it is strange that when I check like: if (defined $array$index +2), that element is correctly not defined. If you understand this problem, please help me out. Thanks
  • Comment on Re^2: find position of element of array

Replies are listed 'Best First'.
Re^3: find position of element of array
by tirwhan (Abbot) on Nov 13, 2005 at 11:55 UTC

    Just check for the returned index being 0 and handle that special case appropriately. Negative indexes count backwards, so in your above array $a[-1] will correctly return "e". $a[5] will be undef, because the array only contains values for @a[0..4]. Take a look at perldoc perldata for more details on arrays.

    BTW, you should add <code></code> tags around the code in your posts, otherwise it comes out mangled.


    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
      Thanks a lot... I will do as you say.