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!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|