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

How do I print the whole line if I get a hit on a reg expression?
for(@mydata) { if($_ =~ /abc/g) { print "$_\n"; #print the line } }

Replies are listed 'Best First'.
Re: print the whole line
by BrowserUk (Patriarch) on Jan 21, 2004 at 13:35 UTC
    /abc/ and print for @mydata;

    If you want to print the whole line whenyou get a hit, your /g modifier serves no purpose.

    $_ =~ /abc/ is doing the same as /abc/. If your going to use $_, you might as well make full use of it:)


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Timing (and a little luck) are everything!

      Thanks but for some reason it prints out the whole page when I get a hit and not just each line that I get a hit on.
      my $content = get($url); /abc/ and print for $content;

        That's because you have the whole page stored as a single line. In your original post you showed an array. Try

        my @content = split /\n/, get($url); /abc/ and print for @content;

        But note: What will be considered a "line" in html source bears no realationship to the "lines" that appear on the screen in your browser.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        Timing (and a little luck) are everything!

        No need to split $content up into separate lines first. Try something like:
        my $content = get($url); print $content =~ /^(.*abc.*\n)/m;
        (If you don't need $content for anything else, you can just put the get($url) in its place on the print line.)
Re: print the whole line
by borisz (Canon) on Jan 21, 2004 at 13:35 UTC
    your code does it already!?
    for ( @mydata ) { print if /abc/; }
    Boris
Re: print the whole line
by Roger (Parson) on Jan 21, 2004 at 13:47 UTC
    You code is working already.
    use strict; use warnings; use Data::Dumper; my @mydata = qw/ abcd cabc abcdabcd defg xyz /; for(@mydata) { if($_ =~ /abc/g) { print "$_\n"; #print the line } }
    And the output -
    abcd cabc abcdabcd

    I have a feeling that you want to do this with a one-liner instead? Which is already answered by BrowserUk.

Re: print the whole line
by Theo (Priest) on Jan 21, 2004 at 15:55 UTC
    I was just reading last night in Learning Perl about the special variables, $', $& and $`. The $& hold the contents of the m// string while the $` and $' contain the text before the m// and after it. I'm not sure which one does which, but is sounds like with these three variables, you would have the whole line.

    -Theo-
    (so many nodes and so little time ... )

      I believe these are notoriously inefficient and should be avoided. Allegedly using some of these causes them to be calculated for the whole program, slowing down every regex whether you need the variables or not. Sorry, I forgot the source where that info came from.

      The code posted above is functional for the task (it can be refactored a small bit), he just needs to load up @data differently.