Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8853677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:42:39+00:00 2026-06-14T13:42:39+00:00

I’m trying to make a program where I read in a file with a

  • 0

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; 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:42:40+00:00Added an answer on June 14, 2026 at 1:42 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.