jerrygarciuh has asked for the wisdom of the Perl Monks concerning the following question:

In the Llama merlyn uses this to illustrate a grep command:
grep abc somefile >results
Llama says this should search the file and pipe (?right term?) the results to a file so named. I tried to construct my own from the model like so:
#!/usr/local/bin/perl -w use strict; grep abc menu.txt >results.txt;

But I get this error:Not enough arguments for grep
Can someone explain what is wrong? I looked at the definition in perlfunc and I see a whole other construct...
TIA
jg

Ain't no time to hate! Barely time to wait! ~RH

Replies are listed 'Best First'.
Re: Getting a grip on codegrep/code
by Amoe (Friar) on Sep 26, 2001 at 19:49 UTC
    What's happening I think is that merlyn was giving you an example of the usage of the UNIX system command grep. grep() in Perl is different: the syntax is: grep PATTERN, LIST or grep BLOCK LIST You'll have to write the returned array to file yourself, because grep() doesn't support redirection (as does any Perl function...)

    --
    my @words = grep({ref} @clever);
    sub version { print "I cuss your capitalist pig tendencies bad!\n" }
      grep() is not solely a regex tool. It actually takes ANY expression you give it:
      # grep EXPR, LIST @biggish = grep $_ > 10, @numbers;

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

        Cool, I didn't know that but it makes sense. Thanks.

        --
        my @words = grep({ref} @clever);
        sub version { print "I cuss your capitalist pig tendencies bad!\n" }
Re: Getting a grip on codegrep/code
by tachyon (Chancellor) on Sep 26, 2001 at 20:04 UTC

    That is command line shell syntax. It should work on unix if you enclose it in backticks `.....`.

    Here is the equivalent in perl (using perl's grep or a loop):

    #!/usr/bin/perl -w use strict; my $file = 'c:/test.pl'; my @file = get_file($file); my @results = grep { /my/ } @file; # the grep is equivalnet to this code # foreach $line (@file) { # push @results, $line if $line =~ m/my/; # } set_file( 'c:/results.txt', @results ); exit; sub set_file { my $file = shift; open FILE, ">$file" or die "Oops can't write $file $!\n"; print FILE @_; close FILE; } sub get_file { my $file = shift; open FILE, $file or "Oops can't read $file: $!\n"; my @content = <FILE>; close FILE; return @content; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print