Multiple choice technology programming languages

Code 1: Code: my @lines = grep /^hi/, @allLines; Code 2: Code: my @lines = (); foreach (@allLines) { push (@lines,$_) if (/^hi/); } What is the difference between the two snippets (Code 1 and Code 2) above?

  1. In code 1, the operation occurs in one command; code 2 iterates each element in the array for matches.

  2. In code 1, the elements of @lines are the indexes in @allLines, in which matches were found.

  3. In code 1, the elements of "@lines" begin with the index in "@allLines", in which the match was found.

  4. In code 1, "@lines" may not necessarily be in the same order; the original lines appear in "@allLines".

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

In grep, when used in scalar context (each element tested individually), it returns the index where the match was found, not the actual matched value. This is why Code 1's @lines would contain indexes (with matching values prepended), while Code 2 explicitly pushes the matching lines themselves into @lines.