Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 this code so that I can append or write (output) the string that is selected onto the second file.
Thanks.

You have opened the second file for read. You need to open for read / write.
http://www.perlfect.com/articles/pe...
You should name to two file handles differently to avoid confusion.
To append to a file you have open for write, you can do something like
open FILE, ">filename.txt" or die $!;
print FILE "String" ;
close FILE;

To append to an existing file you need to open it in append mode, which is '>>'. If you use '>', that will overwrite the file, which means you'll loos any data that was already in the file.
Here's some comments on the code.
You're using the 3 arg form of open, which is good, but you should also be using a lexical var for the filehandle instead of the bareword and my personal preference is to use the lower precedence or instead of ||.
open my $FILE, '>>', 'num.txt' or die "Unable to open num.txt <$!>\n";Having an if block that does nothing is pointless. Just reverse the logic and print.
while( <$FILE> ) { chomp; print "$_\n" if not exists $fileHash{$_}; }

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |