Daniel David wrote:
> 
> Hi,

Hello,

> I couldn't seem to find a built-in function for finding the index
> of an element in an array...so I wrote this one:
> 
> --------------------------------------------------------------------
> 
> #  position_of returns the position of a string in an array of strings,
> #  and -1 if the string is not a member of the array.
> 
> sub position_of {
>    my ($x,@y) = @_;
>    foreach $z (0..$#y) {
>       if (@y[$z] eq $x) {
>          return ($z);
>       }
>    }
> 
>    return -1;
> }
> ----------------------------------------------------------------------
> 
> it works.... but somehow i feel there's a built in function for this

If you just want to know that that particular item exists then that will
work (simplified as):

sub position_of {
    my $x = shift;
    for my $z ( 0 .. $#_ ) {
        if ( $_[$z] eq $x ) {
            return $z;
            }
        }
    return -1;
    }

If you want to delete to item:

@array = grep $_ ne $x, @array;

If you want to modify the item:

for ( @array ) {
    if ( $_ eq $x ) {
        # modifying $_ will modify the actual
        # item in @array
        $_ = 'something else';
        }
    }



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to