> -----Original Message-----
> From: Grant [mailto:emailgr...@gmail.com] 
> Sent: Tuesday, April 21, 2009 09:58
> To: Perl Beginners List
> Subject: 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.
> 
> 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.
> 
> Is that a tall order?  I'm hoping it's trivial for someone here.
> 
> Thanks,
> Grant
        Here is a simple script that attempts to do what you ask. If you
are new at Perl, then
can seem overwhelming to say the least. But once you get into it, you
start thinking differently. Jim G looks at indexing, etc while that
never ended my sphere of things. I thought of spliting on white space
and rebuilding the line along the way you wanted it back out. It is only
a start and does not take into account if you have a very large word
that could be say greater than 34 or 26 characters.
Code follows:

#!/usr/bin/perl

use strict;
use warnings;

=head1
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.

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.

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

=cut

my $var1 = q[];
my $var2 = q[];
my @MyWorkw = ();
while ( 1 ) {
    print
"1234567891123456789212345678931234^2345678911234567892123456\n";
    chomp($_ = <STDIN>);
    last if ( /^(ex|qu)/i );
    ($var1, $var2) = ( q[], q[] );
    
    if ( length($_) < 35 ) {
        $var1 = $_ . q[:];
     }
     else {
         @MyWorkw = split(/\s+/, $_);
         while ( 1 ) {
             if ( length($var1)+length($MyWorkw[0]) < 35 ) {
                 $var1 .= shift(@MyWorkw) . q[ ];
              }
              else {
                  $var1 =~ s/ $/:/;
                  last
              }
         }
         while ( 1 ) {
             if ( length($var2)+length($MyWorkw[0]) < 26 ) {
                 $var2 .= shift(@MyWorkw) . q[ ];
              }
              else {
                  $var2 =~ s/ $/:/;
                  last
              }
         }
        
     }
    printf "Original Data:\n<%s>\n", $-;
    printf "var1: <%s>\n
12345678911234567892123456789312345\nvar2: <%s>\n
12345678911234567892123456\n", $var1, $var2;
 }
 ^--- Code ends here

         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 


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

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


  • RE: String manipulatio... Wagner, David --- Senior Programmer Analyst --- CFS

Reply via email to