Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Perls <code>grep</code> function selects/filters all elements from a list that match a certain condition. In your case, you selected all elements that match the regex <code>/Number/</code> from the <code>@input_file</code> array.</p> <p>To select the non-whitespace string after <code>Number</code> use this Regex:</p> <pre><code>my $regex = qr{ Number # Match the literal string 'Number' \s+ # match any number of whitespace characters ([^\s;]+) # Capture the following non-spaces-or-semicolons into $1 # using a negated character class }x; # use /x modifier to allow whitespaces in pattern # for better formatting </code></pre> <p>My suggestion would be to loop directly over the input file handle:</p> <pre><code>while(defined(my $line = &lt;$input&gt;)) { $line =~ /$regex/; print "Found: $1" if length $1; # skip if nothing was found } </code></pre> <p>If you have to use an array, a <code>foreach</code>-loop would be preferable:</p> <pre><code>foreach my $line (@input_lines) { $line =~ /$regex/; print "Found: $1" if length $1; # skip if nothing was found } </code></pre> <p>If you don't want to print your matches directly but to store them in an array, <code>push</code> the values into the array inside your loop (both work) or use the <code>map</code> function. The map function replaces each input element by the value of the specified operation:</p> <pre><code>my @result = map {/$regex/; length $1 ? $1 : ()} @input_file; </code></pre> <p>or</p> <pre><code>my @result = map {/$regex/; length $1 ? $1 : ()} &lt;$input&gt;; </code></pre> <p>Inside the <code>map</code> block, we match the regex against the current array element. If we have a match, we return <code>$1</code>, else we return an <em>empty list</em>. This gets flattened into invisibility so we don't create an entry in <code>@result</code>. This is different form returning <code>undef</code>, what would create an undef element in your array.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload