http://www.perlmonks.org?node_id=1004779


in reply to Array Filter and Lookahead

Here's another option:

my @test_filtered = map { (/(\d+)[\d.]*/g)[-1] // '' } @test;

Update: changed to // to handle 0s. Thanks again, AnomalousMonk.

Replies are listed 'Best First'.
Re^2: Array Filter and Lookahead
by AnomalousMonk (Archbishop) on Nov 20, 2012 at 20:13 UTC
    >perl -wMstrict -le "my @test = ('0 xx', 'xx 0', '0'); ;; my @test_filtered = map { (/(\d+)[\d.]*/g)[-1] || '' } @test; printf qq{'$_' } for @test_filtered; " '' '' ''

    Update:  // vice  || does the trick.

      Excellent catch, thank you! Changed disjunct positions...

        Changed disjunct positions...

        But that just causes a problem for strings that have no digits at all, e.g., 'xxx', which daugh016 wants to extract as '' (empty string).