On 4/21/09 Tue  Apr 21, 2009  8:58 AM, "Grant" <emailgr...@gmail.com>
scribbled:

> I'd like to take a string and manipulate it in a few ways.
> 
> If the string is 34 characters or less, I'd like to append a colon
> character and save it to $var1.

The length function will tell you how many characters are in a string.

> 
> If the string is longer than 34 characters, I'd like to save up to 35
> characters to $var1, but cut off at the last possible space character
> and not including that space character in the variable.  I'd then like
> to save up to 26 characters of the cut off portion (not including the
> previously mentioned space character) to $var2, cut off at the last
> possible space character and not including that space character in the
> variable.  $var2 should have an appended colon character.

The substr function will return parts of a string, and may also be used to
replace parts of a string.

The concatenation operator (.) may be used to add characters to the end of
the string.

The index function will return the first position of a substring within a
specified string, and -1 if the substring cannot be found. The rindex
function returns the last position, so you can use it to find the last space
in a string.

> 
> Is that a tall order?  I'm hoping it's trivial for someone here.

It should be straight-forward to put these functions together to do what you
want. Give it a try and come back here if you can't get it to work.

See the following:

perldoc -f length
perldoc -f substr
perldoc -f index
perldoc -f rindex
perldoc perlop (Search for 'Additive Operators')



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to