Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Why is grep not searching for contents?

by Anonymous Monk
on Apr 30, 2011 at 02:49 UTC ( [id://902112]=perlquestion: print w/replies, xml ) Need Help??

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

Why is the below grep failing?It fails to search the contents of oem.h,what should I change to search for contents?

#!/usr/bin/perl -w use strict; use warnings; use File::Find; use File::Find; my $folder= "oem"; my @array="./oem.h"; my @content; @content = grep {/\Q$folder\E/} @array; print "@content";#prints ./oem.h instead of the matched content

Replies are listed 'Best First'.
Re: Why is grep not searching for contents?
by wind (Priest) on Apr 30, 2011 at 03:09 UTC

    You need to actually open the file for parsing.

    #!/usr/bin/perl -w use strict; use warnings; my $folder= "oem"; my $file = "./oem.h"; open my $fh, $file or die $!; my @array = <$fh>; close $fh; my @content = grep {/\Q$folder\E/} @array; print "@content";

      I just gave a sample example,I actually have an array with lot of files,how can open a file handle for such situation

      my @files;#I need to have an array that has a file handle for each fil +e my $folder="oem";
        When learning the basics, you might try looking closely at just one thing at a time. Rather than having your whole script fall apart, look at what just one call or construct is doing. Understand what it does and how it should be used. Then look at the larger context it is being used in. Try printing the arguments (use Data::Dumper if they are not simple) to make sure you are feeding it what you intended. Make a miniature test calling just that one thing with the parameters ready to go. If that's OK, then shift your focus to how those values are being prepared for that use.

        Never mind,just added a for loop

    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-03-28 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found