Assist with code.

Reply

Join Date: Jun 2009
Posts: 8
Reputation: rayken1 is an unknown quantity at this point 
Solved Threads: 0
rayken1 rayken1 is offline Offline
Newbie Poster

Assist with code.

 
0
  #1
Jun 24th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Assist with code.

 
0
  #2
Jun 24th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,782
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 113
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: Assist with code.

 
0
  #3
Jun 24th, 2009
if exists $fileHash{$_} then print
not the other way.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 49
Reputation: onaclov2000 is an unknown quantity at this point 
Solved Threads: 5
onaclov2000 onaclov2000 is offline Offline
Light Poster

Re: Assist with code.

 
0
  #4
Jun 26th, 2009
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....
Last edited by onaclov2000; Jun 26th, 2009 at 11:17 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC