in reply to reading/writing to a file
How about something like this? Ignores any punctuation except for internal apostrophes (contractions), works with any Unicode words, not just ASCII or Latin-1.
#! /usr/bin/perl use warnings; use strict; if(scalar(@ARGV) != 3){ die "Usage: $0 inputfile.txt excludefile.txt outputfile.txt\n" ; } my $word = qr/(?<!\p{Alnum})\p{Alnum}+(?!\p{Alnum})/; my %excluded; open my $exclude, '<', $ARGV[1] or die "Error opening exclude file: $! +\n"; while (my $line = <$exclude>) { while ($line =~ /($word('$word)?)/g) { undef $excluded{$1}; } } close $exclude; my %included; open my $input, '<', $ARGV[0] or die "Error opening input file: $!\n"; while (my $line = <$input>) { while ($line =~ /($word('$word)?)/g) { undef $included{$1} unless exists $excluded{$1}; } } close $input; open my $output, '>>', $ARGV[2] or die "Error opening output file: $!\ +n"; print $output join "\n", sort keys %included;
Update: Changed open for output to append instead of truncate, as pointed out by tlm
After looking at this and the OPs post again, I realized that I left out the check for word less than 4 letters and would still possibly (probably) end up with dupe words in the output file. This fixes those problems.
#! /usr/bin/perl use warnings; use strict; if(scalar(@ARGV) != 3){ die "Usage: $0 inputfile.txt excludefile.txt outputfile.txt\n" ; } my $word = qr/(?<!\p{Alnum})\p{Alnum}+(?!\p{Alnum})/; my %excluded; open my $exclude, '<', $ARGV[1] or die "Error opening exclude file: $! +\n"; while (my $line = <$exclude>) { while ($line =~ /($word('$word)?)/g) { undef $excluded{$1}; } } close $exclude; my %included; open my $input, '<', $ARGV[0] or die "Error opening input file: $!\n"; while (my $line = <$input>) { while ($line =~ /($word('$word)?)/g) { next if length $1 < 4; undef $included{$1} unless exists $excluded{$1}; } } close $input; %excluded = (); open my $output, '<', $ARGV[2] or die "Error opening dictionary file: +$!\n"; while (my $line = <$output>) { while ($line =~ /($word('$word)?)/g) { undef $included{$1}; } } close $output; open $output, '>', $ARGV[2] or die "Error opening output file: $!\n"; print $output join "\n", sort keys %included;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: reading/writing to a file
by tlm (Prior) on Jun 18, 2005 at 19:54 UTC |