| | |
Assist with code.
![]() |
•
•
Join Date: Jun 2009
Posts: 8
Reputation:
Solved Threads: 0
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.
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.
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.
not the other way.
•
•
Join Date: Jun 2008
Posts: 49
Reputation:
Solved Threads: 5
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....
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.
![]() |
Similar Threads
- Parsing html form. (PHP)
- Can somebody please help me write this code (C++)
- Changing color of cell in datagridview based on a condition.. CODE not working (VB.NET)
- Code for moving (C++)
- Permutation (C++)
- Problem with do while loop (C)
- connecting and adding new records (VB.NET)
- Run-Time Error 424 (Visual Basic 4 / 5 / 6)
Other Threads in the Perl Forum
- Previous Thread: Problem with ORDER BY LIMIT in MySQL 4.1.22
- Next Thread: simple scripting question, CGI log
| Thread Tools | Search this Thread |






