Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: hash to count words

by NetWallah (Abbot)
on Sep 13, 2012 at 14:30 UTC ( #993504=note: print w/replies, xml ) Need Help??

in reply to hash to count words

Please use "code" tags correctly, as described in Writeup Formatting Tips.

Your regex "$word =~ s/^\w\x\//g;" is invalid.

you probably want:

$word=~s/[^\w]//g; # Eliminate non-word chars
Your re-initilazation of "%hash=()" within the loop makes the hash useless.

Declare it outside the loop, and add data to it using

$hash{$word}++; # Get a count of the word
Later , you can get a list of words found by accessing:
keys (%hash)
Please try to follow davido's excellent advice in response to your previous question on the same subject.

             I hope life isn't a big joke, because I don't get it.

Replies are listed 'Best First'.
Re^2: hash to count words
by underoathed (Initiate) on Sep 13, 2012 at 14:46 UTC

    So here is what i have for code so far for the hash.

    #!/usr/bin/perl -w open (IN, $file) or die "Cannot open file '$file' : $!\n; $line = <IN>; @array = split ( ' ', $line); foreach $word (@array){ $word =~ s/^\w\w//g; $word = lc ($word); print "$word\n";} $hash {$the}++; $hash {$linux}++; $hash {$hello}++;
    What am I doing wrong?
      Ok - I'll take a stab at answering your question "What am I doing wrong?" :
      • Not using 'strict'. (add "use strict;" to the top of your code)
      • Not using canonical "open" with local filehandle. (Try : open(my $f, "<", $file) or die "Cannot open '$file':$!";)
      • Not declaring variables (use my @array; (follows from "use strict"))
      • Not formatting/aligning your code for visibility
      • Not following advice (davido's code works for what you need)
      • Not reading how hashes work
      • Not closing the file handle you opened.
      • Not posing your question(s) clearly
      • Not showing what you expect as a result of your code
      • Not showing sample data
      The answer to the question in your previous post on the meaning of "/g" can be found by running "perldoc perlre".

                   I hope life isn't a big joke, because I don't get it.

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://993504]
[stevieb]: LOL you can easily replace "Helsinki" with "Alberta"
[perldigious]: I used to live at the top of a very steep hill stevieb, I can relate to the video. Literally used to have to build up momentum and try to force my vehicle to make it up the hill to park in my driveway properly... didn't always make it.
[choroba]: Why don't they use tire chains?
[perldigious]: First snow, probably not prepared right away choroba.
[stevieb]: it's Quebec... they do weird stuff there :) In the mountains, it is *mandatory* for trucks to use tire chains. Many passenger vehicles do as well (but it's not mandatory for them)
[perldigious]: That stuff can hit fast... like what we just got where I'm at. Saw a few people who had slid OUT of the roundabout I have to drive through to get to work, not used to the ice yet, take awhile to get the hang of it again and make vehicle adjustments.
[stevieb]: man, when the roads are glare ice, I don't even bother going to work or out... unless I absolutely have to, or was already out in the first place
[choroba]: Tire chains are mandatory here in mountains, and the only unprepared each year are gritters
[stevieb]: I have a 3 day winter survival kit in my vehicle in the event I get snowed in in the mountains (which has happened before due to avalanches closing the roads (and once in the summer due to a massive forest fire that trapped us
[perldigious]: a handful of people in my work area did not make it, but I live pretty close and it's flat the whole way, so I didn't have any trouble. The roundabout is the worst thing I have to negotiate.

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2016-12-06 16:36 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (112 votes). Check out past polls.