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

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

Monks, I am getting very confused with a concept. I am trying to adapt some code that I wrote from matching strings to matching elements of an array. My original code would take a string and return the last number in the string using a lookahead:

my $test_str = "1 text text text 2 text 3 "; my $test_out = q{}; my $sec_num_comb_pat = $empty_str; $sec_num_comb_pat .= '^(([0-9]+)(?![^0-9]*[0-9]))'; my $sec_num_rx = qr{$sec_num_comb_pat}xms; if ( $test_str =~ m{($sec_num_rx)}xms ) { $test_out = $1; } print "\$test_out\n" . $test_out . "\n";

The output would be 3

Now, I have an array @test that looks like the following:

#test[ 0]: "text text text 1 text"
#test[ 1]: "1 text text text 2 text 3 "
#test[ 2]: "text 1 text text 2 text"
#test[ 3]: "text text text 1 text 2 3.0"
#test[ 4]: "1 2 3 4 5 6 7 8 9 10 11"
#test[ 5]: "text text text text"

I want to get the last number in every element and put it into a new array @test_filtered. The output would look like the following:

#test_filtered[ 0]: "1"
#test_filtered[ 1]: "3"
#test_filtered[ 2]: "2"
#test_filtered[ 3]: "3"
#test_filtered[ 4]: "11"
#test_filtered[ 5]: ""

I have tried countless different things. Here is one attempt I have tried:

foreach (@test) { @test_filtered=grep{/^(([0-9]+)(?![^0-9]*[0-9]))/xsm} @test; }

Please help me! Thanks ahead of time!




UPDATE - 11.20.12 at 3:02 PM CST

Thank you everyone for the feedback! I have had some stuff come up tonight and will look at all the replies tomorrow.

Also, the reason for the truncating of the decimal is that the numbers are actually section numbers so sometimes they are written "section 1" and sometimes it could be "section 1.", "section 1)", "section 1.)", etc. I am just actually wanting the last section number in the string.

Thank you again Monks for all the help!