Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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!


In reply to Array Filter and Lookahead by daugh016

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (9)
    As of 2014-12-18 07:25 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (44 votes), past polls