#!/usr/local/bin/perl
#
# Demonstration of LINDEX()
#   tstlindx.pl
#
# =Written By=============|=Version====|=Date========|=Comments==============|
# ------------------------|------------|-------------|-----------------------|
#  Grant Mongardi         | 0.00       | 09-26-2000  | Created	             |
#                         |            |             |                       |
#                         |            |             |                       |
# ===========================================================================|
# 

$| = 1;		# don't buffer output

&default;

# ============================================================================#
# ALL SUBROUTINES GO BELOW THIS LINE (Please describe briefly)			      #
# ============================================================================#

# ***************DEFAULT ROUTINE************************
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# default() subroutine

sub default {
my ( @array, @grep_results );
my ($num, $indx, $expr1, $expr2, $grep_expr);

$expr1 = 'This';
$expr2 = 'lindex';
$grep_expr = 'grep';
$indx = 2;

@array = (
"\n\tThis is String number 0 and should not match anything",
"\n\tThis is string number 1 and will match using grep",
"\n\tCrap String",
"\n\tCrap String",
"\n\tCrap String",
"\n\tCrap String",
"\n\tCrap String",
"\n\tCrap String",
"\n\tCrap String",
"\n\tCrap String",
"\n\tThis is string 10 and will match grep and lindex",
"\n\tThis is string 11 and will match grep" );

    print "START================================================START\n ";
    @grep_results = grep { /$grep_expr/ } @array;
#     $num = &lindex( $expr1, @array, $indx );
#     $num = &lindex( $expr2, @array );
    my @res = &lindex( $expr1, @array );

    print "\ngrep matched on ";
    print @grep_results;
    print "\nBUT I HAVE NO IDEA WHICH LIST ENTRY THEY ARE\nWITHOUT GOING BACK TO THE LIST!\n ";

    print "\nWhile lindex\(\) matched on String numbers:\n";
    foreach (@res){
        print "$_ -> $array[$_] \n";
    }
#    print "\n    Index $num: " . $array[$num];
    print "\n\n\nEND=================================================END\n";

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

# ***************LINDEX ROUTINE************************
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# lindex() retunrs the index into the array of the first
#          entry that matches the expression.

sub lindex {

    my $start = pop;
    if ( $start !~ /^\d+$/ ){
        push( @_, $start );
        $start = 0;
    }
    my ($match, @list) = @_;
    my $count = 0;
    my @ret = ();
    my $max = @list;
    while ( $count < $max ){
        if ($count >= $start){
            if ( $list[$count] =~ /$match/ && !defined wantarray() ){
               return $count;
            } elsif ( $list[$count] =~ /$match/ && defined wantarray()){ 
                if (!defined @ret){
                    @ret = ($count);
                } else {
                    push (@ret, $count);
                }
            }
        } 
        $count++;
    }
    return @ret;

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

__END__
