Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

for loop to get the 3rd index

by Anonymous Monk
on Jun 06, 2003 at 13:42 UTC ( #263657=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a matching part that gets about 9 identical listings from my file. I only need to get the 3rd listing (just one result only). Please advise How I can do this. I tried a for loop but its not working:
while (<TMPFILE>) { chomp; if($_ =~ /matchingStuffHere/) { for(my i=0;i<9;i++) { print $_[2]; #just want the 3rd index } } }

Replies are listed 'Best First'.
Re: for loop to get the 3rd index
by hardburn (Abbot) on Jun 06, 2003 at 14:03 UTC

    What is i in your for loop? It's not a variable. I think you meant:

    for(my $i = 0; $i < 9; $i++) { . . . }

    Which can be written more simply as:

    for my $i (0 .. 8) { . . . }

    Where is @_ coming from? $_ is not the same as @_. As far as perl is concerned those two variables are only similar in that they share a glob.

    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: for loop to get the 3rd index
by crouchingpenguin (Priest) on Jun 06, 2003 at 14:48 UTC

    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.

    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
Re: for loop to get the 3rd index
by Grygonos (Chaplain) on Jun 06, 2003 at 13:47 UTC
    ok if i'm understanding you correctly.. you're reading from a file and wanting to find the third match? here's how
    my $match_count = 0; my $third_match; while (<TMPFILE>) { chomp; if($_ =~ m/matchingStuffHere/) { $match_count++; if($match_count eq 3) { $third_match = $1; } last; } }
    thats my solution .. hope it helps..
      There are some problems with your solution.
      1. To get your match into $1, it should be m/(matchingStuff)/. That is, there needs to be parens.

      2. Your 'if' statement should be if ($match_count == 3). This is a numerical comparison.
      Try this:
      my @matches; open (F,"files.txt") || die ("Couldn't open file: $!\n"); while(<F>) { chomp; if (/matchingstffHere/) { push(@matches, $_); } } close(F); if (scalar(@matches) >= 3) { print "Needed = $matches[2].\n"; }
      oops.. drop that call to last; on the line after $third_match = $1; sorry... other wise it will drop the loop on first match.. sorry

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://263657]
Approved by broquaint
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2018-10-23 08:26 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (125 votes). Check out past polls.