Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: for loop to get the 3rd index

by crouchingpenguin (Priest)
on Jun 06, 2003 at 14:48 UTC ( #263692=note: print w/ replies, xml ) Need Help??


in reply to for loop to get the 3rd index

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."


Comment on Re: for loop to get the 3rd index
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://263692]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-12-22 00:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (109 votes), past polls