CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
break string into 990 character bites

 



spaz
Novice

Mar 29, 2001, 12:35 PM

Post #1 of 6 (632 views)
break string into 990 character bites Can't Post

I have a string of 2000 characters that I need to split into 3 different variables:

$X would be the first 990 characters
$y would be the next 990 characters
$z would be the last 20

I've been trying to this is on a loop with the chop command and it's taking forever. Does anyone have a solution for this?

spaz



japhy
Enthusiast

Mar 29, 2001, 1:17 PM

Post #2 of 6 (630 views)
Re: break string into 990 character bites [In reply to] Can't Post

You probably want to use substr():


Code
my $size = 990; 
my $parts = length($string) / $size;
for (my $i = 0; $i < $parts; $i++) {
push @segments, substr($string, $size * $i, $size);
}

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


freddo
User

Mar 29, 2001, 1:54 PM

Post #3 of 6 (629 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)


freddo
User

Mar 29, 2001, 1:58 PM

Post #4 of 6 (627 views)
Re: break string into 990 character bites [In reply to] Can't Post

looks like the time i took to make this little, japhy came with a faster solution...

Real Programmers use goto;


spaz
Novice

Mar 29, 2001, 2:52 PM

Post #5 of 6 (626 views)
Re: break string into 990 character bites [In reply to] Can't Post

I've got it running pretty well, with only one caveat: where the strings are put next together in the final email that is sent, there is a space between them. Probably because of the "\n" at the end of each string. Is there a way to terminate a print MAIL line and not so there won't be a space. The text is actually html code and it's been screwing stuff up, like &n bsp; where it printed all of the letters out instead of the html space.

For example: "and mo mentum in the " where it has added a space in the word momentum because that is the end of one string and the start of another. I need to eliminate that, if possible.

Here's the code that's working:
## break $article into 990 character bytes for easier transmission
my $size = 990;
my $parts = length($article) / $size;
for (my $i = 0; $i < $parts; $i++)
{
$chunk = substr($article, $size * $i, $size);
print MAIL "$chunk\n";
}



spaz
Novice

Mar 30, 2001, 8:53 AM

Post #6 of 6 (615 views)
Re: break string into 990 character bites [In reply to] Can't Post

OK -- it's almost working except for one item. I'm pulling from a database and adding the extra space due to the \n in the print MAIL is messing up my html. So, I want to make sure the 990 (it can be shorter than that) chunk ends in a blank space. So when it adds the space in the print MAIL, all will be cool. If the chunk doesn't have a blank space, then go back and grab a smaller chunk and test until it meet that requirement and will print it. Here's the code I have, which is not working:

## break $article into 990 character bytes for easier transmission
my $size = 990;
my $parts = length($article) / $size;
for (my $i = 0; $i < $parts; $i++)
{
@chunk = substr($article, $size * $i, $size);
&test_chunk; #test to make sure it ends with a space
}

sub test_chunk
{
my $endchunk = pop(@chunk); #whack off last character
if ($endchunk !~ / /) #if last character is not a space
{
@chunk = push(@chunk,$endchunk); #add character back onto line
my $size = $size - 1; #redo the size to be shorter than the first time
$article = join("",@chunk,$article); #add the chunk back onto the article
@chunk = substr($article, $size * $i, $size); #get new chunk
&test_chunk; #test it again
}
else {
print MAIL "@chunk", "\n";
}
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives