473,748 Members | 6,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

number of string occurence

89 New Member
I am trying open a text file, and find the occurence of first column string (ie) $v[0] in a separate file which contains only one column.

my code is as follows but somehow it doesn't seem to count the number of occurences in the second file.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. $datafile = $ARGV[0];
  4. print "$datafile\n";
  5. open (LIST1, $datafile) || die "File not found\n";
  6.  
  7. $tags = "tags.txt";
  8. open FILE, "$tags" or die "Couldn't open file: $!";
  9. @test=<FILE>;
  10. $string = join("", @test);
  11.  
  12.  
  13. while(<LIST1>) {
  14.         chomp;
  15.         my(@v) = split(/\t/);        
  16.         #$count = ($string =~ tr/$v[0]//);
  17.         $count = split(/$v[0]/, $string);
  18.         print "$v[0]\t$count\n";
  19. }
  20.  
  21. close FILE;
  22. close LIST1;
  23.  
I know that substring search can't be done using #$count = ($string =~ tr/$v[0]//);

split also doesn't give the numbers correctly. Anything I am missing?
Thanks.
Aug 4 '09 #1
3 3565
KevinADC
4,059 Recognized Expert Specialist
please post some sample data so we aren't trying to guess what the problem is.
Aug 4 '09 #2
lilly07
89 New Member
Thanks for your response Kelvin.

data file format (tags.txt) would be as follows:

tag1
tag2
tag1
tag4
tag6
tag2

For every line of another file which has the format as fooolows

tag1 test test2 tes3...

I want to count how many times the first column tag1 is appearing in the tag.txt file.

I tried as stated below.
1. Opened tags.txt under an array
2. $count = split(/$v[0]/, $string); to count the number of "tag1" occurence in tags.txt but it is not working.
Thanks.
Aug 11 '09 #3
nithinpes
410 Recognized Expert Contributor
You can try:
Expand|Select|Wrap|Line Numbers
  1. my $count = $string =~ s/\b$v[0]\b//g;
  2.  
Word boundary(\b) is included in the pattern assuming you want to match the whole word. Else, you need not use them.
Aug 11 '09 #4

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

Similar topics

2
2463
by: Tesla | last post by:
Hey guys, I have a string like "lalala: djkahsd : dajkdassd : adasd :" Is there a function to find the position of the "I"th occurence of a character/string? Like lets say I want to find out the position of the second colon. How do I go about that?? I can only find functions that return the position of the first or last occurence, but not a variable occurence ::sniffles::
4
1435
by: Yves-Alain NICOLLET | last post by:
I have a script that opens a small window containing a button which when clicked performs a find to the next occurence of a string. It works well when the string is made of only one word or of consecutive words. Now I want it to find the next occurence of one of the words contained in a list, ie. I want to dynamically change the string to find. I failed to use a regular expression instead of a string in the find statement. Any idea of...
4
14127
by: sibingpeter | last post by:
Hi there, Im trying to find the right way to code the loop to count the number of occurences of a given substring in a string. Im able to find the first occurence using the strstr function, but I just cant seem to think of the right loop that would continue searching after finding this first occurence. Could someone please help me out here?
2
4209
by: scottys0 | last post by:
Hi everyboy, I'm working in an IOC-IBM (IBM open class) replamecent library. the code seens confused but , no, isn't. #define INumber long class IString : public std::string { private: ... // some control variables
2
1972
by: DaFerg | last post by:
Hi ... I have a weblog database where I want to count the occurences of a table of string values that appear in all the urls viewed. My tblWebLog as a field that contains the url ... tblWebLog. I have another table ... tblStrings ... that has a field for a string value and an integer field to count the occurence of the string in tblWebLog.
2
2413
by: gauravguleria | last post by:
wht's the program which takes two strings using command line arguments and finds the occurence of second string in the first string.The program should return the starting position of first occurence if it is available otherwise should display "NOT FOUND".
5
4489
by: IdleBrain | last post by:
I am trying to log the Application name, Method name, line number and column number whenever an exception is generated in an C# 2005 application using the following code. Problem is that the line number that is being obtained by stackFrame.GetFileLineNumber() represents AnotherForm.LogMessage function's line number. I am trying to write the line number of the statement which is causing an exception and not the LogMessage function's line...
4
3956
by: mercuryshipzz | last post by:
Hi, My objective is to get the line number of the first occurance of the search pattern. my test.txt contains: ..... .................. total rows.... ................... ..
1
1138
by: StepIre | last post by:
Hey Guys, Sorry if this is silly question but i am new to all of this! Basically i have a file that has some content in it that i want to get out. i want to find say an occurence of the string "blah". then i want to find the next occurence of the string "blah2" after that.
0
8987
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9534
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9316
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.