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

open(FILE, "<", "numb.txt" ) || die "Unable to open numb.txt <$!>\n"; while ( <FILE> ) { chomp; $fileHash{$_} = $i++; } close(FILE); open(FILE, "<", "num.txt" ) || die "Unable to open num.txt<$!>\n"; while( <FILE> ) { chomp; if( exists $fileHash{$_} ) { } else { print "$_\n"; } } close(FILE); Kindly help me with the code that will enable me add or append the string which is chosen to be different to the second file that us opened in this script. Thanks.

Replies are listed 'Best First'.
Re: Assist with code
by Corion (Patriarch) on Jun 24, 2009 at 07:02 UTC

    Maybe see open? I'm sorry, but I can't help you further because I don't understand the problem you have. If you want to open more than one file handle at the same time, use lexical filehandles:

    open my $file1, '<', 'numb.txt' or die "numb.txt: $!"; open my $file2, '<', 'numb.txt' or die "num.txt: $!"; ...
Re: Assist with code
by si_lence (Deacon) on Jun 24, 2009 at 07:14 UTC
    Hi

    What are you trying to do? From your code you just read in both files and print the lines in the second one that are not present in the first. But you talk about "add or append" and I don't see where you want to add or append the lines to.

    Also if you just want to keep track of the lines already seen in the first file you don't need to use $i++ in the assigment to the hash, you can just put the value to a defined non-zero one (like '1').

    It also helps readability if you use <code> .. </code> tags.

    Cheers

    si_lence