473,396 Members | 1,998 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,396 software developers and data experts.

Getting Info from Array

Hi

I have a array in which I need to count the number of ocurrence of a particular word
for eg I need to count no of times a word "test" , "test2" occurs in a array @list.
(The contents of the array is around 100 lines)

Code:
Expand|Select|Wrap|Line Numbers
  1. open(FH3, "sample.txt");
  2. while(<FH3>)
  3. {
  4. @array=$_;
  5. print @array;
  6. }
  7. close(FH3);
  8. my %counts = ();
  9. for (@array) {
  10.    $counts{$_}++;
  11. }
  12. foreach my $keys (keys %counts) {
  13.    print "$keys = $counts{$keys}\n";
  14. }
  15.  
  16.   if($counts{$keys}==1)
  17.         {
  18.       print "$keys = $counts{$keys}\n";
  19.         }
  20.  
I dont want the count for all the words in the array and I need the same only for a given Input word...

Can you help me in this regard..

Thanks In Advance
Mar 14 '08 #1
2 1404
nithinpes
410 Expert 256MB
The script you have used is very confusing to understand your objective as per the description. Is that the file sample.txt contains one word per line which you are trying to pass to an array? In that case, you can achieve it without the need of the array:

Expand|Select|Wrap|Line Numbers
  1. open(FH3, "sample.txt");
  2. while(<FH3>)
  3. {
  4. chomp;
  5.  $seen{$_} ++ ;
  6. }
  7. print "enter the string that you want to search:";
  8. chomp($str=<STDIN>);
  9. print "$str occurs $seen{$str} times";
  10.  
  11.  
OR, is that you have text file with multiple lines and you need to split each line into words and push it into array for search? For this you can use:

Expand|Select|Wrap|Line Numbers
  1. open(FH3, "cookies.txt");
  2. while(<FH3>)
  3. {
  4. chomp;
  5.  push @a, split(/\s+/,$_); ##split on spaces
  6. }
  7. my %seen =();
  8. $seen{$_}++ foreach(@a);
  9. print "enter the string that you want to search:";
  10. chomp($str=<STDIN>);
  11.  
  12. print "$str was found $seen{$str} times";
  13.  
Mar 14 '08 #2
KevinADC
4,059 Expert 2GB
This is just wrong (in the original code):


Expand|Select|Wrap|Line Numbers
  1. open(FH3, "sample.txt");
  2. while(<FH3>)
  3. {
  4. @array=$_;
  5. print @array;
  6. }
unless the file only has one line. Otherwise @array will be populated only with the value of the last line of the file.
Mar 14 '08 #3

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

Similar topics

2
by: Timo J | last post by:
Hi - Im sitting and trying to understand this OOP - and need to create a class wich can do the following.. ShowBlocks() - Displays the data wich is inside it - If empty creates a form wich sends...
2
by: Tek9_AK | last post by:
I need to find a way to transfer all the values of an array inside a function out of the fuction into another array. IE function splice($filename){ if(file_exists($filename)){...
12
by: Mary Catherine | last post by:
I have 2 scipts that I am trying to get to run on the same page. One is a time/date script, the other is a countdown script (i.e. countdown days, hours, mins until a given date). They both work...
2
by: jeff_zhang446 | last post by:
Hi, I try to write some data structure into a file as below and would appreciate if someone can give me some advice here. row(1) 100 1 0 0 0 0 0 0 0 1 row(2) 150 5 0 0 0 0 0...
3
by: rasa | last post by:
I am able to instantiate and make calls to a com module that use only primitive data types, but I simply don't know what the c# equivalent is for a function expecting short*. Any help would be...
1
by: Mr. B | last post by:
VB.net 2003 c/w Framework 1.1 and MS Access db We have a commercial program that does our Acounting and Time Sheets (Timberline). At least once a day our Accounting department runs a Script...
9
by: Freebird | last post by:
Hello everyone ... I've a problem in here, I need to make a 70 000 rows insert, and it's taking a lot more than 30 seconds, my problem is that it will be a script that will work in many...
9
by: Vikram | last post by:
I was just curious to know how are calls like say dynamic_cast implemented. Is it really expensive to get the exact type of an object being pointed to by the baseclass pointer? The reason I am...
13
by: Ørjan Langbakk | last post by:
I wanna make a file that holds the complete pricelist for a small webshop (yes, I know that a database in the background would be a lot simpler, but that is not an option today, unfortunately). ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.