Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change an array into a hashtable?

Tags:

hashmap

perl

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; 
}
like image 826
Kirs Kringle Avatar asked Feb 01 '26 08:02

Kirs Kringle


1 Answers

Some notes about hashes in Perl... Problem description:

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.

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 $word occured in your @words array. 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' by

$count{foo}++;

Your 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:

%count = (hello => 42, world => 17);

set a single value for a given key by assigning a single value for a defined key (this is what we did before):

$count{hello} = 42;

set a list of values for a given list of keys using a so-called hash slice:

@count{qw(hello world)} = (42, 17);

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.

like image 176
memowe Avatar answered Feb 04 '26 01:02

memowe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!