bhaskar_219 has asked for the wisdom of the Perl Monks concerning the following question:

i am using the following logic @STATION_TYPE[$i] = map { $_=~m/\w+\s+(\w+)\s+\w+/ } grep /@Last_Element[$i]/, @CALL_NAME;
here for ex: @CALL_NAME having the information
test AB 25
test BC 29
test AC 25
test AA 30
test AA 31
test AA 25
test AA 20
and @Last_Element[$i] having value 25
So finally i am expecting output as @STATION_TYPE[$i] = AA that is last occurance of 25.
As of now i am getting output as AB that is first occurance in an array.
So could you give the solution how to find last occurance as a simple modification in the above code..

Replies are listed 'Best First'.
Re: How to find last occurance in an array
by ikegami (Patriarch) on Aug 28, 2010 at 01:54 UTC
    You could use a list slice.
    my $last = ( grep { ... } LIST )[-1];
      Thanks for your swift reply and I got the solution.
Re: How to find last occurance in an array
by AnomalousMonk (Archbishop) on Aug 28, 2010 at 04:50 UTC

    This is largely redundant now, but:

    >perl -wMstrict -le "use List::AllUtils qw(lastval); my @call_names = ( 'test AB 25', 'test BC 29', 'test AC 25', 'test AA 30', 'test AA 31', 'test AA 25', 'test AA 20', ); my $last_25 = lastval { m{(\d+)}xms and $1 == 25 } @call_names; print qq{'$last_25'}; " 'test AA 25'

    Or even (Larry help us)
        my $last_25 = lastval { (m{\d+}xmsg)[0] == 25 } @call_names;

Re: How to find last occurance in an array
by ww (Archbishop) on Aug 28, 2010 at 02:13 UTC
    Welcome to the Monastery.
    We may be able to help...
    if you provide enough detail that we can understand what you're doing!
    (See I know what I mean. Why don't you? and How do I post a question effectively? re "detail" and see Markup in the Monastery on formating your post (chiefly re <c>...</c> tags around code and data.)

    Please answer these questions by posting the code that generates the unsatisfactory output:

    • How and where is @Last_Element declared
    • what are $i's initial value and limit?
    • How are you incrementing $i?

    I -- for one -- am having a hard time crystal-balling code that would behave as you say. One standard bit of advice here is that those with questions post code that actually compiles under strict and warnings.

    Note (just for starters) that @STATION_TYPE[$i] offends the warnings pragma (which we hope you are using along with use strict;) instigating the message: Scalar value @STATION_TYPE[$i] better written as $STATION_TYPE[$i]

    Oh, yes: the standard way to find the last element in an array is to use the subscript -1 ("print $CALL_NAME[-1];") ... which, of course is not the element you actually want. But if you were to iterate backward from $CALL_NAME[-1] looking for the FIRST "25" you could save a lot of processing time.

Re: How to find last occurance in an array
by bart (Canon) on Aug 28, 2010 at 21:42 UTC
    Search from the back. You could reverse your original array — put it somewhere after the map plus block.

    And, uh, your original code is not very good. For example, it'll match "2250" too.