Appending char to strings by char 
Author Message
 Appending char to strings by char

: I am in need to:

: $field="welcome"

: i need it to become

: $field="WwEeLlCcOoMmEe" (Upper and Lower Case)

: Any ideas?

Here is something to try:

#! /usr/bin/perl
$field="welcome";
$new_field = "";
foreach $c (split(//,$field)) {
    $new_field = $new_field . uc($c) . $c;

Quote:
}

print "output: $new_field\n";

--
*********************************************************************
*  The Consultant's Curse:                                          *
*    When the customer has beaten upon you long enough, give him    *
*  what he asks for, instead of what he needs.  This is very strong *
*  medicine, and is normally only required once.                    *
*********************************************************************



Sun, 30 Aug 1998 03:00:00 GMT  
 Appending char to strings by char

Quote:
Diaz) writes:
> $field="welcome"

> i need it to become

> $field="WwEeLlCcOoMmEe" (Upper and Lower Case)

Perl borrows sed's very useful uppercase and lowercase regexp operators,  
you can make the next character uppercase with \u, the next character  
lowercase with \l (\U and \L operate until \E).  This way, we can just run  
a search/replace on word characters:

  #!/usr/bin/perl -w
  use strict;
  while(<STDIN>) {
    print;
    s/(\w)/\u$1\l$1/g;
    print;
  }

This will work for any incoming case (upper, lower, mixed ... note that  
another poster's response assumes lowercase input).  More info on the  
perlre(1) manual page.

Regards, Robert

--
Robert Seymour                       Swiss Bank Corporation

(SBCNet)

Statements herein may not reflect the policies of my employer (Swiss  
Bank).



Sun, 30 Aug 1998 03:00:00 GMT  
 Appending char to strings by char

Quote:

>$field="welcome"
>i need it to become
>$field="WwEeLlCcOoMmEe" (Upper and Lower Case)

Besides other choices given, you could could also do:

$field = join '', map {uc($_), $_} split '', $field;
--
Andrew Langmead



Sun, 30 Aug 1998 03:00:00 GMT  
 Appending char to strings by char
: I am in need to:

: $field="welcome"

: i need it to become

: $field="WwEeLlCcOoMmEe" (Upper and Lower Case)

Here is yet another way:

$field =~ s/(.)/uc($1).$1/eg;

--
"You can do this in a number of ways.  IBM chose to do all of them.
Why do you find that funny?"
                -- D. Taylor, Computer Science 350



Mon, 31 Aug 1998 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. reading char by char in a string

2. Char position of 1st non-word char in a string

3. @chars the same as ($#chars) ?

4. Replacing special chars with special chars

5. Q: using a c-lib with char** and even char***

6. s/Hex/Char/ ? s/Char/Hex/

7. Why no regex char class for punc chars???

8. Replacing multiple occurences of newline chars with a single newline char

9. Match any char EXCEPT [char]

10. Char-by-char client/server with echo option!?

11. Char-by-char in internet server?

12. split a string not only by a single char but also by a string

 

 
Powered by phpBB® Forum Software