User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 427,203 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,176 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Perl advertiser: Programming Forums
Views: 4310 | Replies: 32
Reply
Join Date: Oct 2007
Posts: 41
Reputation: mank is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mank mank is offline Offline
Light Poster

count characters in a string

  #1  
Mar 18th, 2008
This code counts number of characters in a single line
how can I make it count(give total X Y Z) it in multiple lines seperated by ">"

while(<IN>) {
  chomp;
  my @line = split(//, $_);  
  if($line[0] eq ">"){print @line;}
          elsif (($line[0] eq "X"||"Y"||"Z")||($line[0] ne ">"))
                        {
                my %cnter;
                         foreach my $gc (@line) 
                             {$cnter{$gc} ++;}

                      print join(" ", @cnter{qw(X Y Z)}), "\n";

                        }


                }

close(IN);
exit;
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2006
Posts: 221
Reputation: katharnakh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 20
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: count characters in a string

  #2  
Mar 18th, 2008
  1. open(IN, "readme.txt") || die "ERROR: $!";
  2. my %cnter;
  3.  
  4. while(<IN>) {
  5. chomp;
  6. my @line = split(//, $_);
  7. if($line[0] eq ">"){print @line, "\n";}
  8. elsif (($line[0] eq "X"||"Y"||"Z")||($line[0] ne ">"))
  9. {
  10. foreach my $gc (@line)
  11. {
  12. $cnter{$gc} = $cnter{$gc} ? $cnter{$gc} += 1 : 1;
  13. }
  14. }
  15. }
  16. foreach $k(keys %cnter){
  17. print "$k: $cnter{$k} | ";
  18. }
  19. close(IN);

# readme.txt
> this is first line
X this is second line
Y this is third line
Z this is forth line

# Output
> this is first line
 : 12 | c: 1 | d: 2 | e: 4 | f: 1 | h: 5 | i: 10 | l: 3 | n: 4 | o: 2 | r: 2 | s: 7 | t: 5 | X: 1 | Y: 1 | Z: 1 |

katharnakh.
challenge the limits
Reply With Quote  
Join Date: Oct 2007
Posts: 41
Reputation: mank is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mank mank is offline Offline
Light Poster

Re: count characters in a string

  #3  
Mar 18th, 2008
This is what I am looking for 
input file

>abc
XXYYZZ
XXYYZZ
>bcd
XXZZ
XXYY
>cde
ZZYYXX
>def
YYZZ
YYXX

output->

>abc 4 4 4
>bcd 4 2 2
>cde 2 2 2
>def 2 4 2
Reply With Quote  
Join Date: Jan 2006
Posts: 221
Reputation: katharnakh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 20
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: count characters in a string

  #4  
Mar 18th, 2008
Hi,
since, X, Y and Z are repeating after every line having '>', make line having '>' as initial key in hash, and then add keys(X, Y, Z) and values.

  1. open(IN, "readme.txt") || die "ERROR: $!";
  2. my (%cnter, $marker);
  3. while(<IN>) {
  4. chomp;
  5. my @line = split(//, $_);
  6. if($line[0] eq ">")
  7. {
  8. $marker = $_;
  9. print @line, "\n";
  10. }
  11. elsif (($line[0] eq "X"||"Y"||"Z")||($line[0] ne ">"))
  12. {
  13. foreach my $gc (@line)
  14. {
  15. $cnter{$marker}{$gc} = $cnter{$marker}{$gc} ? $cnter{$marker}{$gc} += 1 : 1;
  16. }
  17. }
  18. }
  19.  
  20. foreach $k(keys %cnter){
  21. print "\n$k: ";
  22. foreach $kk(keys %{$cnter{$k}}){
  23. print "\t$kk: $cnter{$k}{$kk}";
  24. }
  25. }
  26. close(IN);
Note: Please dont post your queries to private message, the forum is provided for that only, getting help.

katharnakh
challenge the limits
Reply With Quote  
Join Date: Oct 2007
Posts: 41
Reputation: mank is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mank mank is offline Offline
Light Poster

Re: count characters in a string

  #5  
Mar 18th, 2008
Thanks
Reply With Quote  
Join Date: Mar 2008
Posts: 17
Reputation: godevars is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
godevars's Avatar
godevars godevars is offline Offline
Newbie Poster

Re: count characters in a string

  #6  
Apr 8th, 2008
I have been playing with this. I have tried updating it to count words. When I update the split to /\s+/ I receive an error for an unitialized hash value. How would I correct it? I tried by changing the elsif line to not include the X,Y and Z argument.
Reply With Quote  
Join Date: Mar 2006
Posts: 620
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 33
KevinADC's Avatar
KevinADC KevinADC is online now Online
Practically a Master Poster

Re: count characters in a string

  #7  
Apr 8th, 2008
post your code and some sample data.
Reply With Quote  
Join Date: Mar 2008
Posts: 17
Reputation: godevars is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
godevars's Avatar
godevars godevars is offline Offline
Newbie Poster

Re: count characters in a string

  #8  
Apr 8th, 2008
Below is the code I am working with:

1. use strict;
2.
3. open(FILEIN,'Sample.txt') || die "Can't open OUT file: $!\n";
4. open (FILEOUT, '>SampleOut2.txt') ||die "Can't open OUT file: $!\n";
5.
6. my (%cnter, $marker);
7. while(<FILEIN>) {
8.		chomp;
9.		my @line = split(/\s+/, $_);
10.		if($line[0] eq "=")
11.		{
12.			$marker = $_;
13.			print FILEOUT @line, "\n";
14.		}
15.		elsif ($line[0] ne "=")
16.		{
17.		  	foreach my $gc (@line)
18.				{
19.		 		$cnter{$marker}{$gc} = $cnter{$marker}{$gc} ? $cnter{$marker}{$gc} += 1 : 1;
20.		 		}
21.		}
22. }
23.
24. foreach my $k(keys %cnter){
25.		print FILEOUT "\n$k: ";
26.		foreach my $kk(keys %{$cnter{$k}}){
27. 				print FILEOUT "\t$kk: $cnter{$k}{$kk}\n";
28.		}
29. }
30. close FILEIN;
31. close FILEOUT;

Text for the sample is:

======Retirement Plan Fundamentals Part I

The objective of the Retirement Plan Fundamentals, Parts I and II, is to give an individual
beginning a career as a retirement plan professional a general background in qualified plans as a first step toward meeting the challenges of the profession.

=====Retirement Plan Fundamentals Part III

Retirement Plan Fundamentals Part III (RPF-3) covers plan administration, including census collection, benefit allocations and coverage and nondiscrimination testing. This course emphasizes daily valuation recordkeeping but includes discussions of balance-forward plans and conversions.

I am looking to count the words in each section seperately but reported in one output.

Thanks
Reply With Quote  
Join Date: Jan 2006
Posts: 221
Reputation: katharnakh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 20
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: count characters in a string

  #9  
Apr 9th, 2008
  1. open(IN, "readme.txt") || die "ERROR: $!";
  2. open(OUT, ">seeme.txt") || die "ERROR: $!";
  3.  
  4. my (%cnter, $marker);
  5. while(<IN>) {
  6. chomp;
  7. my @line = split(/\s+/, $_);
  8. if($line[0] =~ /=/)
  9. {
  10. $marker = $_;
  11. print @line, "\n";
  12. #print OUT @line, "\n";
  13. }
  14. elsif ($line[0] =! /=/)
  15. {
  16. foreach my $gc (@line)
  17. {
  18. $cnter{$marker}{$gc} = $cnter{$marker}{$gc} ? $cnter{$marker}{$gc} += 1 : 1;
  19. }
  20. }
  21. }
  22.  
  23. foreach $k(keys %cnter){
  24. print OUT "\n$k: ";
  25. foreach $kk(keys %{$cnter{$k}}){
  26. print OUT "\n$kk: $cnter{$k}{$kk}";
  27. #print "\t$kk: $cnter{$k}{$kk}";
  28. }
  29. }
  30. close(IN);
  31. close(OUT);

You should use a regular expression to check whether line is having a character '=', and everything else is same. Also you need to one more line to inner foreach loop to output to a file.

sample output:
======Retirement Plan Fundamentals Part I:
plans: 1
background: 1
first: 1
individual: 1
...
=====Retirement Plan Fundamentals Part III:
conversions.: 1
plans: 1
course: 1
allocations: 1
...

katharnakh.
Last edited by katharnakh : Apr 9th, 2008 at 1:08 am.
challenge the limits
Reply With Quote  
Join Date: Mar 2006
Posts: 620
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 33
KevinADC's Avatar
KevinADC KevinADC is online now Online
Practically a Master Poster

Re: count characters in a string

  #10  
Apr 9th, 2008
this might return false matches:


if($line[0] =~ /=/)

it would be better (I think judging by the sample data) to anchor it to the beginning of the string:

if($line[0] =~ /^=/)

at least that way you know it not somewhere else in the string. Might be better to just use the index() function though to avoid the unecessary overhead of a regexp.

This is also not correct syntax:

elsif ($line[0] =! /=/)

should be:

elsif ($line[0] !~ /=/)
Last edited by KevinADC : Apr 9th, 2008 at 3:16 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Perl Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Perl Forum

All times are GMT -4. The time now is 10:49 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC