In your example, I think you mean to use a character class, $agent =~ /[A-Z]/. Applying that to your question, you want to use the substitution operator:
$agent = 'PerlBeginnerPerl';
$agent =~ s/([A-Z])/ $1/g;
Taking that apart, the parens store the matched character in $1. A space followed by the value of $1 is substituted. The g flag is for global, it make the substitution apply all through the string..
This will insert a leading space if the first character is capitol. To avoid that we can use a more sophisticated gadget. The additional element is a negative lookbehind assertion that we're not at the beginning of the string: $agent =~ s/(?<!^)([A-Z])/ $1/g;
Update: (Assuming my reading of the question was correct) The second version can be converted to capture nothing by using positive lookahead for the capitol letter: $agent =~ s/(?<!^)(?=[A-Z])/ /g;
This does the insertion by looking at the surroundings of the notch before a character, and inserting a space if conditions warrant.
After Compline, Zaxo | [reply] [d/l] [select] |
Conversely, you could use \B to match a non-boundary. This would still take into account the beginning of the string, while also allowing for spaces within the string.
$agent = 'PerlBeginnerPerl AndAnother';
$agent =~ s/\B([A-Z])/ $1/g;
# Produces 'Perl Beginner Perl And Another'
$agent = 'PerlBeginnerPerl AndAnother';
$agent =~ s/\B([A-Z])/ $1/g;
# Produces 'Perl Beginner Perl And Another'
I realize that spaces within the string is probably outside the scope of the original question, but \B covers his bases and might be a little easier for a beginner to grok than a look-ahead.
-- grummerX
| [reply] [d/l] |
$agent =~ m/[A-Za-z]*/;
which will match a string of characters (uppercase or
lowercase) or
$agent =~ m/PerlBeginner(Perl)?/;
Which will match the strings "PerlBeginner" and "PerlBeginnerPerl".
All of this is documented in the perlre perldoc.
1. dude, what does mine say?
2. "sweet", what about mine?
3. "dude", what does mine say?
4. GOTO 2
| [reply] [d/l] [select] |
you propably meant $agent =~ m/[A-Za-z]+/; in the first example, the * will also match the null string...
~Particle ;Þ
| [reply] [d/l] [select] |
This is a bit vague, but guessing from your code snippet, I think all you want to do is wrap your strings in quotes.
While it may be handy to use barewords in the smallest of scripts, you may want to follow the advice in perldata, which says,
A word that has no other interpretation in the grammar will be treated as if it were a quoted string. These are known as ``barewords''. As with filehandles and labels, a bareword that consists entirely of lowercase letters risks conflict with future reserved words, and if you use the -w switch, Perl will warn you about any such words. Some people may wish to outlaw barewords entirely.
There's more in perldata about the much (and generally rightly) maligned bareword.
If this still doesn't make sense, compare the 2 short programs :
use strict;
use diagnostics;
my $foo=PerlBeginner;
print $foo;
This will not run because PerlBeginner is not a core perl routine, and not a sub defined in the script, or in the strict or diagnostics modules.
Compare that with :
use strict;
use diagnostics;
my $foo="PerlBeginner";
print $foo;
Now replace "PerlBeginner" with "Perl Beginner", and see what happens. This compiles and runs because the string is wrapped in quotes. Also note the use strict and use diagnostics pragmas. These will alert you to the use of barewords (and other things) and enable verbose error messages for debugging, respectively. Most people consider the use of strict to be mandatory for scripts of any complexity.
If you don't like the idea of having to use quotes, look in perlop under "Quote and Quote-like Operators" for other ways to do it.
update : If this doesn't help you, or was way off base, please consider adding a little more descriptive content to your question; the three responses you've gotten so far are all over the field. | [reply] [d/l] [select] |
<update>Untested code rocks. :) dvergin is absolutely correct, this code will not work. I will leave it as is, to serve as a warning to others about posting without testing. Thanks for the lesson, dvergin. :)
And now back to our regular show... </update>
I would try something like:
$agent =~ s/.([A-Z])/ $1/g;
Which should insert a space in front of each capital letter, except if it is at the start of the string (the dot makes sure there must be at least one letter of any kind in front of the matched uppercase letter).
If you don't want to insert when there are capital letters standing together, like in CIA => C I A, you could try something like:
$agent =~ s/[^A-Z]([A-Z])/ $1/g;
Which should make sure it matches an uppercase letter, that is preceded with anything but an uppercase letter.
Hope I understood the question correctly. :)
You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue. | [reply] [d/l] [select] |
You did not test your code. The character matched by the
dot is being lost in the replacement.
The following snippet:
my $agent = 'PerlBeginnerPerl';
$agent =~ s/.([A-Z])/ $1/g;
print $agent;
produces: Per Beginne Perl
To make your approach work, you would need:
$agent =~ s/(.)([A-Z])/$1 $2/g; | [reply] [d/l] [select] |
The code definitely did work.
Thanks a lot.
c_lhee@yahoo.com
| [reply] |