> -----Original Message-----
> From: Grant [mailto:[email protected]]
> Sent: Tuesday, April 21, 2009 11:47
> To: Perl Beginners List
> Subject: Re: String manipulation question
>
> >> 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')
>
> Thanks guys. With some help I've come up with this:
>
> $string = 'abc def ghi jkl mno pqr stu vwx yz';
> if(length($string) = 34) {$var1 = $string.":";}
change = to == ( this is the test for equality).
Also add use strict and warnings to your Perl and you would have had a
heads up to what is happening.
If you have any questions and/or problems, please let me know.
Thanks.
Wags ;)
David R. Wagner
Senior Programmer Analyst
FedEx Freight
1.719.484.2097 TEL
1.719.484.2419 FAX
1.408.623.5963 Cell
http://fedex.com/us
> if(length($string) > 34) {
> $string =~ s/\s//g;
> ($var1, $var2) = $string =~ /(.){35}(.){26}/;
> $var2 .= ":";
> }
>
> but I get:
>
> Safe: Can't modify length in scalar assignment
>
> I've got to leave Safe on. Can I modify this to work within it?
>
> - Grant
>
> --
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> http://learn.perl.org/
>
>
>
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/