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: Beginner: Re: [TJC] Loading words from one .txt file, searching another for these words and printing the frequency: Edit Log



zapzap
User

Jan 15, 2014, 5:00 AM


Views: 3052
Re: [TJC] Loading words from one .txt file, searching another for these words and printing the frequency

Your approach looks pretty good.


Code
#!/usr/bin/perl 
use strict;
use warnings;

open my $in, '<','input.txt' or die "Unable to open input file: $!\n";
open my $out, '>', 'frequency.txt' or die "Unable to create file for writing: $!\n";
open my $titles, '<', 'titles.txt' or die "Unable to open titles file: $!\n";

my @Titles;
# Search through titles file for titles and store in titles array
# We know every title starts with '<' so just search for that with
# a regex, perl is awesome for these situations.
while ( <$titles> ) {
chomp;
push(@Titles,$1) if /^>([[:alpha:]]+)/;
}

# So now we have our titles array created
# Now we can proceed to search through the input file and look
# for either one of the titles. But first we need to create
# a regular expression of the titles to search for in the input file
# I'm going to use your approach to create the string
# title1|title2|title3 for the regex using the join function with
# the '|' as the delimiter
my $str = join('|',@Titles);
my $re = qr/$str/; # I'm not sure if this line is necessary?

my %count;

# Loop through input file
while ( <$in> ) {
chomp;
my @words = split(/ /,$_);
for my $word (@words) {
$count{$word}++ if $word =~ $re;
}
}

for my $key ( sort { $a cmp $b } keys %count ) {
print $out "$key occurs $count{$key} times\n";
}

close $in;
close $out;
close $titles;


Personally, I wouldn't do anything like that.
I would try something like this. I'm sure there are others that can do better and I wouldn't mind witnessing a different approach.



Code
#!/usr/bin/perl 
use strict;
use warnings;

open my $in, '<','input.txt' or die "Unable to open input file: $!\n";
open my $out, '>', 'frequency.txt' or die "Unable to create file for writing: $!\n";
open my $titles, '<', 'titles.txt' or die "Unable to open titles file: $!\n";

my %count;
my $str = join('|',map{ /^>([[:alpha:]]+)/ } <$titles>);
while(<$in>) {
chomp;
for(split(/ /)) {
$count{$_}++ if /$str/;
}
}

print $out map { "$_ occurs $count{$_} times\n" } sort { $a cmp $b } keys %count;
close $in;
close $out
close $titles;


I hope this helps, if it doesn't, I apologize.
zap

P.S. One small note, I had difficulty understanding your goal because the counts for the fruits are off. There are 2 Grapes.


(This post was edited by zapzap on Jan 15, 2014, 5:03 AM)


Edit Log:
Post edited by zapzap (User) on Jan 15, 2014, 5:03 AM


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

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