CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Difficult problem...

 



errodr
newbie

Jul 22, 2002, 10:30 PM

Post #1 of 5 (1793 views)
Difficult problem... Can't Post

I have a difficult problem, which I'm sure someone here will be

able to figure out quickly.

Problem:

I have two files (.txt) each file contains a list of page titles.

What I need to do is take File A (the Master list) remove from

it page titles that appear in File B (the a page title subset).

Basically I need to take the page titles that appear in File B

and remove those titles from File A.

I have tried this, and it simply spits out File A with no changes.

Code
 open(FILEB,"FileB.txt")||die; 
while(<FILEB>){
$file1hash{$_}=1;
}
close(FILEB);open(OUTPUTFILE,">new_report.txt")||die;open(FILEA,"FileA.txt")||die;
while(<FILEA>){
print OUTPUTFILE $_ unless defined($file1hash{$_});
}
close(FILEA);close(OUTPUTFILE);

Thanks a lot...


Jean
User


Jul 22, 2002, 11:41 PM

Post #2 of 5 (1790 views)
Re: [errodr] Difficult problem... [In reply to] Can't Post

Your code in general seems fine to me...

The only problem I can possibly think of is hash redefinition.
I can not stress enough the importance of use strict;

Try adding my %file1hash outside (before) the while block...
As I see it now your hash is local to the while block and then it is declared again inside the next while. Hopefully I'm not misdirecting you...


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


jryan
User

Jul 23, 2002, 2:51 PM

Post #3 of 5 (1781 views)
Re: [errodr] Difficult problem... [In reply to] Can't Post

You need to use exists instead of defined. Defined works on the variable itself; since %file1hash has elements, it will always be defined. Exists checks for the existence of a key within the hash, which is what you want.


Paul
Enthusiast

Jul 23, 2002, 4:44 PM

Post #4 of 5 (1779 views)
Re: [jryan] Difficult problem... [In reply to] Can't Post

>>
You need to use exists instead of defined. Defined works on the variable itself; since %file1hash has elements, it will always be defined. Exists checks for the existence of a key within the hash, which is what you want.
<<

defined in the context used above checks whether the hash value is "defined", exists checks whether the hash key "exists".

>>
since %file1hash has elements, it will always be defined
<<

No it won't. Consider:

$hash{Test1} = 1;
$hash{Test2} = undef;
print defined $hash{Test2} ? 1 : 0;

That will print 0 as the Test2 key has an undefined value, yet %hash still has "elements".


(This post was edited by RedRum on Jul 23, 2002, 4:45 PM)


errodr
newbie

Jul 30, 2002, 11:39 AM

Post #5 of 5 (1763 views)
Re: [errodr] Difficult problem... [In reply to] Can't Post

I found my error...

.

.

.

The Data...



I stripped the spaces and newlines, and it worked perfectly.

Thanks...

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives