Re: Case conversion
by Zaxo (Archbishop) on Sep 01, 2005 at 15:21 UTC
|
$string =~ tr/AEIOUaeiou/aeiouAEIOU/;
There are lots of ways to do it.
| [reply] [d/l] |
|
|
"A, E, I, O, U, and sometimes Y":
if (int rand(2))
{
$string =~ tr/AEIOUaeiou/aeiouAEIOU/;
}
else
{
$string =~ tr/AEIOUYaeiouy/aeiouyAEIOUY/;
}
:-)
| [reply] [d/l] |
|
|
Why type y/a-zA-Z/A-Za-z/ when you can type my $inverted = join '', map {$_ = uc eq $_ ? lc : uc} split //, $input;
| [reply] [d/l] [select] |
|
|
Well,
- it was about vowels,
- 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;
| [reply] [d/l] |
Re: Case conversion
by davido (Cardinal) 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.
| [reply] |
|
|
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. ;-)
| [reply] |
|
|
| [reply] |
Re: Case conversion
by ikegami (Patriarch) 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).
| [reply] |
Re: Case conversion
by blazar (Canon) on Sep 01, 2005 at 15:23 UTC
|
| [reply] [d/l] [select] |
|
|
Doesn't work, but close. Solution:
s/[aeiou]/$&^"\x20"/gei
And you used $&? ouch!
s/([aeiou])/$1^"\x20"/gei
| [reply] [d/l] [select] |
|
|
Doesn't work, but close. Solution:
s/[aeiou]/$&^"\x20"/gei
Ouch! Just forgot that...
And you used $&? ouch!
s/([aeiou])/$1^"\x20"/gei
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)".
| [reply] [d/l] [select] |
|
|
|
|