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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Array Filter and Lookahead
by CountZero (Bishop) on Nov 20, 2012 at 20:13 UTC | |
|
Re: Array Filter and Lookahead
by moritz (Cardinal) on Nov 20, 2012 at 19:06 UTC | |
by AnomalousMonk (Archbishop) on Nov 20, 2012 at 20:00 UTC | |
by moritz (Cardinal) on Nov 21, 2012 at 06:38 UTC | |
by daugh016 (Initiate) on Nov 20, 2012 at 21:00 UTC | |
by AnomalousMonk (Archbishop) on Nov 20, 2012 at 21:27 UTC | |
|
Re: Array Filter and Lookahead
by ww (Archbishop) on Nov 20, 2012 at 20:34 UTC | |
|
Re: Array Filter and Lookahead
by thundergnat (Deacon) on Nov 20, 2012 at 17:25 UTC | |
by AnomalousMonk (Archbishop) on Nov 20, 2012 at 19:51 UTC | |
|
Re: Array Filter and Lookahead
by Kenosis (Priest) on Nov 20, 2012 at 19:13 UTC | |
by AnomalousMonk (Archbishop) on Nov 20, 2012 at 20:13 UTC | |
by Kenosis (Priest) on Nov 20, 2012 at 20:29 UTC | |
by AnomalousMonk (Archbishop) on Nov 20, 2012 at 21:18 UTC | |
by Kenosis (Priest) on Nov 20, 2012 at 21:31 UTC |