Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

uc() every other letter

by Anonymous Monk
on Sep 28, 2001 at 13:07 UTC ( [id://115342]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How would I uppercase every other letter in the alphabet?
@lcase = (a..z); @upcase = map { uc() } @lcase;
But I don't want to uppercase all the letters I only want: AbCdEfG

Replies are listed 'Best First'.
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;
      Oooh. Bonus points for the obfuscated use of a hash to break out the alternate letters!
        Obfuscated? You are too kind. You got me thinking though...

        @upcase = map { chr(ord() ^ ((ord() & 1) << 5)) } (a..z);
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."

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
    --

Re: uc() every other letter
by tachyon (Chancellor) on Sep 28, 2001 at 14:41 UTC

    Or with a XOR toggle:

    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

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/;
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
      simpler:
      $lcase =~ s/(\w)(\w)/\l$1\u$2/g

      -- stefp

        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
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;
      MiNi kRaZy golf on the modulus theme:
      my $i=0; my @a = ('a'...'z'); $i++%2||($_=uc)for@a;
        I can trim one char off of that....
        $i++%2or$_=uc for@a;

        -Blake

Re: uc() every other letter
by runrig (Abbot) on Sep 28, 2001 at 20:25 UTC
    Just for variety:
    $_ = uc for @array[grep { not $_ % 2 } 0..$#array];
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;
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 }
Re: uc() every other letter
by jdporter (Paladin) on Mar 14, 2024 at 17:57 UTC
    @lcase = ('a' .. 'y'); @upcase = sub { local $_ = join '', @_; s/(.{1,2})/ ucfirst($1) /ge; s +plit }->( @lcase );

    Of course, you could make this a stand-alone named sub if you expect to reuse it.

(dkubb) Re: (1) uc() every other letter
by dkubb (Deacon) on Sep 29, 2001 at 13:42 UTC

    Here's one using the "magical" $| variable:

    my @mixed_case = map --$| ? uc : $_, a..z;
      Nice, why didn't I think of that...
      golf + obfu + toggles = --$|

      So, we have a new leader in our golf game... 17 chars

      $|--or$_=uc for@a;

      -Blake

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..
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;
Re: uc() every other letter
by Anonymous Monk on Sep 29, 2001 at 19:58 UTC
    this code:
    --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.
Re: uc() every other letter
by cricket (Acolyte) on Sep 30, 2001 at 01:57 UTC
    i'm a lousy golfer...
    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";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://115342]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-19 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found