12 Replies - 19085 Views - Last Post: 22 March 2006 - 06:36 PM

#1 ktsirig   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 98
  • Joined: 14-September 05

Search String for Vowels

Posted 31 January 2006 - 05:35 AM

Hi all!
My question is rather silly, but I can't seem to be able to find the solution myself...

I have the array:
@letters = (A,B,C,D,E,F);

and the string:
$string = "AFTYUBEWTWECRTUTYIYTDDDDRYJURTHJTREEEEEFGSDFF";

and I want to find how many times each letter appears in the $string.

I go like:
foreach $a(@letters)
{
$count = ($string = ~tr/$a//);
print $count;
}


But it doesn't count anything...
Does it have to do with the $a in the pattern matching?
I tested the same thing and instead of $a I put A or B etc and it works...

Is This A Good Question/Topic? 0
  • +

Replies To: Search String for Vowels

#2 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Search String for Vowels

Posted 31 January 2006 - 09:01 AM

Do you have to use an array, or can you just use a defined regular expression?

This demonstrates how to find all matches within a given string....you can modify the code to give you the count of matches.
Was This Post Helpful? 0
  • +
  • -

#3 Voodoo Doll   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 108
  • Joined: 24-January 06

Re: Search String for Vowels

Posted 31 January 2006 - 09:24 AM

$count = ($string = ~tr/$a//);

tr is a compile time operator and doesn't interpolate, so you can't use a variable in it outside of an eval. Sometimes it's smarter to just do things the obvious way, and optimize with a more obscure solution if the obvious way turns out to be too slow. :)
use strict;

my $base = "AFTYUBEWTWECRTUTYIYTDDDDRYJURTHJTREEEEEFGSDFF";
my @find = ('A','B','C','D','E','F');
my %freq = ();

foreach my $findc (@find) {
  my $count = 0;

  foreach my $basec (split(//, $base)) {
    if ($basec eq $findc) {
      ++$count;
    }
  }  

  $freq{$findc} = $count;
}

foreach my $key (sort keys %freq) {
  print $key, " -- ", $freq{$key}, "\n";
}


Was This Post Helpful? 0
  • +
  • -

#4 dorknexus   User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1272
  • View blog
  • Posts: 4,625
  • Joined: 02-May 04

Re: Search String for Vowels

Posted 31 January 2006 - 07:46 PM

the part where you know c++ AND perl makes me wanna.....something bad, real bad.

don't worry amadeus, i still love you.
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Search String for Vowels

Posted 31 January 2006 - 07:53 PM

Sorry, Nexus, what didn't work? Using a regular expression to find vowel occurences? I haven't coded it (just a suggestion), but I could give it a shot...
Was This Post Helpful? 0
  • +
  • -

#6 dorknexus   User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1272
  • View blog
  • Posts: 4,625
  • Joined: 02-May 04

Re: Search String for Vowels

Posted 31 January 2006 - 07:56 PM

EDIT!! worry*

pfft, the day you post something that doesn't work is the day i leap in front of a bus.
Was This Post Helpful? 0
  • +
  • -

#7 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Search String for Vowels

Posted 31 January 2006 - 08:08 PM

Well, thank you for the vote of confidence, my friend, but just for the record:
$string = "asdjopqiweuralsk;djfouwerx,.mncvqopiwuesdfoqiweuraapoutre";
$count = 0;
while ($string =~ m/[aeiou]/g) {
  $count=$count+1;
}
print "There are ".$count." lower case vowels in the string\n";


will find the number of lower case vowel occurences in the string.
Was This Post Helpful? 0
  • +
  • -

#8 Voodoo Doll   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 108
  • Joined: 24-January 06

Re: Search String for Vowels

Posted 31 January 2006 - 09:08 PM

Amadeus, on 1 Feb, 2006 - 12:05 AM, said:

Well, thank you for the vote of confidence, my friend, but just for the record:
$string = "asdjopqiweuralsk;djfouwerx,.mncvqopiwuesdfoqiweuraapoutre";
$count = 0;
while ($string =~ m/[aeiou]/g) {
  $count=$count+1;
}
print "There are ".$count." lower case vowels in the string\n";


will find the number of lower case vowel occurences in the string.

That's kind of confusing. How about removing any non-vowel characters from the string and then printing the length?
$string = "asdjopqiweuralsk;djfouwerx,.mncvqopiwuesdfoqiweuraapoutre";
$string =~ s/[^aeiou]//g;
print "Total vowels: ", length($string);


Then you can add more power by letting the user give you a pattern to match so that it's more than just a vowel counter.
$string = "alweiyropanievgmawmoaAWEROICVJWWOETAiaweurociuaopxiaedmxta";
$pattern = shift @ARGV;
$string =~ s/[^$pattern]//g;
print "Total matching characters: ", length($string);


:)
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Search String for Vowels

Posted 01 February 2006 - 08:21 AM

Well, I'm not sure that I agree that it is confusing, but I do agree that it can certainly be made more than a simple vowel counter. However, since the user requirements specified a vowel counter, I supplied him/her with a technique that illustrates how to do that. The user should then be able to take that technique, and modify it for their purposes.

As with any code snippet, it can be optimized and structured to take variables to improve flexibility (which you have shown admirably), and I wholeheartedly agree that users should do so in every case possible...that is just good coding practice. However, if I simply supply them with an optimized and versatile version, they may not become aware of the types of steps usually involved in such procedures.

All in all, I agree with your assessment that it can be optimized, and had I been writing it for an application or script I was developing, I certainly would have done so. It is certainly not the most efficient way to accomplish the task in it's current form. :)
Was This Post Helpful? 0
  • +
  • -

#10 Voodoo Doll   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 108
  • Joined: 24-January 06

Re: Search String for Vowels

Posted 01 February 2006 - 11:17 AM

Quote

However, since the user requirements specified a vowel counter, I supplied him/her with a technique that illustrates how to do that.

Unfortunately, the thread topic talks about searching for vowels, but the example code clearly wanted to search for a pattern derived from an array. I think it's safer to assume a generic solution rather than tailoring it to vowels alone. :)

Quote

However, if I simply supply them with an optimized and versatile version, they may not become aware of the types of steps usually involved in such procedures.

I hope you don't mind me politely disagreeing with you. :) The optimized and versatile solution conveniently falls into your lap in the case that you were describing. To understand the types of steps that go into it would involve dropping down to a low level implementation like C because the component parts are all fundamental Perl operations. So by turning an expression into a loop that uses a variant of the expression, you confuse people with the unnecessary complexity. On top of that, I have trouble believing that anyone remotely familiar with Perl wouldn't get a simple use of s// and length(). ;)

Quote

I agree with your assessment that it can be optimized

Please don't take this the wrong way, because I can't think of anything to soften it. :cry: But it isn't that I think it can be optimized as much as I thought your solution was inappropriate.
Was This Post Helpful? 0
  • +
  • -

#11 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Search String for Vowels

Posted 01 February 2006 - 11:53 AM

Voodoo Doll, on 1 Feb, 2006 - 02:14 PM, said:

I hope you don't mind me politely disagreeing with you. :)

I certainly do not mind, in any way shape or form...nor would I mind if you rudely disagreed with me :). My opinions are just that...and are no more or less valid than anyone's.

My opinions are developed, like everyone's, from a period of working with the things in question...I feel they convey the proper message (otherwise I would not offer them), but that by no means suggests that everyone should feel that way. Each person should make choices based on what they judge to best for them, espcially as far as teaching goes.

In any case, I definitely have no problems with you (or anyone) disagreeing with me.

Quote

Please don't take this the wrong way, because I can't think of anything to soften it.  But it isn't that I think it can be optimized as much as I thought your solution was inappropriate.

Again, I have no problem with that at all...like myself, and everyone, your opinions are valid.

All I ask is that the same respect is returned...not everyone sees things the same way, and we all have different methods of teaching others, emthods and techniques that we consider valid (and perhaps maybe even used in an academic setting)...if we all work together, I feel the site members will benefit.
Was This Post Helpful? 0
  • +
  • -

#12 Voodoo Doll   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 108
  • Joined: 24-January 06

Re: Search String for Vowels

Posted 01 February 2006 - 11:58 AM

In that case, let's just say I was adding another alternative to the discussion and let everything work together to add to the value of the thread. :)
Was This Post Helpful? 0
  • +
  • -

#13 Jayen   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 04-May 04

Re: Search String for Vowels

Posted 22 March 2006 - 06:36 PM

If I am not too late - this will work--


@letters = (A,B,C,D,E,F);

$string = "AFTYUBEWTWECRTUTYIYTDDDDRYJURTHJTREEEEEFGSDFF";


foreach $a(@letters)
{

$count = ($string =~ s/$a/$a/g);
print $a . $count;
}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1