Kanishka has asked for the wisdom of the Perl Monks concerning the following question:

i want to fetch the second line in the html when the first line is matched!
<option value="PDAConfig3.asp?sid=167216582&ID=136"> i705 <option value="PDAConfig3.asp?sid=167216582&ID=116"> III <option value="PDAConfig3.asp?sid=167216582&ID=118"> IIIc

Replies are listed 'Best First'.
Re: how do i get the 2nd line when the 1st is matched (quicky)
by tye (Sage) on Aug 17, 2004 at 04:32 UTC

    Since you are impatient (in the chatterbox), here is a quicky way:

    while( <> ) { if( /\Qsid=167216582&ID=1­16\E/ ) { my $nextLine= <>; print $nextLine; } }

    A bit shy on details, just like your question. (:

    - tye        

      $ egrep -A1 some-pattern in-files|grep -v '<option'
Re: how do i get the 2nd line when the 1st is matched
by Zaxo (Archbishop) on Aug 17, 2004 at 04:34 UTC

    With the obligatory warning that you should be using some HTML::Parser module, here is how to conditionally get the next line.

    # $re is the pattern to match my @next_lines; while (<FILE>) { push @next_lines, scalar(<FILE>) if /$re/; }
    The scalar call is needed to override list context. Without it, the diamond op would return the rest of the file.

    After Compline,
    Zaxo

Re: how do i get the 2nd line when the 1st is matched
by ikegami (Patriarch) on Aug 17, 2004 at 04:33 UTC
    One way is to treat both lines as one... ($match) = $text =~ /whatever_you_already_have\s*(\S+)/s

      Are you sure that pattern is what you want? It demands that the string be followed by any amount of whitespace (including none!), followed by at least one character of non-whitespace which will be capture. Here are some things it will match:

      $text = "whatever_you_already_havefoobar\n"; # $1 eq "foobar" $text = "whatever_you_already_have foo\nbar"; # $1 eq "foo" $text = "whatever_you_already_have\nfoobar"; # $1 eq "foobar" $text = "whatever_you_already_have\nfoo bar"; # $1 eq "foo"

      You probably meant something more along the lines of

      /whatever_you_already_have.*\n(.*)/

      Or if you want to do it using /s for other reasons,

      /whatever_you_already_have[^\n]*\n([^\n]*)/s

      Makeshifts last the longest.

        Aye, the solution can be adapted to more complex input if required. Thanks for expanding on it.
Re: how do i get the 2nd line when the 1st is matched
by murugu (Curate) on Aug 17, 2004 at 05:36 UTC

    Here is a way,

    while (<DATA>){ if (/ID=136/)######## any condition to be satisfied to print next l +ine. { my $t=<DATA>;#### do what ever u want with $t print $t; } } __DATA__ option value="PDAConfig3.asp?sid=167216582&ID=136"> i705 <option value="PDAConfig3.asp?sid=167216582&ID=116"> III <option value="PDAConfig3.asp?sid=167216582&ID=118"> IIIc
Re: how do i get the 2nd line when the 1st is matched
by johnnywang (Priest) on Aug 17, 2004 at 05:51 UTC
    Somehow I do not like calling <> inside another <> (just a personal thing), so my first version would be something like:
    my $match = 0; while(<INPUT>){ if($match){ print; $match = 0; } if(/<option value="PDAConfig3\.asp/i>{ $match = 1; } }
Re: how do i get the 2nd line when the 1st is matched
by Mr_Micawber (Beadle) on Aug 18, 2004 at 01:18 UTC
    Since TMTOWTDI how about this?
    (not as concise, but clearer (to me) )
    open( FILE, "<data.txt"); my @lines = (<FILE>); close FILE; my $i = 0; for(@lines){ chomp; print $_ if ( $lines[$i-1] =~ /116">$/ ) && ( $i ); $i++; }
    The second clause of the if conditional is to prevent the "wrap around" effect of a -1 array subscript.