By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,710 Members | 1,906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,710 IT Pros & Developers. It's quick & easy.

number of string occurence

P: 89
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
Share this Question
Share on Google+
3 Replies


KevinADC
Expert 2.5K+
P: 4,059
please post some sample data so we aren't trying to guess what the problem is.
Aug 4 '09 #2

P: 89
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
Expert 100+
P: 410
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

Post your reply

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