Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

A Problem With Hashes and Keys

by bivouac (Beadle)
on Jul 19, 2004 at 13:45 UTC ( [id://375547]=perlquestion: print w/replies, xml ) Need Help??

bivouac has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks - I'm having a problem with a another problem that I experienced before. I have three different text files. One is a master file, one is a delete file and another is a add file. I need to remove the contents of the deleted file from the master file and add the contents of the add file to the new master file. I figured this would be fairly trivial given what I learned from my previous problem and yet I can't get this to work.
#!/usr/bin/perl -w use strict; my $dir = "/foo/"; my $raw_file = $dir."sample_qw_zips.txt"; my $delete_file = $dir."sample_qw_deletes.txt"; my $add_file = $dir."dsl_capable_add_0615.txt"; my %delete = (); open (DELETE_FILE, "< $delete_file") or die "Can't open $delete_file: $!\n"; open (RAW_FILE, "< $raw_file") or die "Can't open $raw_file: $!\n"; open (OUTPUT, "> output.txt") or die "Can't open OUTPUT: $!\n"; #populate delete hash while (my $deleted_zip = <DELETE_FILE>) { chomp $deleted_zip; $delete{$deleted_zip} = 1; } while (my $raw_zip = <RAW_FILE>) { chomp $raw_zip; # strings in a weird place in a weird place my $zip = substr($raw_zip, 3, 9); # maybe use an unless statement if (exists $delete{$zip}) { # don't do anything } else { print OUTPUT $raw_zip, "\n"; } } close DELETE_FILE; close RAW_FILE; close OUTPUT;
You'd think after all these years I would be more comfrtable with hashes. Nope. Thanks.

Replies are listed 'Best First'.
Re: A Problem With Hashes and Keys
by Grygonos (Chaplain) on Jul 19, 2004 at 14:24 UTC

    You want something like Tie::File. This would allow you to edit the master file in place (unless that's a problem) Also it seems like you only need an array, as there is no association to be made (ie all it seems you are populating is that a hash key hashes to one when it needs to be deleted. You would be better off doing something like this

    open(DELETE,$file) or die "$!"; my @delete = <DELETE>; chomp for(@delete); close(DELETE); open(MASTER,$file) or die "$!"; open(OUTPUT,$file) or die "$!"; while(<MASTER>) { my $zip = substr($_, 3, 9); if(!deletable(\$zip,\@delete)) { print OUTPUT, $zip; } } close(OUTPUT); close(MATSER); sub deletable { my ($item,$list) = @_; my $rv = 0; foreach(@{$item}) { if($$item eq $_) { $rv = 1; last; } } return $rv; }
    that's untested but its how i see it working best...Tie::File is a good option.. but if you're wanting to keep the same format.. this should work. This is all assuming that your delete file isn't ginormous.

Re: A Problem With Hashes and Keys
by eXile (Priest) on Jul 19, 2004 at 14:11 UTC
    your code works pretty well on my machine. I used the next filecontent for a test:
    input:
    CA 92037 CA 92021 CA 92055 CA 92037
    delete:
    92037 92055
    And this gave me the output:
    CA 92021
    Maybe something wrong with your input-file format?
      Is it possible that my substr call is hosing everything up? When I removed that from the code that seemed to work great - but by removing the substr call I need to adjust the incoming data which isn't a good thing. Still confused as heck.
        Just make shure that your substr produces the same values what are stored in DELETE_FILE
Re: A Problem With Hashes and Keys
by meredith (Friar) on Jul 19, 2004 at 14:16 UTC

    I'm really not sure if you asked a question or not. =) I noticed that you don't appear to be using your $add_file at all in the code, too.

    Anyway, a technique that seems to be more sane to me is one where you:

    1. Read/parse $raw_zip, and populate a %master hash.
    2. In a while loop reading from $delete_file, check if the key (in %master) for each line exists, and delete that key.
    3. Do similar for $add_file, adding entries to %master as requested.
    4. Finally, walk back over the %master hash, and output back to your original file.
    How does that sound to you?

    Update: I saw that file format for the "master" file. I would populate it like so, if you do something like I said above:

    my ($state, $zip) = split; $master{$zip} = $state;
    I am assuming that there won't be any duplicate lines in your file, also.

    mhoward - at - hattmoward.org

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://375547]
Approved by Grygonos
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (11)
As of 2024-03-28 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found