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);

Hi all, kindly assist me with the above code so that the selected string is added of written (output) to the second file being opened in this script.

Thanks.

Recommended Answers

All 3 Replies

You need to clarify your requirements better. Right now you are looping through num.txt and populating a hash with the lines of the file and the count of how many times each line is seen in the file. From there what do you want to do? See if the same line is in a different file? Otherwise your code makes no sense because you open the same file and then look to see if the lines are in the same file, which of course they are.

if exists $fileHash{$_} then print
not the other way.

Member Avatar for onaclov2000

First off I would recommend doing this to get all the text (in my opinion) if you dont' have a large file anyways:
open(FILE, "<", "numb.txt" ) || die "Unable to open numb.txt <$!>\n";

@inputFile = <FILE>;

foreach $line (@inputFile)
{

}

What that is doing is opening the file, then sticking each line in the @inputFile array, from there you are going to "loop" through using a for loop,

Now simply set your requirements (if then statement) for the particular line, I suppose you could simply do a "double for loop", as in stick a for loop inside, that for loop and do a compare of $line with whatever other "line" you call, and if they match then you can simply set a flag to print it to the output file,
Don't forget to print to your output file you would need to add FILE to your print statement:

print FILE "whatever your wanting to print";

If that doesn't help just let me know,

I have a few posts (older) on my blog with a couple perl examples, but you can use that code to look at how to get the lines out of a file if you want as well....

onaclov2000.blogspot.com

Then again I might be completely missing the point of your post....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.