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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.