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

Why can't I do this ??
open(FILE1,"/tmp/file1"); open(FILE2,"/tmp/file2"); while(<FILE1>){ $var = $_; while(<FILE2>){ #compare $var against new $_ if($var eq $_){ print "$_ matched!\n"} } }
tn%
amonaco@devel.unixstate.com

Edit: 2001-03-02 by neshura

Replies are listed 'Best First'.
Re: Double While!
by zigster (Hermit) on Mar 01, 2001 at 22:16 UTC
    while () will loop forever and will not set $_ you could update your code to actually parse the file using the operator <FILE1> in the while clause:
    open FHAND,"filename"; while (<FHAND>) { $var = $_; }
    The problem is if once you have run through a file <> returns EOF to indicate the file is complete (it is that return that jumps out of the loop). Placing a while like this inside a loop will mean there fore that the inner loop will only work once. A solution would be to close and reopen the file this is going to be expensive. The best way is to read the file into memory and compair in memory
    open FHAND1,"filename1"; open FHAND2,"filename2" @fileData1 = <FHAND1>; @fileData2 = <FHAND2>;
    now compair the arrays fileData1 and fileData2.
    --

    Zigster
Re: Double While!
by mirod (Canon) on Mar 01, 2001 at 22:20 UTC

    Because you can only have one $_. But if I understand what you want to do you can try this:

    open(KEYWORDS,"/tmp/file1") or die "cannot open /tmp/file1: $!"; my %keywords= map { ($_, 1) } <KEYWORDS>; # create a hash keyword => +1 open(STUFF_TO_MATCH,"/tmp/file2"); or die "cannot open /tmp/file2: $!"; while(<STUFF_TO_MATCH>) { print "$_ matched!\n" if($keyword{$_}); }
      I recommend this solution.

      A common error made by both beginners and experienced programmers alike is to compare arrays by, for each element in the one array, search the other array. This is OK for small amounts of data, but if the arrays have a thousand elements apiece, this is a million comparisons! (It scales quadratically.)

      This is why Perl has hashes as a native data type, to make it easy to write efficient algorithms like Mirod did above. Basically whenever you find yourself thinking, "search" think about whether you can use a hash lookup instead. You cannot always, but whenever you can you will be rewarded with much faster code. (And in Perl it is often shorter as well.)

Less whiles more wiles :^)
by frankus (Priest) on Mar 15, 2001 at 16:54 UTC
    This is what you are doing:

    If we take 2 files like so:
    /tmp/file1
    1
    2
    3
    4
    
    /tmp/file2
    a
    b
    c
    d
    e
    
    
    We'd get these results using your code:
    outer loop $_= 1
    inner loop $_= a
    inner loop $_= b
    inner loop $_= c
    inner loop $_= d
    inner loop $_= e
    outer loop $_= 2
    outer loop $_= 3
    outer loop $_= 4
    
    What we see is that the inner loop isn't working... care to know why? You need to open and close the inner file. Otherwise file2 is not set to the start again after each search, it will be EOF for subsequent loops. The $_ are fine,

    I would recommend using foreach rather than while, and if the files are small enough why not load the files into arrays and drive the comparison off them? If the files are of a size and the computer you're using is of a low power, think about using a sorted file and a binary chop and index the search file, just a thought... The O'Reilly Wolf book or the Perl Cookbook are great places to look for examples.

    Hope this helps...

    --
    
    Brother Frankus