Re: uc() every other letter
by echo (Pilgrim) on Sep 28, 2001 at 13:26 UTC
|
%lcase = (a..z);
@upcase = map { uc $_, $lcase{$_} } sort keys %lcase;
| [reply] [d/l] |
|
Oooh. Bonus points for the obfuscated use of a hash to break out the alternate letters!
| [reply] |
|
Obfuscated? You are too kind. You got me thinking though...
@upcase = map { chr(ord() ^ ((ord() & 1) << 5)) } (a..z);
| [reply] [d/l] |
Re: uc() every other letter
by davorg (Chancellor) on Sep 28, 2001 at 13:38 UTC
|
my @alpha = ('a' .. 'z');
@alpha = map { $_ % 2 ? uc $alpha[$_] : $alpha[$_] } 0..25;
--
<http://www.dave.org.uk>
"The first rule of Perl club is you don't talk about
Perl club."
| [reply] [d/l] |
Re: uc() every other letter
by jmcnamara (Monsignor) on Sep 28, 2001 at 13:43 UTC
|
my @lcase = ('a'..'z');
my @upcase = map { ord() % 2 ? uc() : $_ } @lcase;
jOhn
--
| [reply] [d/l] |
Re: uc() every other letter
by tachyon (Chancellor) on Sep 28, 2001 at 14:41 UTC
|
my $t = 0;
for( a..z ){ $t ^= 1; push @ary, $t ? $_ : uc $_ }
print @ary
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
| [reply] [d/l] |
Re: uc() every other letter
by Sifmole (Chaplain) on Sep 28, 2001 at 16:16 UTC
|
If you really want to upcase only A's, C's, E's etc. You could use the tr/// construct.
my $word = "The quick brown fox";
$word =~ tr/acegikmoqsuwy/ACEGIKMOQSUWY/;
| [reply] [d/l] [select] |
Re: uc() every other letter
by George_Sherston (Vicar) on Sep 28, 2001 at 15:02 UTC
|
How about a generic solution with a regex? Turn @lcase into a string, then
$lcase =~ s/(\w{1})(\w{1})/{$1.uc$2}/eg;
§ George Sherston | [reply] [d/l] [select] |
|
simpler:
$lcase =~ s/(\w)(\w)/\l$1\u$2/g
-- stefp
| [reply] [d/l] |
|
Smart! Every day I learn something. Hmm. And if it's short we want, one might go as far as s/(.)(.)/$1\u$2/g
§ George Sherston
| [reply] [d/l] |
Re: uc() every other letter
by Aristotle (Chancellor) on Sep 28, 2001 at 14:46 UTC
|
my @mixed_case = 'a' .. 'z';
my $other_letter = 0;
$_ = uc() for grep ++$other_letter % 2, @mixed_case;
| [reply] [d/l] |
|
MiNi kRaZy golf on the modulus theme:
my $i=0;
my @a = ('a'...'z');
$i++%2||($_=uc)for@a;
| [reply] [d/l] |
|
I can trim one char off of that....
$i++%2or$_=uc for@a;
-Blake
| [reply] [d/l] |
Re: uc() every other letter
by runrig (Abbot) on Sep 28, 2001 at 20:25 UTC
|
$_ = uc for @array[grep { not $_ % 2 } 0..$#array];
| [reply] [d/l] |
Re: uc() every other letter
by Ven'Tatsu (Deacon) on Sep 28, 2001 at 22:28 UTC
|
@lcase = (a..z);
@mixcase = map { ($f = !$f) ? uc : lc } @lcase;
| [reply] [d/l] |
Re: uc() every other letter
by thunders (Priest) on Sep 28, 2001 at 18:49 UTC
|
I would use Sifmole's solution
but here's a fairly silly way to do it.
for(97..122){
$init = chr; $rep = uc($init);
eval '$string =~'."s/$init/$rep/g" if $_ % 2
}
| [reply] [d/l] |
(dkubb) Re: (1) uc() every other letter
by dkubb (Deacon) on Sep 29, 2001 at 13:42 UTC
|
| [reply] [d/l] [select] |
|
$|--or$_=uc for@a;
-Blake
| [reply] [d/l] |
Re: uc() every other letter
by dcd (Scribe) on Sep 29, 2001 at 02:10 UTC
|
perl -le '@l=("a".."z"); $_=join("",@l);
s/../\u$&/g;
print'
When the number of leters is even, the ucfirst /\u/
will work on pairs of numbers matched above.
if the length of the string was odd, the last character
could be handled as a special case.
Seeing some of the other responses leads me to think
that the question was not clearly stated
what kind of input,
was it a mapping on a per character basis, (where tr could help)
etc.. | [reply] [d/l] |
Re: uc() every other letter
by Aitvo (Initiate) on Sep 29, 2001 at 01:39 UTC
|
@lcase=(a..z);
foreach $item(@lcase)
{
if ($sw =~/1/)
{
push (@case,lc($item));
$sw=0;
}
else
{
push (@case,uc($item));
$sw=1;
}
}
print @case; | [reply] |
Re: uc() every other letter
by Anonymous Monk on Sep 29, 2001 at 19:58 UTC
|
--snip--
@lcase = (a..z);
@other = ();
for ($i = 0; $i <= $#lcase; $i++) {
if ($i % 2) {
@other[$i] = uc($lcase[$i]);
}
else {
@other[$i] = $lcase[$i];
}
}
print "lcase:";
foreach $l (@lcase) { print " $l"; }
print "\n";
print "other: ";
foreach $o (@other) { print " $o"; }
print "\n";
--snip--
gives these results:
$ perl asdsda.txt
lcase: a b c d e f g h i j k l m n o p q r s t u v w x y z
other: a B c D e F g H i J k L m N o P q R s T u V w X y Z
hope that helps.
| [reply] [d/l] |
Re: uc() every other letter
by cricket (Acolyte) on Sep 30, 2001 at 01:57 UTC
|
use strict;
my @lc = ('a'..'z');
my (@uc, $counter);
for my $letter (@lc) {
$counter++;
if ($counter % 2) {
$letter = uc($letter);
}
push (@uc, $letter);
}
print "@uc" . "\n";
| [reply] [d/l] |