in reply to Re^2: (Mis)Understanding <c>grep...eq $_<c>
in thread (Mis)Understanding grep...eq $_
Means take each item in @a and put it into the anon grep subroutine. If that subroutine returns true, then move that item to @b. Absent an explicit return statement, the return value of a Perl sub is the last statement. In this case it is "1", or always true. So this is the same as: @a=@b; as far as grep{} is concerned. This push statement gets executed but has no bearing upon the action of grep{}.#!/usr/bin/perl -w use strict; my @a = qw( a b c d ); my $i=0; my @b = grep { push(@a,'!') if !$i++; 1 } @a; print "@b\n"; print "@a\n"; __END__ output: a b c d a b c d !
So, YES grep{} is meant to process/filter lists! I would argue that putting this push statement inside the grep is a weird thing to do.
grep does have a scalar context also! This is useful in some cases, here this is just a "dumb" example.
But this can be used to say count number of matches in @a.my $num = grep{1}@a; print $num; ##prints 5 (num of elements in @a) ie, abcd!
Update:Perl will not allow the input list to be modified within the grep{} subroutine. This could create a circular endless loop and that just won't happen.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: (Mis)Understanding <c>grep...eq $_<c>
by ikegami (Patriarch) on Feb 25, 2009 at 13:10 UTC | |
by tye (Sage) on Feb 25, 2009 at 15:27 UTC | |
by ikegami (Patriarch) on Feb 25, 2009 at 16:03 UTC | |
by tye (Sage) on Feb 25, 2009 at 18:27 UTC | |
by ysth (Canon) on Mar 01, 2009 at 06:28 UTC | |
by Marshall (Canon) on Feb 26, 2009 at 18:59 UTC | |
by Marshall (Canon) on Feb 26, 2009 at 21:47 UTC |