Re: Changing case of the first letter of words in string
--- zsdc [EMAIL PROTECTED] wrote: *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. thanks for the reminder zsdc - i should have remembered to cc the list when replying to you. bis wrote: Tnaks zsdc and JEGII. --- zsdc [EMAIL PROTECTED] 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. No, it doesn't. Only the lines containing SCTN: Have you run it? yes i have run it and below is the kind of output i 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: 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? I have the script in the Perl/bin directory because it 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); [EMAIL PROTECTED] 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 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? 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; I get no result with any of this. at the beginning) and see which suits your needs better. You might find better examples in the Cookbook. Thank you. I'll take a look. -bis -zsdc. Want to chat instantly with your online friends? Get the FREE Yahoo! Messenger http://uk.messenger.yahoo.com/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Changing case of the first letter of words in string
bis wrote: #!/usr/bin/perl -p s/(\s)(.)/$1\u$2/g if/SCTN:/; This capitalises the first letter of every word in the whole document. No, it doesn't. Only the lines containing SCTN: Have you run it? yes i have run it and below is the kind of output i get (original input all lower case except capitalised tags and for SCTN: line which is mixed case): [...] 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? I have the script in the Perl/bin directory because it 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. So you have changed my program, removing the -p switch. Have you read about the switch you removed? See perlrun(1) manpage: http://www.perldoc.com/perl5.8.0/pod/perlrun.html#-p -p causes Perl to assume the following loop around your program, which makes it iterate over filename arguĀ- ments somewhat like sed: LINE: while () { ... # your program goes here } continue { print or die -p destination: $!\n; } [...] I have no experience with Perl under DOS, so it might be related to CRLF or some other issues as well, but just in case, did you run my program as: program input.txt output.txt or in some different way? It's a filter, so it reads its input on STDIN or from files given as arguments, and prints the output on STDOUT. Read about the diamond operator (the null filehandle) in perlop(1) manpage: http://www.perldoc.com/perl5.8.0/pod/perlop.html#I-O-Operators The original input is a file called test1.txt and the output is a file called test3.txt The whole program is as follows: [...] You can't put part of my program anywhere into your code to make it do whatever you want (even Perl has a DWIM-wise limit). It only works in the same context, i.e. when it's executed for every line of input, having a single line of input in $_ every time it is run. 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; I get no result with any of this. Of course you don't get any results if you remove the -p switch which is absolutely essential for such a filter. Every program I wrote for you is correct, but when you change them, they obviously might stop being correct any more. -zsdc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Changing case of the first letter of words in string
James Edward Gray II wrote: On Friday, August 22, 2003, at 05:06 PM, bis wrote: Thanks Gabriel - your suggested code s/(SCTN:\s*)(\w+)/$1\u$2\E/g; [...] Well, let's see if we can get a little closer: s/(SCTN:\s*)(.+)$/join '', $1, map { ucfirst $_ } split /( )/, $2/ge; See if that helps any. It gives a syntax error. Maybe try this: #!/usr/bin/perl -p s/(\s)(.)/$1\u$2/g if/SCTN:/; or instead of /SCTN:/ use /^SCTN:/ or /^\s*SCTN:/ to match only if it's the first word in the line. Is that what you need? -zsdc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Changing case of the first letter of words in string
On Saturday, August 23, 2003, at 02:34 AM, zsdc wrote: s/(SCTN:\s*)(.+)$/join '', $1, map { ucfirst $_ } split /( )/, $2/ge; See if that helps any. It gives a syntax error. Maybe try this: Sorry, I forgot to backwack those /. My fault for not testing the code. I'll try again: s{(SCTN:\s*)(.+)$}{ join '', $1, map { ucfirst $_ } split /(\s+)/, $2 }ge; Good luck. James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
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: Tnaks zsdc and JEGII. --- zsdc [EMAIL PROTECTED] 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. No, it doesn't. Only the lines containing SCTN: Have you run 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? 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? 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; at the beginning) and see which suits your needs better. You might find better examples in the Cookbook. -zsdc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
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 --
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? : ( Bis My guess (without actually trying) is the word boundary marker (\b). 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; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Changing case of the first letter of words in string
Bis [EMAIL PROTECTED] 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? : ( That will only change the first letter of the first word after 'SCTN:'. 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. :-) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Changing case of the first letter of words in string
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! :) --- Gabriel Cooper [EMAIL PROTECTED] wrote: 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? : ( Bis My guess (without actually trying) is the word boundary marker (\b). 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; Want to chat instantly with your online friends? Get the FREE Yahoo! Messenger http://uk.messenger.yahoo.com/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Changing case of the first letter of words in string
On Friday, August 22, 2003, at 05:06 PM, bis wrote: 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! :) Well, let's see if we can get a little closer: s/(SCTN:\s*)(.+)$/join '', $1, map { ucfirst $_ } split /( )/, $2/ge; See if that helps any. James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]