#!/usr/bin/perl
#===============================================================================
#
# FILE: test2.pl
#
# USAGE: ./test2.pl
#
# DESCRIPTION: Weekly Test2: The script has a paragraph defined at it's start.
# The script reads the each word of the paragraph and processes it
# to check following conditions:
#
# 1. has an 'a'
# 2. starts with an 'a'
# 3. has 'th'
# 4. has an 'a' or an 'A'
# 5. has a '*' in it
# 6. starts with an 'a' or an 'A'
# 7. has both 'a' and 'e' in it
# 8. has an 'a' followed by an 'e' somewhere in it
# 9. does not have an 'a'
# 10. does not have an 'a' nor 'e'
# 11. has an 'a' but not 'e'
# 12. has at least 2 consequtive vowels (a,e,i,o,u)
# 13. has at least 3 vowels
# 14. has at least 6 characters
# 15. has at exactly 6 characters
# 16. all the words with either 'aba' or 'ada' in them
# 17. all the words with either 'aba' or 'eda' in them
# 18. has a double character (e.g. 'oo')
# 19. for every word print the first vowel
#
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Parag Kalra, paragkalra@gmail.com
# COMPANY: PERSISTENT SYSTEMS LTD, http://www.persistentsys.com/
# VERSION: 1.0
# CREATED: 3/19/2009 2:11:51 PM
# REVISION: ---
#===============================================================================
use Acme::Comment;
# Declaring the Paragraph
$mypara="Finish each day and be done with it. You have done what you could. Some blunders and absurdities no doubt crept in; forget them as soon as you can. Tomorrow is a new day; begin it well, with no pre-conceived notions and with too high a spirit so as to be able to hold back your old stuff. India is great. I love star*star. Vowels combination: axxiyywwrroo arixeupoq abajan abaada adamant abacuss edaiot Apple MAP Axe Gooogle PEANUT PqRs AIm";
# Spliting the string by space characater to store the words of the para into an array.
@para_words=split /\s+/, $mypara;
@vowels=qw/a e i o u/;
#---------------------------------------------------------------------------
# Processing each word of the array with regular expressions.
#---------------------------------------------------------------------------
foreach ( @para_words ) {
#print "Processing: $_ \n";
# 1
# Words containing 'a'
if ( $_ =~ m{a+} ) {
push @words_with_a, $_;
# Words containing 'a' but not 'e'
# As of now not working
#if ( $_ =~ m{[^e]} ) {
#push @words_having_a_not_e, $_;
#}
}
# 3
# Words having 'th'
if ( $_ =~ m{(th)+} ) {
push @words_with_th, $_;
}
# 4
# Words having either 'a' or 'A'
if ( $_ =~ m{a+}i ) {
push @words_with_Aa, $_;
}
# 5
# Words having star
if ( $_ =~ m{\*} ) {
push @words_with_star, $_;
}
# 6
# Words starting with 'a' or 'A'
if ( $_ =~ m{^a+}i ) {
push @words_starting_with_Aa, $_;
# 2
# Words starting with 'a'
if ( $_ =~ m{^a+} ) {
push @words_starting_with_a, $_;
}
}
# 7
# Words containing both 'a' and 'e'
if ( $_ =~ m{e+} && $_ =~ m{a}) {
push @words_having_both_ae, $_;
}
# 8
# Words in which 'a' is followed by 'e'
if ( $_ =~ m{(ea)+} ) {
push @words_having_a2e, $_;
}
# 9
# Words which doesnot have 'a'
# As of now not working with carat
if ( $_ !~ m{a} ) {
push @words_without_a, $_;
}
# Multiline comment
/*
if ( $_ !~ m{a+} ) {
push @words_without_a, $_;
if ( $_ !~ m{e+} ) {
push @words_without_a_e, $_;
}
}
*/
# 10
# Words except 'a' and 'e'
if ( $_ !~ m{e+} && $_ !~ m{a+} ) {
push @words_without_a_e, $_;
}
# 11
# Words having 'a' not having 'e'
if ( ($_ !~ m{e}) && ($_ =~ m{a}) ) {
push @words_having_a_not_e, $_;
}
# 12
# Words having 2 consecutive vowels
$var=$_;
foreach ( @vowels ) {
$v=$_;
#print "Current vowel: $v";
for ( $i=0; $i<5 ; $i++ ) {
#print "$v$vowels[$i] \n";
#if ( $var =~ /(($v){1})($vowels[$i]{1})/ ) {
# Matching the pattern for consecutive vowels
if ( $var =~ /$v$vowels[$i]/i ) {
#print "$var \n";
push @words_having_2_con_vowels, $var;
}
}
}
# 13
# Words containing at least 3 vowels
if (tr/aeiouAEIOU/aeiouAEIOU/ >= 3 ) {
push @words_having_3_vowels, $_;
}
# Pattern for matching 3 vowels
#for ( $p=0; $p<5 ; $p++ ) {
#for ( $q=0; $q<5 ; $q++ ) {
#for ( $r=0; $r<5 ; $r++ ) {
#if ( $var =~ /^(.*)($vowels[$p])(.*)$vowels[$q](.*)$vowels[$r](.*)$/ ) {
#if( grep $var, @words_having_3_vowels){
#push @words_having_3_vowels, $var;
#}
#}
#}
#}
#}
# Pattern for matching 3 consecutive vowels
# As of now under-construction
#if ( $_ =~ /[aeiou]{3,}/ ) {
# push @words_having_3_vowels, $_;
#}
# 14
# Words having at least 6 characters
if ( $_ =~ /(.){6,}/ ) {
push @words_with_atl_6_chs, $_;
}
# 15
# Words having exactly 6 characters
# As of now not working for '-'
#if ( $_ =~ /\b(.){6}\b/ ) {
if ( $_ =~ /^(.){6}$/ ) {
push @words_with_6_chs, $_;
}
# 16
# Words containing either 'aba' or 'ada'
if ( $_ =~ m{(aba)|(ada)}) {
push @aba_ada, $_;
}
# 17
# Words containing either 'aba' or 'eda'
if ( $_ =~ m{(aba)|(eda)}) {
push @aba_eda, $_;
}
# 18
# Words having identical adjoining characters
if ( $_ =~ /(.)\1/ ) {
push @words_with_2_chs, $_;
}
# 19
#---------------------------------------------------------------------------
# Printing the word and it's first vowel if at all it contains
#---------------------------------------------------------------------------
$mystring = $_;
#@chars = map substr( $mystring, $_, 1), 0 .. length($mystring) -1;
@chars = unpack('a'x length($mystring), $mystring);
if($mystring !~ /[aeiou]/i){
print "'$mystring' has no vowel \n";
} else {
foreach(@chars){
#print "$_ \n";
if($_ =~ /[aeiou]/i){
print "First vowel of '$mystring': $_ \n";
last;
}
}
}
}
print "\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "Words containing 'a': @words_with_a \n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "Words starting with 'a': @words_starting_with_a \n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "Words containing 'th': @words_with_th \n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "Words containing either 'A' or 'a': @words_with_Aa \n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "Words containing star: @words_with_star \n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "Words starting with 'A' or 'a': @words_starting_with_Aa \n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "Words containing both 'a' and 'e': @words_having_both_ae \n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "Words in which 'a' is followed by 'e': @words_having_a2e \n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "Words which doesn't have 'a': @words_without_a \n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "Words except 'a' and 'e': @words_without_a_e\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "Words having 'a' but not 'e': @words_having_a_not_e \n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "Words having 2 consecutive vowels: @words_having_2_con_vowels \n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "Words having at least 3 vowels: @words_having_3_vowels \n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "Words having at least 6 characters: @words_with_atl_6_chs \n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "Words having exactly 6 characters: @words_with_6_chs \n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "Words having 'aba' and 'ada': @aba_ada \n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "Words having 'aba' and 'eda': @aba_eda \n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "Words having 2 similar and adjoint characters: @words_with_2_chs\n";
print "----------------------------------------------------------------------------------------------------------------------------------------\n";
print "\n";
Saturday, March 21, 2009
Subscribe to:
Comments (Atom)
