![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| count the number of files which have a search string, but counting the file only once | sudheshnaiyer | UNIX for Dummies Questions & Answers | 1 | 08-11-2007 01:50 PM |
| count the number chracters occurances in a line | skyineyes | Shell Programming and Scripting | 6 | 06-26-2007 09:09 AM |
| Replace all occurances of a string in all file-/foldernames, recursively | TheMJ | Shell Programming and Scripting | 2 | 04-12-2006 01:40 AM |
| counting characters | plelie2 | Shell Programming and Scripting | 6 | 02-25-2003 01:38 PM |
| Counting Occurances in Two Files | Keith Gergel | UNIX for Dummies Questions & Answers | 2 | 06-26-2001 02:47 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Counting the number of occurances of all characters (a-z) in a string
Hi,
I am trying out different scripts in PERL. I want to take a line/string as an input from the user and count the number of occurrances of all the alphabets (a..z) in the string. I tried doingit like this : #! /opt/exp/bin/perl print "Enter a string or line : "; $string = <STDIN>; chop $string; $string =~ tr/[A-Z]/[a-z]/; #print @string; print "\n"; @arr = ('a' .. 'z'); foreach $val (@arr) { $count = ($string =~ tr/$val//); print "$val occurred $count times\n"; } But looks like I am going wrong somewhere. Please guide me to solve this. Regards, Sendhil |
|
||||
|
Not trying to win any awards for maintainability...
![]() Code:
#!/usr/bin/perl
#
# count.pl
#
use strict;
use warnings;
my $char;
my %chars=();
print "Enter a line: ";
while( $char = getc() ) {
last if( $char =~ /\n/ );
$chars{ $char }++;
}
my @keys = sort keys( %chars );
foreach my $key (@keys) {
print "$key = $chars{ $key }\n";
}
Code:
$ perl count.pl Enter a line: This is a test = 3 T = 1 a = 1 e = 1 h = 1 i = 2 s = 3 t = 2 |
|
||||
|
Non-destructive counting
I know this is an older thread, but I had a slightly different scenario, where I needed non-destructive counting and thought I'd share.
The s///g and tr/// methods already mentioned remove text from the original string. With a slight modification (adding parentheses and $1), the s/// operator can be used non-destructively: $count = $string =~ s/([a-zA-Z])/$1/g; In my specific case, I needed to sort a list of directories by depth. That is: /, /onelevel, /two/levels, /three/level/path, etc. My solution was the following: @dirs = sort {($a =~s/(\/)/$1/g) <=> ($b =~s/(\/)/$1/g)} @dirs; It sorts the list by the number of times "/" appears in the directory, which (on a Unix system) is the same as the depth. The sort function is able to obtain the counts repeatedly and still have the original strings left over. Note that my code doesn't correctly handle doubled slashes (e.g. "/my//dir") or relative paths (e.g., "../dir" or "/my/dir/../dir2"), but it's easy to handle those by ensuring that the paths are all absolute prior to the above line of code. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|