Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
#!/usr/bin/perl -w use strict; print "Enter a filename to analyze: "; my $file_read = <STDIN>; chomp $file_read; my %w_counter = (); my %c_counter = (); my $totalcount = 0; my $charcount = 0; my $p_val = 0; my $var; my $avglen; open(FILE, "$file_read") or die "Could not open file: $!\n"; my @array = <FILE>; close FILE; foreach (@array) { if (/\b\w+\-\w+\-$/) { s/\n/ /sg; $var .= $_; } elsif (/\b-$/) { s/\b\-\n+/ /sg; $var .= $_; } else { s/\n/ /sg; $var .= $_; } } $var =~ s/-{2}/ /g; $var =~ tr/[A-Z]/[a-z]/; my $expr = q/([\w]+[-]?[']?(?:\w*)?[-]?(?:\w*)?)/; my $subexpr = qr/$expr/; while ($var =~ /$subexpr/g) { $w_counter{$1}++; } sub sort_byval_w { $w_counter{$b} <=> $w_counter{$a}; } open(RESULTS, ">results.txt"); foreach my $key (sort sort_byval_w(keys %w_counter)) { print RESULTS "The word $key was seen $w_counter{$key} times\n"; $totalcount += $w_counter{$key}; } open(FILE, "$file_read") or die "Could not open file: $!\n"; while (<FILE>) { while(/(.)/sg) { $c_counter{$1}++; } } close FILE; sub sort_byval_c { $c_counter{$b} <=> $c_counter{$a}; } foreach my $key (sort sort_byval_c(keys %c_counter)) { my $space = " "; if ($key =~ /\t/) { $p_val = $c_counter{$key}; delete $c_counter{$key}; $key = "<TAB>"; $c_counter{$key} = "$p_val"; } elsif ($key eq "$space") { $p_val = $c_counter{$key}; delete $c_counter{$key}; $key = "<SPACE>"; $c_counter{$key} = "$p_val"; } elsif ($key =~ /\n/) { $p_val = $c_counter{$key}; delete $c_counter{$key}; $key = "<NEWLINE>"; $c_counter{$key} = "$p_val"; } elsif ($key =~ /\r/) { $key = "<RETURN>"; $p_val = $c_counter{$key}; delete $c_counter{$key}; $c_counter{$key} = "$p_val"; } print RESULTS "The char $key was seen $c_counter{$key} times\n +"; $charcount += $c_counter{$key}; } ## Get avg numb words per sentance my $sentences = $c_counter{"."} + $c_counter{"?"} + $c_counter{"!"}; my $avgwords_sent = $totalcount / $sentences; my $avgwords_tot = $charcount / $totalcount; print RESULTS "\n"; print RESULTS "-----------------------------\n"; print RESULTS "Total characters are: $charcount\n"; print RESULTS "Total words: $totalcount\n"; print RESULTS "Average words per sentence is: $avgwords_sent\n"; print RESULTS "Average length of words is: $avgwords_tot\n"; close RESULTS; print "See results.txt for your results\n";

In reply to Word Count Script+ by xgunnerx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the monks are mute...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (6)
    As of 2017-12-16 11:20 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      What programming language do you hate the most?




















      Results (450 votes). Check out past polls.

      Notices?