473,402 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Count substrings from a string

can anyone help me in doing that
Here is the code, where am I going wrong??

Expand|Select|Wrap|Line Numbers
  1. while ($string) {
  2.     foreach $word (@words) {
  3.         @words = split " ", @string;
  4.         $count{$word}++;
  5.     }
  6. }
  7. foreach $word (keys %count) {
  8.     print "$word : $count{$word}\n";
  9. }
Aug 9 '07 #1
2 2293
3,509 Expert Mod 2GB
You have a lot going on here, but not necessarily all in the correct order. The first thing that jumps out at me is this:

Expand|Select|Wrap|Line Numbers
  1. @words = split " ", @string;
You have that defined inside of your foreach loop, but are using the array @words as an argument to your foreach loop. This, I am afraid, is not possible. You must put that outside of the foreach loop so that it is defined before the foreach loop is entered.

Next, in that code snippet above, you reference @string, but it isn't defined anywhere, but $string is.

Do me a favor please. Clean up your code so that it flows properly and if you could, paste all of your code here if it isn't already. Then, please give a better explanation of exactly what you are wanting to do, along with a sample of the data you are referencing, so we know what we are working with.


Aug 10 '07 #2
1,089 Expert 1GB
As Jeff pointed out, your current code is mostly nonsense. However, the goal of counting words is a rather simple one. Observe the following example:

Expand|Select|Wrap|Line Numbers
  1. my $string = q{foo bar baz bar bit baz foo bot but bar biz};
  3. my %count_of;
  4. foreach my $word (split ' ', $string) {
  5.     $count_of{$word}++;
  6. }
  8. while (my ($word, $count) = each %count_of) {
  9.     print "$word : $count\n";
  10. }
- Miller
Aug 10 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Leandro Pardini | last post by:
Hello there, I'm trying to process a binary file and I really don't know how. The story: gPhoto2 downloads the images from my camera just fine, but small areas of 10x3 pixels are screwed up. I...
by: spam | last post by:
Is there a well-known algorithm for replacing many substrings in a string? For example, I'd like to take the string "abc def ghi jkl mno pqr" and replace, say, every instance of "abc", "ghi", and...
by: C3 | last post by:
I have to process some data in C that is given to me as a char * array. I have a fairly large number of substrings (well, they're not actually printable, but let's treat them as strings) that I...
by: JD | last post by:
Hi guys I'm trying to write a program that counts the occurrences of HTML tags in a text file. This is what I have so far: #include <stdio.h> #include <stdlib.h> #include <string.h> ...
by: Robert Dodier | last post by:
Hello all, I'm trying to find substrings that look like 'FOO blah blah blah' in a string. For example give 'blah FOO blah1a blah1b FOO blah2 FOO blah3a blah3b blah3b' I want to get three...
by: Matteo Rattotti | last post by:
Hi all, i've noticed a strange beaviour of string.count: in my mind this code must work in this way: str = "a_a_a_a_" howmuch = str.count("_a_") print howmuch -> 3
by: girish | last post by:
Hi, I want to generate all non-empty substrings of a string of length >=2. Also, each substring is to be paired with 'string - substring' part and vice versa. Thus, gives me , , , , , ] etc....
by: nick048 | last post by:
Hi to All, I need to search the string occurencies in a structure like this: struct node { char info; struct node *next; };
by: Pilcrow | last post by:
This problem was raised in comp.lang.perl.misc, and the poster was concerned, among other things, by the speed of execution. Since C is faster than perl, I wonder how a C coder would solve it? ...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.