DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   Perl (http://www.daniweb.com/forums/forum112.html)
-   -   count characters in a string (http://www.daniweb.com/forums/thread114477.html)

mank Mar 18th, 2008 12:30 am
count characters in a string
 
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;

katharnakh Mar 18th, 2008 1:32 am
Re: count characters in a string
 
open(IN, "readme.txt") || die "ERROR: $!"; 
my %cnter;

while(<IN>) {
  chomp;
  my @line = split(//, $_);
  if($line[0] eq ">"){print @line, "\n";}
  elsif (($line[0] eq "X"||"Y"||"Z")||($line[0] ne ">"))
  {
      foreach my $gc (@line)
      {
              $cnter{$gc} = $cnter{$gc} ? $cnter{$gc} += 1 : 1;
      }
  }
}
foreach $k(keys %cnter){
        print "$k: $cnter{$k} | ";
}
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.

mank Mar 18th, 2008 1:41 am
Re: count characters in a string
 
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

katharnakh Mar 18th, 2008 3:36 am
Re: count characters in a string
 
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.

open(IN, "readme.txt") || die "ERROR: $!";
my (%cnter, $marker);
while(<IN>) {
                chomp;
                my @line = split(//, $_);
                if($line[0] eq ">")
                {
                                $marker = $_;
                                print @line, "\n";
                }
                elsif (($line[0] eq "X"||"Y"||"Z")||($line[0] ne ">"))
                {
                          foreach my $gc (@line)
                                {
                                                $cnter{$marker}{$gc} = $cnter{$marker}{$gc} ? $cnter{$marker}{$gc} += 1 : 1;
                                }
                }
}

foreach $k(keys %cnter){
                print "\n$k: ";
                foreach $kk(keys %{$cnter{$k}}){
                                print "\t$kk: $cnter{$k}{$kk}";
                }
}
close(IN);
Note: Please dont post your queries to private message, the forum is provided for that only, getting help.

katharnakh

mank Mar 18th, 2008 7:36 pm
Re: count characters in a string
 
Thanks :)

godevars Apr 8th, 2008 1:48 pm
Re: count characters in a string
 
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.

KevinADC Apr 8th, 2008 2:46 pm
Re: count characters in a string
 
post your code and some sample data.

godevars Apr 8th, 2008 4:11 pm
Re: count characters in a string
 
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

katharnakh Apr 9th, 2008 1:07 am
Re: count characters in a string
 
open(IN, "readme.txt") || die "ERROR: $!";
open(OUT, ">seeme.txt") || die "ERROR: $!";

my (%cnter, $marker);
while(<IN>) {
                chomp;
                my @line = split(/\s+/, $_);
                if($line[0] =~ /=/)
                {
                                $marker = $_;
                                print @line, "\n";
                                #print OUT @line, "\n";
                }
                elsif ($line[0] =! /=/)
                {
                          foreach my $gc (@line)
                                {
                                                $cnter{$marker}{$gc} = $cnter{$marker}{$gc} ? $cnter{$marker}{$gc} += 1 : 1;
                                }
                }
}

foreach $k(keys %cnter){
                print OUT "\n$k: ";
                foreach $kk(keys %{$cnter{$k}}){
                                print OUT "\n$kk: $cnter{$k}{$kk}";
                                #print "\t$kk: $cnter{$k}{$kk}";
                }
}
close(IN);
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.

KevinADC Apr 9th, 2008 3:12 am
Re: count characters in a string
 
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] !~ /=/)


All times are GMT -4. The time now is 1:59 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC