Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

First of all, do not comment out 'use strict' and 'use warnings'. If you have to ask why, you aren't to a level where you should consider doing so.

Second, you have a ',' (comma) at the end of your assignment to $file_name where you should have a ';' (semicolon), which is an error (that you would have found using 'strict' and 'warnings'). Once this was fixed, the code you provided (with the line '..' commented out) generated the expected output.

Tested code:

$ cat 11104497.pl #!usr/bin/local/perl use strict; use warnings; my %count; my $file_name = shift or die "Usage: perl $0 [FILE]\n"; open my $fh, '<', $file_name or die "Could not open '$file_name' $!"; while ( my $line = <$fh> ) { chomp $line; foreach my $word ( split /\s+/, $line ) { $count{$word}++; } } foreach my $word ( sort keys %count ) { printf "%-31s %s\n", $word, $count{$word}; } # .. my @sorted_by_count = sort { $count{$b} <=> $count{$a} } keys %count; print "These are the 10 most frequented words of $file_name :\n"; print "$_ occured $count{$_} times\n" for @sorted_by_count[ 0 .. 9 ];

Output:

$ cat janne.txt This is a test. This is only a test. Had this been an actual emergency, the attention signal you just heard + would have been followed by news or instructions. This has been a test. $ $ perl 11104497.pl janne.txt Had 1 This 3 a 3 actual 1 an 1 attention 1 been 3 by 1 emergency, 1 followed 1 has 1 have 1 heard 1 instructions. 1 is 2 just 1 news 1 only 1 or 1 signal 1 test. 3 the 1 this 1 would 1 you 1 These are the 10 most frequented words of janne.txt : a occured 3 times This occured 3 times been occured 3 times test. occured 3 times is occured 2 times has occured 1 times or occured 1 times news occured 1 times by occured 1 times this occured 1 times $

Moral of the story: when in doubt, use 'use strict' and 'use warnings'.

Hope that helps.

2019-09-14: Removed duplicated 'and's and added missing 'the' to paragraph two (2), sentence two (2).


In reply to Re: Why isn't my script reading from my file? by atcroft
in thread Why isn't my script reading from my file? by Jannejannesson

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 web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (7)
    As of 2020-10-19 15:36 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favourite web site is:












      Results (204 votes). Check out past polls.

      Notices?