•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 375,198 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 1,994 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:
Views: 3243 | Replies: 32
![]() |
•
•
Join Date: Oct 2007
Posts: 41
Reputation:
Rep Power: 1
Solved Threads: 0
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 ">"
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; Perl Syntax (Toggle Plain Text)
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.
challenge the limits
•
•
Join Date: Oct 2007
Posts: 41
Reputation:
Rep Power: 1
Solved Threads: 0
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
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.
Note: Please dont post your queries to private message, the forum is provided for that only, getting help.
katharnakh
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.
Perl Syntax (Toggle Plain Text)
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);
katharnakh
challenge the limits
Below is the code I am working with:
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
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
Perl Syntax (Toggle Plain Text)
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.
Last edited by katharnakh : Apr 9th, 2008 at 1:08 am.
challenge the limits
this might return false matches:
it would be better (I think judging by the sample data) to anchor it to the beginning of the string:
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:
should be:
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.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Perl Marketplace
Similar Threads
- Count characters of a string. (C++)
- Counting all Non-blank Characters in a String (Java)
- why does my string array do this? (C++)
- Counting specific characters in a string (C)
- Character/letter count (Pascal and Delphi)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
- String operatios (C++)
Other Threads in the Perl Forum
- Previous Thread: add text in the c functions.plz help me
- Next Thread: Can Array Name have Variable?


Linear Mode