I’m trying to make a program where I read in a file with a bunch of text in it. I then take punctuation out and then I read in a file that has stop words in it. Both get read in and put into arrays. I’m trying to put the array of the general text file and put it in a hash. I’m not really sure what I’m doing wrong, but I’m trying. I want to do this so I can generate stats on how many words are repeated and what not, but I have to take out stop words and such.
Anyway here is what I have so far I put a comment #WORKING ON MERGING ARRAY INTO HASH that is where I’m working at. I don’t think the way I’m trying to put the array into the hash is right, but I looked online and the %hash{array} = “value”; doesn’t compile. so not sure how else to do it.
Thanks, if you have any questions for me I will respond back quickly.
#!/usr/bin/perl
use strict;
use warnings;
#Reading in the text file
my $file0="data.txt";
open(my $filehandle0,'<', $file0) || die "Could not open $file0\n";
my@words;
while (my $line = <$filehandle0>){
chomp $line;
my @word = split(/\s+/, $line);
push(@words, @word);
}
for (@words) {
s/[\,|\.|\!|\?|\:|\;]//g;
}
my %words_count; #The code I was told to add in this post.
$words_count{$_}++ for @words;
Next I read in the stop words I have in another array.
#Reading in the stopwords file
my $file1 = "stoplist.txt";
open(my $filehandle1, '<',$file1) or die "Could not open $file1\n";
my @stopwords;
while(my $line = <$filehandle1>){
chomp $line;
my @linearray = split(" ", $line);
push(@stopwords, @linearray);
}
for my $w (my @stopwords) {
s/\b\Q$w\E\B//ig;
}
Some notes about hashes in Perl… Problem description:
At first, ask yourself why you want to “put the array into the hash”. An array represents a list of values while a hash represents a set of key-value pairs. So you have to define what keys and values should be. Not only for us, but for you. It often helps to explain even simple things to get a better understanding.
In this case, you may want to count how often a given word
$wordoccured in your@wordsarray. This could be done by iterating over all words and increase$count{$word}by one each time. This is what @raina77ow did in his answer. Important here is, that you’re accessing single hash values, which are represented with the scalar sigil$in Perl. So if you have a hash named%count, you can increase the value for the key'foo'byYour result of “online looking” above (
%hash{array} = "value") doesn’t make sense. There are three valid ways to store values in a hash:set all key-value pairs by assingning a even-sized list to the whole hash:
set a single value for a given key by assigning a single value for a defined key (this is what we did before):
set a list of values for a given list of keys using a so-called hash slice:
Note the use of sigils here:
%for a hashy even-sized list of keys and values mixed,$for single (scalar) values and@for lists of values. In your example you’re using%, but define an array in the key braces{...}and assign a single scalar value.