#!/usr/bin/perl # this is a little bit bizarre regarding various books the writer the presser of the button
use strict;
use warnings;
use DB_File;
use Carp;
use 5.34.0;
use feature 'try';
local $SIG{__WARN__} = sub {
die;
}; # turns warnings into exceptions
unlink "wf4";
tie my %words, "DB_File", "wf4";
my ($filename, $howmanywords) = @ARGV;
$howmanywords ||= 5;

#open FILE, "<", $filename or croak $!;
open my $handle, '<', $filename or croak $!;
chomp(my @lines = <$handle>);
close $handle;

#my (%words, $total);
my $total;
my %hasharray;

#while (<FILE>) {
for my $row (@lines) {
	try {
#		print "row $row\n";
#		while ( $row =~ /(\w[\w'-]*)/g ) {
		my @parts = split ' ', $row;
#		print $parts[1], "\n";
#		$total++;
		$total += $parts[0];
#		$words{$parts[1]}->{f} += $parts[0]; # unless substr($1, 0, 1) =~ /[A-Z]/;
#		$words{$parts[1]}->{p} .= " ".$parts[2];
		$hasharray{$parts[1]}->{f} += $parts[0]; # unless substr($1, 0, 1) =~ /[A-Z]/;
		$hasharray{$parts[1]}->{p} .= $parts[2].' '; 				
=end			
			print "$1\n";
			$total++;
			$words{$2}->{f} += $1; # unless substr($1, 0, 1) =~ /[A-Z]/;
			$words{$2}->{p} .= " $3"; 	
=cut
#		}
#		print "finished\n";
	}
	catch ($e) {
		print "error row $row\n";
#		croak;		
	}
}

for my $key (keys(%hasharray)) {
	$hasharray{$key}->{p} =~ s/\s+$//;
	$words{$key} = join $;, $hasharray{$key}->{f}, $hasharray{$key}->{p};
}

print "$total\n";
#close FILE;
untie %words;
