Yes, it works this way, but you can improve it.

As a rule of thumb, use as small scopes as possible for variable declarartion, e.g. $pattern isn't used outside the foreach-loop (especially since $pattern is an alias to the current element of @compiled), and so it's useless to declare it "globally"

The same procedure with $line (although line is no alias, and the last content of line would stay in $line after the while block); if you write while( my $line = <> ) {, $line would only be usable within the while-loop-block, and you don't need to care if there is another $line somewhere outside the block which value is changed after the while loop)

use strict; use warnings; my $number = shift; # first usage my @regexp = (); # initialize before first usage, my $regexp[$_] wou +ldn't work $regexp[$_] = shift foreach (0..$number-1); my @compiled = map qr/$_/, @regexp; # first usage while( my $line = <> ) { # only in block foreach my $pattern (@compiled) { # only in block if( $line =~ /$pattern/ ) { print $line; last; } } }

or the like

And I guess you can write the shift foreach-line in a better readable way:

my $number = shift( @ARGV ); my @regexp = @ARGV[ 0..$number-1 ];

Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"


In reply to Re^2: YARQ: multi_grep example in perlretut by strat
in thread YARQ: multi_grep example in perlretut by carcassonne

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.