Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

using grep to search an array

by alicatserver (Initiate)
on May 23, 2011 at 20:05 UTC ( [id://906354]=perlquestion: print w/replies, xml ) Need Help??

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

I need to search fileB with fileA values. unfortunately, I cant get grep to work. Can anyone help.
fileA has 7 char numbers
fileB has 7 char plus some text

$data_file="FILEA.TXT";
open(DAT, $data_file) || die("Could not open file!");
@raw_data=<DAT>;
close(DAT);

$fileb="FILEB.TXT";
open(DAT2, $fileb) || die("Could not open file!");
@fileb=<DAT2>;
close(DAT2);

foreach (@raw_data)
{
$testit=$_;
@grepnames = grep/^$testit/,@fileb;
}

print "@grepnames\n\n";

Replies are listed 'Best First'.
Re: using grep to search an array
by toolic (Bishop) on May 23, 2011 at 20:15 UTC
Re: using grep to search an array
by John M. Dlugosz (Monsignor) on May 23, 2011 at 20:14 UTC
    Are you looking for lines exactly the same or just prefixes? If the latter, the "\n" at the end of $testit will get in the way.

    In any case, you are overwriting @grepnames each time through the loop, so you will only return the matches from the last line of FILEA.

    Try push @grepnames, grep... instead.

    And use strict and warnings!

      Here's the data that I should have added earlier...
      Also, the goal is to print the matching 7 digit line. If I found 7770005 I would get the full line from fileB.
      (And I change the grep line to a push)
      push(@grepnames,grep/$testit/,@fileb);

      fileA:
      7770005
      7770012

      fileB:
      7770001 ABA BANKING JOURNAL.RECORD # = b12822826. OK
      7770002 THE ABNF JOURNAL.RECORD # = b13671558. ok
      7770003 ACADEMIC EXCHANGE QUARTERLY : AEQ.RECORD # = b14618114. ok
      7770004 THE ACADEMY OF MANAGEMENT REVIEW.RECORD # = b12691070. ok
      7770005 THE ACCOUNTING REVIEW.RECORD # = b13123269. ok
      7770006 "ACCOUNTING, ORGANIZATIONS AND SOCIETY.RECORD " # = b15123236. ok
      7770007 "ACTA CRYSTALLOGRAPHICA. SECTION A: CRYSTAL PHYSICS, DIFFRACTION,
      7770008 ADAPTED PHYSICAL ACTIVITY QUARTERLY : APAQ.RECORD # = b13274272. ok
      7770009 ADMINISTRATION IN SOCIAL WORK.RECORD # = b12754225. ok
      7770010 ADMINISTRATIVE SCIENCE QUARTERLY.RECORD # = b13123415.
      7770011 ADOLESCENCE.RECORD # = b13171288. ok
      7770012 ADVANCES IN NURSING SCIENCE.RECORD # = b12795896. ok
        OK, so it is supposed to be a prefix. That means the \n at the end of $testit is getting in the way of letting it match.

        Use chomp inside the loop.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 21:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found