Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Case conversion

by anniyan (Monk)
on Sep 01, 2005 at 15:15 UTC ( #488396=perlquestion: print w/ replies, xml ) Need Help??
anniyan has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Today i went through some perl question papers, which has been set for interviews.

There they have asked to convert the capital vowels into small case and small case vowels into capital.

I think there might be some modules, but is it possible to do without modules? If so please explain.

I tried using hash, other than that is there anyway?

input: $a= 'abcIdeOu'; output: AbcidEoU


Comment on Case conversion
Download Code
Replies are listed 'Best First'.
Re: Case conversion
by Zaxo (Archbishop) on Sep 01, 2005 at 15:21 UTC

    Here's a simple one,

    $string =~ tr/AEIOUaeiou/aeiouAEIOU/;
    There are lots of ways to do it.

    After Compline,

      Why type y/a-zA-Z/A-Za-z/ when you can type my $inverted = join '', map {$_ = uc eq $_ ? lc : uc} split //, $input;
        1. it was about vowels,
        2. also, many people use split like that, but I find that a simple match (in list context) is better suited. In this case, with reference to your example:
          my $inverted = join '', map { whatever } $input =~ /./g;
Re: Case conversion
by davido (Archbishop) on Sep 01, 2005 at 15:24 UTC

    Yes, it's possible, but if you got the job because I told you how to do it, your employer and you would both have been done an injustice.

    I will tell you where to do your own research though: Look in perlop. Also look in perlfunc under the "Functions for SCALARS or Strings" heading.

    It's all there.


      Under the theory that it's not knowing that counts, but being able to find the information that matters, the OP knew to look on PM, and, indeed, did find the answer. Using the same methodology for more obscure pieces of information, the OP is more likely than non-monks to be able to find what they need, and quickly. So it doesn't seem to be an injustice to me. ;-)

        Not funny
Re: Case conversion
by ikegami (Pope) on Sep 01, 2005 at 18:34 UTC

    What about "é"? That's a vowel...

    I wonder if interviewers give extra credit if you mention assumptions such as "Doesn't work for accented vowels" and "Only works on ASCII and ASCII-based character sets" (for blazar's solution).

Re: Case conversion
by blazar (Canon) on Sep 01, 2005 at 15:23 UTC

      Doesn't work, but close. Solution:

      And you used $&? ouch!

        Doesn't work, but close. Solution:
        Ouch! Just forgot that...
        And you used $&? ouch!
        Well, this comes out too often!! Most of times it's about the performance penalty. As far as the latter goes, perlre specifies clearly that it's the same as that involved in capturing and using numbered vars. So it's not a problem for me if I have to use what I matched in any case. But then the other common objection is that it's just plainly ugly. Then I reply that beauty is in the eye of the beholder, and I don't find it to be quite that disturbing.

        Granted: a variable name like $& is not that beautiful either, and IMHO it doesn't convey the psychological feeling of referring to what has been matched (Hopefully Perl6 will be better at this, but we still have to wait...) - as a side note there are situations, albeit not common ones, in which I'd like it to be $_: it's the topicalizer, and I say "match this, then substitute it with that (possibly depending on it)".

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://488396]
Approved by VSarkiss
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2015-11-26 16:21 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (701 votes), past polls