The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-09-2007
rsendhilmani rsendhilmani is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 4
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
  #2 (permalink)  
Old 07-09-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
If it can be done in shell script:
Code:
while true
do
 echo "Enter string:"
 read mInp
 mCnt=`echo $mInp | egrep -c '[a-zA-Z]'`
 if [ "$mCnt" = "1" ]; then
   echo "Found letters"
 fi
done
  #3 (permalink)  
Old 07-09-2007
kahuna's Avatar
kahuna kahuna is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 149
rsendhilmani,

This had me puzzled for a while, but I think I figured out what is wrong. I found this
Quote:
Because the transliteration table is built at compile time, neither the SEARCHLIST nor
the REPLACEMENTLIST are subjected to double quote interpolation. That means that
if you want to use variables, you must use an eval():
So the tr command is trying to translate the letters "$", "v", "a", and "l", not the value that $val contains.

Try
Code:
eval "\$count = (\$string =~ tr/$val//)";
Or use the s/// instead
Code:
$count = $string =~ s/$val//g;
  #4 (permalink)  
Old 07-09-2007
craigp84 craigp84 is offline
Registered User
  
 

Join Date: May 2007
Location: Glasgow, Scotland
Posts: 59
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
  #5 (permalink)  
Old 07-10-2007
rsendhilmani rsendhilmani is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 4
Hi,

Thanks a lot. It worked :-)
  #6 (permalink)  
Old 08-05-2008
bounsy bounsy is offline
Registered User
  
 

Join Date: Aug 2008
Location: Virginia, USA
Posts: 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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0