
freddo
User
Mar 29, 2001, 1:54 PM
Post #3 of 6
(467 views)
|
|
Re: break string into 990 character bites
[In reply to]
|
Can't Post
|
|
hello, chunk you can use substr this way: $x=substr($buffer, 0, 990); $y=substr($buffer, 990, 990); $z=substr($buffer, 1980, 990); but if i understand your problem something like this might be better: #/usr/bin/perl $buffer = "F"x2000; # the string to split $chunkSize = 990; # the size of the chunks $bufferLength = length($buffer); # size of the var to be splitted $offset = 0; # where are we in the string @chunks; # the pieces while ($offset < $bufferLength) { push @chunks, substr($buffer,$offset,$chunkSize); $offset += $chunkSize; } #my $count; print map { "\nBlock ".$count++.": $_"; } @chunks; ### EOF my second attempt to make this better gives this: #/usr/bin/perl -w use strict; sub chainSaw { my ($bufferRef, $chunkSize) = @_; # string to split & chunks size my $bufferLength = length($$bufferRef); # size of the var to be splitted my $offset = 0; # where are we in the string my @chunks; # the pieces while ($offset < $bufferLength) { push @chunks, substr($$bufferRef,$offset,$chunkSize); $offset += $chunkSize; } return @chunks; } my $buffer = "F"x20; my $chunkSize = 9; my $count; my @bufferChunks = chainSaw(\$buffer, $chunkSize); print map { "\nBlock $count: $_"; } @bufferChunks; ### EOF i think it would even be faster if you add, as an argument to the sub a ref to the array you work on (so you dont create a useless array in the sub, i will try this with Jasmine's benchmark trick she gaves a few days back. Anybody knows how to improve the little program? Real Programmers use goto;
(This post was edited by freddo on Mar 29, 2001, 12:55 PM)
|