Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^4: hash to count words

by AnomalousMonk (Canon)
on Sep 13, 2012 at 16:18 UTC ( #993531=note: print w/replies, xml ) Need Help??

in reply to Re^3: hash to count words
in thread hash to count words

Please allow an addition to the list: Not bothering to fix a previous post known to be problematic.

Replies are listed 'Best First'.
Re^5: hash to count words
by underoathed (Initiate) on Sep 17, 2012 at 12:59 UTC
    Now that I have that under control, how do I print the output of the script to a file? I cannot figure it out. Thanks in advance and of course here is my coding.
    #!/usr/bin/perl -w foreach $file (@ARGV) { open (IN, $file) or die "Cannot open file '$file' : $!\n"; while(<>) { $line = <IN>; @array = split (' ', $line); foreach $word (@array){ $word =~ s/[^\w\s]//g; $word = lc ($word); $wordcount{$word} += 1; } } foreach $key (keys %wordcount) { print "Word: $key " . ($wordcount{$key}) . "\n"; } } print ("scriptOutput\n\n");
      Now that I have that under control...

      If 'that' refers to the lack code tags in your OP, the situation is still out of control. A  <code> tag is closed by a  </code> tag, not by a  </c> tag. Please see Markup in the Monastery. Please check the effects of your changes.

      Printing to a file usually involves supplying an output file handle (see open) to the print statement. E.g.:
          my $filename = '';
          open my $fh_out, '>', $filename or die "opening '$filename': $!";
          my $something_to_output_to_file = your_process();
          print $fh_out "here is something for the file \n";
          print $fh_out $something_to_output_to_file;
          print $fh_out "that's all, folks! \n";
          close $fh_out or die "closing '$filename': $!";

      Please consider re-reading the answers you have been given in this and related threads. Please consider using lexical filehandles (as in the example given above). See also autodie.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://993531]
[ambrus]: Eily: hmm... do you count your user and your scratchpad as root nodes you posted?
[Eily]: I would have if it had been convenient :P
[hippo]: Those are auto-posted. We just update them.
[LanX]: "you haven't been a monk" ???
[LanX]: oh "yet" .. got it
[Eily]: LanX: sadly I wasn't born a monk
[LanX]: #SoSad #Shame
LanX silently plays Springsteen in the background "♪..♫ ... I'm on fire ..."

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2017-03-24 14:45 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (304 votes). Check out past polls.