Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: No such file or directory error

by jwkrahn (Monsignor)
on Jan 10, 2013 at 12:50 UTC ( #1012652=note: print w/ replies, xml ) Need Help??


in reply to No such file or directory error

You have a lot of duplication in your code so I cleaned it up a bit and removed some redundant code.

#!/usr/bin/perl use warnings; use strict; use List::Util qw/ max sum /; my %file_types = ( ENG => 'ENGLISH', AFR => 'AFRIKAANS', NDB => 'NDEBELE', SEP => 'SEPEDI', SIS => 'SISWATI', SOT => 'SOTHO', TSO => 'TSONGA', TSW => 'TSWANA', VEN => 'VENDA', XHO => 'XHOSA', ZUL => 'ZULU', ); my %words; for my $file ( <korpus*.txt> ) { $file =~ /\Akorpus([A-Z]{3})\.txt\z/ or next; my $type = $1; exists $file_types{ $type } or next; local $/; open my $FH, '<', $file or die "Cannot open '$file' because: $!"; @{ $words{ $type } }{ grep length, split /\n/, <$FH> } = (); } ############################################################# my $input; do { print "To classify a single file, press 1\nTo work within a direct +ory, press 2\n"; chomp( $input = <STDIN> ); } until $input =~ /\A[12]\z/; if ( $input == 1 ) { print "Please type the name of the file you wish to classify\n"; chomp( my $filename = <STDIN> ); folder( $filename ); } else { my $dir = 'C:\Users\ZB\Desktop\Text Files'; opendir my $DIR, $dir or die "cannot opendir $dir"; for my $file ( map "$dir/$_", readdir $DIR ) { next unless -f $file; folder( $file ); } } ###################################################################### +############################ sub folder { my ( $fname ) = @_; open my $FIN, '<', $fname or die "Cannot open '$fname' because: $! +"; my %hash; while ( <$FIN> ) { while ( /(\w+)/g ) { $hash{ lc $1 }++; } } ###################################################### my $total = sum values %hash; my @highest = ( '', 0 ); for my $type ( keys %file_types ) { my $percentile = sum( @hash{ keys %{ $words{ $type } } } ) / $ +total; @highest = ( $type, $percentile ) if $highest[ 1 ] < $percenti +le; } ########################################################## D +etermine lingo through percentiles print "The file '$fname' is $file_types{ $highest[ 0 ] }\n"; }


Comment on Re: No such file or directory error
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1012652]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2015-07-31 03:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls