473,545 Members | 2,358 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
  3. $datafile = $ARGV[0];
  4. print "$datafile\n";
  5. open (LIST1, $datafile) || die "File not found\n";
  7. $tags = "tags.txt";
  8. open FILE, "$tags" or die "Couldn't open file: $!";
  9. @test=<FILE>;
  10. $string = join("", @test);
  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. }
  21. close FILE;
  22. close LIST1;
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?
Aug 4 '09 #1
3 3553
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
89 New Member
Thanks for your response Kelvin.

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


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.
Aug 11 '09 #3
410 Recognized Expert Contributor
You can try:
Expand|Select|Wrap|Line Numbers
  1. my $count = $string =~ s/\b$v[0]\b//g;
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

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...
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...
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?
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
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...
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".
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...
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.... ................... ..
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.
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...
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. ...
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...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
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...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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...

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.