Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: for loop to get the 3rd index

by crouchingpenguin (Priest)
on Jun 06, 2003 at 14:48 UTC ( [id://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."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-03-29 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found