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

Hi, the following code
use List::Compare; use strict; open (GEN, "general.txt")||die("general.txt File cannot open\n"); open (SEA, "search.txt")||die("search.txt File cannot open\n"); my @gen=<GEN>; my @sea=<SEA>; for $a ($gen) { @result = grep/^\Q$a\E$/, @sea; push (@final , @result); } open(OUT, ">textCompare3Output.txt")||die("cannot create\n"); print OUT "\nSearch string that matches against general data:\t@final" +;
gives back the error message:
Global symbol "$gen" requires explicit package name at textCompare3.pl + line 8. Global symbol "@result" requires explicit package name at textCompare3 +.pl line 10. Global symbol "@final" requires explicit package name at textCompare3. +pl line 11. Global symbol "@result" requires explicit package name at textCompare3 +.pl line 11. Global symbol "@final" requires explicit package name at textCompare3. +pl line 15. Execution of textCompare3.pl aborted due to compilation errors.

Replies are listed 'Best First'.
Re: error message txtFile comparison with output in another txtfile
by BaldPenguin (Friar) on Jul 14, 2005 at 22:03 UTC
    You don't have a $gen declared only an array, @result and @final are not declared, try:
    use List::Compare; use strict; open (GEN, "general.txt")||die("general.txt File cannot open\n"); open (SEA, "search.txt")||die("search.txt File cannot open\n"); my @gen=<GEN>; my @sea=<SEA>; my @final = (); for $a (@gen) { my @result = grep/^\Q$a\E$/, @sea; push (@final , @result); } open(OUT, ">textCompare3Output.txt")||die("cannot create\n"); print OUT "\nSearch string that matches against general data:\t@final" +;

    Don
    WHITEPAGES.COM | INC
    Everything I've learned in life can be summed up in a small perl script!

      The variable in the foreach loop should also be properly declared, and preferably not called $a.

      for my $pattern (@gen) { my @result = grep/^\Q$pattern\E$/, @sea; push (@final , @result); }
      the code has no errors now, but the comparison doesnt work properly, there is no output in the new textfile except 'Search string that matches against general data:' Even if I have two same entries in 'search.txt' and 'general.txt', there is still no output. Perhaps the @final variable does not work properly? Jurgen
Re: error message txtFile comparison with output in another txtfile
by GrandFather (Saint) on Jul 14, 2005 at 22:10 UTC

    Global symbol "..." requires explicit package name is telling you that ... is undefined. For $gen that is because it should be @gen. For the others you need a

    my ...;

    in each case.


    Perl is Huffman encoded by design.
      Thanks, but there is (still) no output in the new produced textfile (see also my upper message) although 'general.txt' and 'search.txt' have both the same content. Jurgen

        You should provide sample data (file contents) in a <readmore>...</readmore> block in your original post so that others can reproduce your problem.


        Perl is Huffman encoded by design.

        and now that the "compile" errors are fixed you should state your new problem: what do you expect to see and what do you actually see.

        For test purposes it would be better to print to STDOUT rather than to a file.


        Perl is Huffman encoded by design.