Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The following might be a good starter assuming the file is small enough to be slurped in (otherwise you'll want to loop over each line):

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; ### slurp in the data into our scalar my $data; { local $/; $data = <DATA>; } ### if you want to see all the matches my @matches = ($data =~ m/matchingStuffHere[0-9]+/g); print Dumper(\@matches),"\n"; ### only pull the third token my $third_token = ( $data =~ m/matchingStuffHere[0-9]+/g )[2]; print $third_token,"\n"; ### pull the entire line that contains the third token my $third_token_line = ( grep { m/matchingStuffHere[0-9]+/ } split($/, +$data) )[2]; print $third_token_line,"\n"; ### kindly donated from fortune __DATA__ Do matchingStuffHere1 you suffer painful elimination? Do you matchingStuffHere2 suffer painful recrimination? Do you suffer painful matchingStuffHere3 illumination? Do you suffer painful hallucination matchingStuffHere4? In order to succeed matchingStuffHere5 in any enterprise, one must be +persistent and patient. Saints should always be matchingStuffHere6 judged guilty until they ar +e proved innocent. By trying we can easily learn to endure adversity. Another man's, I m +atchingStuffHere7 mean.

I'm not sure exactly where you are going with this if you only want the third token (since you said the tokens are all the same). The preceding example code assumes the tokens are different so that it is easy to identify which one matched.


cp
----
"Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."

In reply to Re: for loop to get the 3rd index by crouchingpenguin
in thread for loop to get the 3rd index by Anonymous Monk

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



  • 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.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 20:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found