"King, Jason G" wrote:
> 
> Alan writes..
> 
> >"Kenneth Jideofor [ MTN - Ikoyi ]" wrote:
> >>
> >> Hi folks,
> >>
> >> I have two files, FILE1 and FILE2. Each of the two files is an array
> >> of numbers. I want to check for the occurrence of each element of
> >> FILE1 in FILE2. In other words, FILE2 is checked for the occurrence
> >> of each of the elements in of FILE1. For any element of FILE1 that
> >> occurs in FILE2, this element is saved in a file, FILE3.txt.
> >>
> >> I tried the attached Perl script to perform this exercise but the
> >> script failed with the following error message:
> >>
> >> "main:database" used only once: possible typo at ./GREP1.pl line 8"
> >>
> >> Kindly assist me with a solution to actualising this task.
> >
> >try this:
> >
> >#!/usr/bin/perl -w
> >
> >open FILE1, "</tmp/AAA.txt" or die "Can,t open AAA.txt: $!";
> >open FILE2, "</tmp/DATA.dat" or die "Can't open DATA.dat: $!";
> >open OUT, ">>/tmp/FILE3.txt" or die "Can't open BBB.txt: $!";
> >
> >my @numbers = <FILE1>;
> >my @database = <FILE2>;
> >close(FILE1);
> >close(FILE2);
> >
> >foreach $number (@numbers) {
> >                chomp($number);
> >                print OUT grep {/$number/} @database;
> >}
> >close(OUT);
> 
> What if FILE1 contains the number 1 and FILE2 does not contain 1 but
> contains 11 then the regex will match against 11 and print '1' out into
> FILE3 but the number 1 was not in FILE2 at all.
> 
> Additionally your syntax:
> 
>   print OUT grep {/$number/} @database;
> 
> Will mean that your output will print out multiple entries into FILE3
> for each iteration, eg if FILE1 contained:
> 
>   1
>   2
>   3
> 
> And FILE2 contained:
> 
>   11
>   12
>   13
>   14
>   21
>   44
>   22
> 
> Then your output would be as follows (I've added the spaces in for
> readability - in fact there would be no spaces or newlines in your
> output):
> 
>   11 12 13 14 21 12 21 22 13
>   ^^^^^^^^^^^
>   First pass
>               ^^^^^^^^^^^
>               Second pass
>                           ^^
>                           Third pass
> 
> At the very least you'd have to do {/^$number$/} but that's MUCH less
> efficient than doing { $_ == $number } - perl strips off the \n itself
> when converting $_ to a number. But my preference is to use the hash
> lookup detailed in my other post.

well, right, but since he didn't post his data, which would have
illuminated the situation, I assumed rather larger numbers, all 
of the same length, which seemed to be more like what he was talking
about. TMTOWTDI, but my suggestion was closer to his code.

BTW, your solution has the two files reversed:

> I want to check for the occurrence of each element of
> FILE1 in FILE2. In other words, FILE2 is checked for the occurrence
> of each of the elements in of FILE1.

FILE1 == your IN and FILE2 == your DB.

-- 
Alan F. Dickey - Interaction and Realization
http://www.intac.com/~afdickey
mailto:[EMAIL PROTECTED]
VOX: 908-273-3232 Cell: 908-334-0932

_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to