Ask a Question related to PERL Beginners, Design and Development.
-
Bis #1
Changing case of the first letter of words in string
I want to make the case of the first letter of all the words in a
selected string to upper case. The code
s/\b(\w+)/\u$1\E/g;
enables me to do this for the whole document.
But the string I want to match and operate on is all instances of
text following the string
SCTN:
as in
SCTN: News Analysis
or
SCTN: Special Report
But when I try
s/(SCTN:\s*)\b(\w+)/$1\u$2\E/g;
nothing seems to change? : (
Bis
--
Bis Guest
-
Changing from half letter to letter document size
I have a document that was created in the size it will be (5.5" x 8.8") and it will be punched on the spine side. It has a slightly larger margin on... -
No Pcase? how do I capitolize the first letter of aword, with the rest of the word lower case?
I know I can go one way or another, Ucase(var) all upppercase, or Lcase(var) all lowercase, but how can I get a Propercase? I've seen this in VB,... -
opening a file whose letter case is unknown
Say we have a file named "ABC" , yet A B and C can be upper or lower case each ( 8 combinations ). how can one neatly open this file without... -
String question: Returning portion of string with words surrounding highlighted search term?
I'm looking to find or create an ASP script that will take a string, examine it for a search term, and if it finds the search term in the string,... -
How to pick out words from a non-delimited string?
Hi, Since Perl excels at string parsing, I thought this would be the right group to ask my question. If not, I apologize for the confusion. I... -
Gabriel Cooper #2
Re: Changing case of the first letter of words in string
Bis wrote:
My guess (without actually trying) is the word boundary marker (\b).> I want to make the case of the first letter of all the words in a
> selected string to upper case. The code
> s/\b(\w+)/\u$1\E/g;
> enables me to do this for the whole document.
> But the string I want to match and operate on is all instances of text
> following the string
> SCTN:
> as in
> SCTN: News Analysis
> or
> SCTN: Special Report
> But when I try
> s/(SCTN:\s*)\b(\w+)/$1\u$2\E/g;
> nothing seems to change? : (
> Bis
Since you're grabbing all preceding whitespace you can't (in theory)
expect it to find whitespace before your word. So... try this:
s/(SCTN:\s*)(\w+)/$1\u$2\E/g;
Gabriel Cooper Guest
-
David K. Wall #3
Re: Changing case of the first letter of words in string
Bis <biswadip.dasgupta@centaur.co.uk> wrote:
That will only change the first letter of the first word after 'SCTN:'.> I want to make the case of the first letter of all the words in a
> selected string to upper case. The code
>
> s/\b(\w+)/\u$1\E/g;
>
> enables me to do this for the whole document.
>
> But the string I want to match and operate on is all instances of text
> following the string
>
> SCTN:
>
> as in
>
> SCTN: News Analysis
>
> or
>
> SCTN: Special Report
>
> But when I try
>
> s/(SCTN:\s*)\b(\w+)/$1\u$2\E/g;
>
> nothing seems to change? : (
That's what you're seeing, right? If you want to change *all* the words
after SCTN: to start with uppercase (and leave the ones before it alone),
maybe something like this:
my $text = q(leave this alone SCTN: this isn't capitalized but should be);
if (/SCTN:/) {
my ($before, $after) = split /SCTN:/, $text, 2;
$after =~ s/(\S+)/\u$1/g;
$text = $before . 'SCTN:' . $after;
}
print $text;
I used \S instead of \w in an attempt to handle contractions such as
"can't" and "don't", etc.
Now I'd almost bet someone (John Krahn?) will come up with a more clever
approach and make it into a one-liner. :-)
David K. Wall Guest
-
John W. Krahn #4
Re: Changing case of the first letter of words in string
Bis wrote:
>
> I want to make the case of the first letter of all the words in a
> selected string to upper case. The code
>
> s/\b(\w+)/\u$1\E/g;
>
> enables me to do this for the whole document.
>
> But the string I want to match and operate on is all instances of
> text following the string
>
> SCTN:
>
> as in
>
> SCTN: News Analysis
>
> or
>
> SCTN: Special Report
>
> But when I try
>
> s/(SCTN:\s*)\b(\w+)/$1\u$2\E/g;
>
> nothing seems to change? : (
s{^SCTN:\s*(.*)}{ ($a = $1) =~ s[(\w+)][\u$1]g; $a }e;
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
James Edward Gray II #5
Re: Changing case of the first letter of words in string
On Friday, August 22, 2003, at 05:06 PM, bis wrote:
Well, let's see if we can get a little closer:> Thanks Gabriel - your suggested code
>
> s/(SCTN:\s*)(\w+)/$1\u$2\E/g;
>
> is an improvement - it does capitalise the first
> letter - but only of the first word after "SCTN: " so
> i get something like
>
> SCTN: This is a section name
>
> What I need is
>
> SCTN: This Is A Section Name
>
> hope that makes sense! :)
s/(SCTN:\s*)(.+)$/join '', $1, map { ucfirst $_ } split /( )/, $2/ge;
See if that helps any.
James
James Edward Gray II Guest
-
Zsdc #6
Re: Changing case of the first letter of words in string
*Please* CC the mailing list when you answer. I'm writing it so everyone
on the list could learn something, not just to fix your program or solve
your particular problem.
bis wrote:
No, it doesn't. Only the lines containing "SCTN:" Have you run it?> Tnaks zsdc and JEGII.
>
> --- zsdc <zsdc@wp.pl> wrote:
>>>>It gives a syntax error. Maybe try this:
>>
>> #!/usr/bin/perl -p
>> s/(\s)(.)/$1\u$2/g if/SCTN:/;
> This capitalises the first letter of every word in the
> whole document.
Well... Yes, it does. How did you run it, anyway?>>>or instead of /SCTN:/ use /^SCTN:/
> This doesn't do anything.
>
> or /^\s*SCTN:/
>
> Nor does this.
This is much harder. It's easy to make the output like this:> I think the title of my query was misleading - what I
> maybe should have said was "Uppercasing the first
> letter and lowercasing all the other letters of every
> word in a mixed case part of a string"?
I'Ve Been Reading O'Reilly Books
OR:
I've Been Reading O'reilly Books
but not:
I've Been Reading O'Reilly Books
which is correct. I suggest you to only uppercase characters, but if you
have to also lowercase the other ones then try:
#!/usr/bin/perl -p
s/(\s)(\S+)/$1\L\u$2/g if/^\s*SCTN:/;
or
#!/usr/bin/perl -p
s/\b(\w+)/\L\u$1/g if/^\s*SCTN:/;
(and maybe add:
use locale;
at the beginning) and see which suits your needs better. You might find
better examples in the Cookbook.
-zsdc.
Zsdc Guest
-
Bis #7
Re: Changing case of the first letter of words in string
--- zsdc <zsdc@wp.pl> wrote: > *Please* CC the mailing
list when you answer. I'mthanks for the reminder zsdc - i should have> writing it so everyone
> on the list could learn something, not just to fix
> your program or solve
> your particular problem.
>
remembered to cc the list when replying to you.
yes i have run it and below is the kind of output i> bis wrote:
>> the> > Tnaks zsdc and JEGII.
> >
> > --- zsdc <zsdc@wp.pl> wrote:
> >> >> >>It gives a syntax error. Maybe try this:
> >>
> >> #!/usr/bin/perl -p
> >> s/(\s)(.)/$1\u$2/g if/SCTN:/;
> > This capitalises the first letter of every word in>> > whole document.
> No, it doesn't. Only the lines containing "SCTN:"
> Have you run it?
get (original input all lower case except capitalised
tags and for SCTN: line which is mixed case):
DOCB:
SRCE: Marketing Week
SCTN: Special Report:pRoMotIons And Incentives
PGNO: 5
HDLN: What
TEXT:
This Is Text This Is Text This Is Text These Are
Double Spaces This Is Text This Is Text This Is Text
These Are Double Spaces
This Is Text This Is Text This Is Text These Are
Double Spaces
This Is Text This Is Text This Is Text These Are
Double Spaces
This Is Text This Is Text This Is Text These Are
Double Spaces
DOCE:
DOCB:
SRCE: Marketing Week
SCTN: Special Report:pRoMotIons And Incentives
PGNO: 5
HDLN: What
TEXT:
This Is Text This Is Text This Is Text These Are
Double Spaces This Is Text This Is Text This Is Text
These Are Double Spaces
This Is Text This Is Text This Is Text These Are
Double Spaces
This Is Text This Is Text This Is Text These Are
Double Spaces
This Is Text This Is Text This Is Text These Are
Double Spaces
DOCE:I have the script in the Perl/bin directory because it>>> >> >>or instead of /SCTN:/ use /^SCTN:/
> > This doesn't do anything.
> >
> > or /^\s*SCTN:/
> >
> > Nor does this.
> Well... Yes, it does. How did you run it, anyway?
does not run if I have it anywhere else. Also I run it
as #!/usr/bin/perl -w because #!/usr/bin/perl -p
freezes my MS-DOS prompt.
The original input is a file called test1.txt and the
output is a file called test3.txt
The whole program is as follows:
#1. path to input and output file
$inputfile = "C:/My Documents/Programming/Perl
exercises/test/test1.txt";
$outputfile = "C:/My Documents/Programming/Perl
exercises/test/test3.txt";
#2. filehandles
open (INPUT, "$inputfile") || die "can't open
$testfile";
open (OUTPUT, ">$outputfile") || die "can't open
$testfile";
#initialise $text
$text = "";
#3. read input file into $text
while (<INPUT>){
$text = $text . $_;
}
# 4) split document by DOCE: string into array of
tags
@stories = split (/DOCB:/, $text);
#@stories is array of each story
for (@stories){
# 5) general substitutions
#Marketing Week substitutions
if (/SRCE:\s*Marketing Week/i) {
#put substitutions here
s/(\s)(.)/$1\u$2/g if/SCTN:/; #uppercases everything
# 6) now put back DOCE: tag into joined up array
$text = join ("DOCB:", @stories)
}
# 7) now output file
print OUTPUT $text;
#close filehandles
close (INPUT);
close (OUTPUT);
I get no result with any of this.>> what I> > I think the title of my query was misleading -> every> > maybe should have said was "Uppercasing the first
> > letter and lowercasing all the other letters of>> > word in a mixed case part of a string"?
> This is much harder. It's easy to make the output
> like this:
>
> I'Ve Been Reading O'Reilly Books
>
> OR:
>
> I've Been Reading O'reilly Books
>
> but not:
>
> I've Been Reading O'Reilly Books
>
> which is correct. I suggest you to only uppercase
> characters, but if you
> have to also lowercase the other ones then try:
>
> #!/usr/bin/perl -p
> s/(\s)(\S+)/$1\L\u$2/g if/^\s*SCTN:/;
>
> or
>
> #!/usr/bin/perl -p
> s/\b(\w+)/\L\u$1/g if/^\s*SCTN:/;
>
> (and maybe add:
>
> use locale;
Thank you. I'll take a look.>
> at the beginning) and see which suits your needs
> better. You might find
> better examples in the Cookbook.
-bis__________________________________________________ ______________________>
> -zsdc.
>
Want to chat instantly with your online friends? Get the FREE Yahoo!
Messenger [url]http://uk.messenger.yahoo.com/[/url]
Bis Guest



Reply With Quote

