#!/usr/local/bin/perl
#
# lindex() subroutine prototype
#   lindex.pl
#
# =Written By=============|=Version====|=Date========|=Comments========|
# ------------------------|------------|-------------|-----------------|
#  Grant Mongardi         | 0.00       | 09-26-2000  | Created	       |
#                         |            |             |                 |
#                         |            |             |                 |
# =====================================================================|
# 
# prototypes:
#    $index = lindex( $match_expression, @LIST [, $starting_index] ); 
#  @indexes = lindex( $match_expression, @LIST [, $starting_index] );
#
#  In a scalar context, lindex() 'short-circuits' on the first match and 
#  returns the index number of the first matching array entry.
#
#  In a list context, lindex() returns an array of index numbers of all 
#  matching array entries
#
# ============================================================================#
# ALL SUBROUTINES GO BELOW THIS LINE (Please describe briefly)			      #
# ============================================================================#

# ***************LINDEX ROUTINE************************
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# lindex() - matching arrays

sub lindex {

    my $start = pop;
    if ( $start !~ /^\d+$/ ){   #
        push( @_, $start );     # there's probably a better way
        $start = 0;             # to do this  
    }                           #
    my ($match, @list) = @_;
    my $count = 0;              # iterator
    my @ret = ();               # wantarray return
    my $max = @list;            # end of array
    while ( $count < $max ){
        if ($count >= $start){
            if ( $list[$count] =~ /$match/ && !defined wantarray() ){
               return $count;  # short circuit for scalar rettype
            } elsif ( $list[$count] =~ /$match/ && defined wantarray()){ 
                if (!defined @ret){
                    @ret = ($count); 
                } else {
                    push (@ret, $count);
                }
            }
        } 
        $count++;
    }
    return @ret;

}
# ***************END OF DEFAULT ROUTINE**************

__END__
