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

I have this date in my file:   sbs/c/b/cow.bld:three.bld I am trying to only grep the last .bld file which is three.bld

I am doing this now   push(@last, $1) && last if  (m/(\w+\.bld(?!.*#))/) but is getting the first build ...

Edit by dws for formatting and title

Replies are listed 'Best First'.
Re: Problem with pattern match
by ichimunki (Priest) on Jul 09, 2002 at 21:56 UTC
    If you know you want the last thing on the line, use an anchor ($).
    push(@last, $1) && last if /(\w+)\.bld$/; #or if you want 'three.bld' in the push: push(@last, $1) && last if /(\w+\.bld)$/;
Re: Problem with pattern match
by George_Sherston (Vicar) on Jul 09, 2002 at 22:10 UTC
    If you're ever likely to want to pull out different ones from the list, you might like to take advantage of the fact that, in array context, a pattern match returns a list of everything that was captured. So
    my $s = 'sbs/c/b/cow.bld:three.bld'; my @a = $s =~ /(\w+\.bld)/g; print $a[$#a];
    ... will print out the one you want, but also leave you with an array with all the others in it as well.

    § George Sherston
Re: Problem with pattern match
by abaxaba (Hermit) on Jul 09, 2002 at 21:55 UTC
    Why not just:
    push (@last, $1) && last if (/:(^:)+$/);
    HTH.
    ÅßÅ×ÅßÅ
    "It is a very mixed blessing to be brought back from the dead." -- Kurt Vonnegut