Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

split a string into array based on string length

Status
Not open for further replies.

jez

Programmer
Apr 24, 2001
370
VN
Hi all,

I am trying to take a text field of up to 360 characters and split it into an array of strings that are maximum 60 characters.

here is my code...

Code:
#!/perl/bin/perl -w


my $longText = <<END;
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed quam. Maecenas non pede. Donec metus tortor, cursus quis, aliquet et, ornare id, pede. Vestibulum vestibulum porttitor nisi. Quisque tristique vehicula lacus. Donec id dui. Donec nec massa nec turpis aliquet ornare. Integer vitae ante. Suspendisse potenti. Sed enim est, dignissim a, fermentum sed.
END

$count = length($longText);
@stringChunksArray = split(/.{60}/, $longText);

print STDOUT $count ." is the total characters in the long text";

for($i=0;$i<@stringChunksArray;$i++){
    print STDOUT $stringChunksArray[$i]."\n";
    print STDOUT length($stringChunksArray[$i]) . "\n\n\n";
    
}
exit(0);


It is a test script so i can get it right on the split.

I think this is right, but it does not seem to work, can anyone see where i am going wrong.

Thanks

Jez
 
I might have solved it...

Code:
split(/(.{60})/, $longText);
 
This is not a job for split.
This is a job for substr.
Code:
my $longText = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Se"
             . "d quam. Maecenas non pede. Donec metus tortor, cursus quis, "
             . "aliquet et, ornare id, pede. Vestibulum vestibulum porttitor"
             . " nisi. Quisque tristique vehicula lacus. Donec id dui. Donec"
             . " nec massa nec turpis aliquet ornare. Integer vitae ante. Su"
             . "spendisse potenti. Sed enim est, dignissim a, fermentum sed.";

my ( $chunksize, $longtextlength ) = ( 60, length($longText) );
my @stringChunksArray = ();

for( my $counter = 0; $counter< $longtextlength; $counter += $chunksize ) {
  push @stringChunksArray, substr( $longText, $counter, $chunksize )
}

print STDOUT $longtextlength ." is the total characters in the long text\n";

for($i=0;$i<@stringChunksArray;$i++){
    print STDOUT $stringChunksArray[$i]."\n";
    print STDOUT length($stringChunksArray[$i]) . "\n\n";
}

I re-did the string as it reads as 361 characters (includes the \n between the period and the END delimiter.

 
Yes, generally with split you want to throw out the delimiter, when you put capturing parenthesis around the split pattern they are retained, which is why it worked. But substr (or maybe unpack) is better suited for this purpose since you are not really splitting your text up by a pattern or delimiter.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Cool, thanks.

I wasn't sure if split was right for this and after taking the 'split' code into the script where it was for, i got some odd results.

Thanks very much, its much appreciated.


Jez :)

 
Looks like I'm a day late and a dollar short but this might work for you as well:
Code:
my @stringChunksArray = ($longText =~ m/(.{1,60})/gs);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top