Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: (Mis)Understanding <c>grep...eq $_<c>

by jethro (Monsignor)
on Feb 24, 2009 at 06:49 UTC ( [id://745915]=note: print w/replies, xml ) Need Help??


in reply to (Mis)Understanding grep...eq $_

grep doesn't search in strings, it searches arrays or lists. It makes no sense to use it on scalars. You give grep a list, and an operation to execute on each element of the list. For example:

my @list= (1,8,3,9,2); my @newlist= grep $_<5, @list; # @newlist would be (1,3,2);

grep is sort of a loop and what you did was create a loop that always loops only once. So the following two lines are practically equivalent

unless ( grep $person_has eq $_, $item ) { unless ( $person_has eq $item ) {

Replies are listed 'Best First'.
Re^2: (Mis)Understanding <c>grep...eq $_<c>
by ysth (Canon) on Feb 24, 2009 at 07:21 UTC

      Ooh, nice! That's eminently readable. Much more likely to survive a code review than aristotle's ex-bang-bang operator.

      Rather than

      my @part = ( 'http://example.net/app', ( 'admin' ) x!! $is_admin_link, ( $subsite ) x!! defined $subsite, $mode, ( $id ) x!! defined $id, ( $submode ) x!! defined $submode, );

      we would now have

      my @part = ( 'http://example.net/app', grep( $is_admin_link. 'admin' ), grep( defined($subsite), $subsite ), $mode, grep( defined($id), $id ), grep( defined($submode), $submode ), );

      I like it.

      • another intruder with the mooring in the heart of the Perl

        What is the difference?
Re^2: (Mis)Understanding <c>grep...eq $_<c>
by ikegami (Patriarch) on Feb 24, 2009 at 07:09 UTC

    [grep] searches arrays or lists.

    I don't think so. Do you have any reason to believe grep knows anything about arrays?

    >perl -le"@a = qw( a b c d ); @b = grep { push(@a,'!') if !$i++; 1 } @ +a; print @b" abcd

    Contrast with for which does:

    >perl -le"@a = qw( a b c d ); for (@a) { push(@a,'!') if !$i++; push @ +b, $_ } print @b" abcd!
      Grep could have been called "filter" in array context.
      Most common form is @output = grep{anon_sub_returning_True_False}@input;
      This code:
      #!/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 !
      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{}.

      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.

      my $num = grep{1}@a; print $num; ##prints 5 (num of elements in @a) ie, abcd!
      But this can be used to say count number of matches in @a.
      $num = grep {/some_regex/}@a;
      But of course most common use would be @a=grep{/regex/}@b; which moves any item from @b which matches regex to @a.

      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.

        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.

        Obviously processes/filters lists. Obviously a weird thing to do. Jethro said grep also works on arrays, not just lists, and I was demonstrating that it only works on lists.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://745915]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-19 06:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found