Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Grep arrays

by nickawesome07 (Initiate)
on Feb 22, 2013 at 18:40 UTC ( [id://1020202]=perlquestion: print w/replies, xml ) Need Help??

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

I am having an issue with using the input from an array(file) to grep through another array(file). Heres what I have so far. Thanks in advance. I am attempting to use the 'list' as the criteria to grep through file.csv and put each successful find into a report. Thanks in advance.
open(MYINPUTFILE, "<list"); foreach(<MYINPUTFILE>) { my $out = `grep -i "$_" /home/file.csv`; open(OUTPUT, ">>/tmp/file.report"); print OUTPUT "$out\n"; } close(MYINPUTFILE); close(OUTPUT);
Dave thank you for the fast reply, Essentially the list size is 567 lines, the file.csv size is 6436. When the code is run the output is 3.6 million lines. Thanks in advance. The "list" are email addresses, and the file.csv is a list of email addresses. So there can only be 567 matches. Thanks in advance.

Replies are listed 'Best First'.
Re: Grep arrays
by davido (Cardinal) on Feb 22, 2013 at 18:51 UTC

    One issue is opening the output file inside the loop and closing it outside the loop. Move the 'open' to before the loop. Also at the top of your script place the following line:  use autodie;.

    But to give an effective answer to your question we would need to know how the code you posted is failing, as well as see a brief sample of the input files. Phrasing it in the form of a question may add clarity as well.


    Dave

Re: Grep arrays
by CountZero (Bishop) on Feb 22, 2013 at 20:50 UTC
    Of course we have not yet seen the formats of your input files, but I'd do something along the following lines:
    use Modern::Perl; use Regexp::Assemble; # first we make one big regex. my $ra = Regexp::Assemble->new; $ra->add(<DATA>); say $ra->as_string; # to see how the regex looks my $regexp = $ra->re; open my $HOMEFILE, '<', '/home/file.csv'; open my $OUTPUT, '>', '/tmp/file.report'; while (<$HOMEFILE>) { /$regexp/ and say $OUTPUT "Something in [$_] matched"; } __DATA__ my.email@somewhere.com your.postbox@anywhere.net our.address@dropithere.org me.andmyself@sendit.com
    Of course you will need to add some dies to check all your files opened properly. or use autodie to save you the hassle.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Grep arrays
by Anonymous Monk on Feb 22, 2013 at 19:58 UTC

    And the problem is?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 08:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found