PDA

View Full Version : optimize without using arrays


macchisp
10-16-2007, 08:52 PM
Does anyone know of a way NOT to use the split function? I have used the split function and now I DO NOT want to use arrays because I feel they take a long time to process. I would also like to NOT use shift, unshift, and join as well, but I feel if I can NOT use split it would work its way out.

Here is the code:


my @paragraphs = split(/<br>/, $text);
my @words = split(/\s+/, $paragraphs[0]); #split up paragraph to be indented

while ( 1 ) {
my $next_word = shift(@words);
$first_line .= $next_word . " "; #add next word to indented line
$line_length = $txt->advancewidth($first_line); #get length of indented line
if ( $line_length > $indent_length ) { #if indented line longer than allowed
unshift(@words, $next_word); #put word back into array
$first_line = substr($first_line, 0, length($first_line) - ( length($next_word) + 1 ) );
$paragraphs[0] = join(' ',@words);
last;
}
if ( @words == 0 ) {
shift(@paragraphs);
last;
}
}

Are there any other methods of doing this without using arrays? How can I break up the paragraph without putting it into an array and put it into a string instead?

Thanks for your help

KevinADC
10-16-2007, 10:54 PM
Can you give an example of what output the above code produces?

FishMonger
10-17-2007, 12:37 AM
It would be helpful to see more of your code and a sort explanation of what you're needing to accomplish beyond the desire to not use the array.

macchisp
10-17-2007, 03:01 PM
It would be helpful to see more of your code and a sort explanation of what you're needing to accomplish beyond the desire to not use the array.

Fish - To answer your post, I need the script to run faster, or to optimize the script. My goal is to not use arrays because of the large memory allocation and the amount of resources they use. I wanted to start with removing the arrays and then go on to other parts of the script if the script doesn't run faster.

Can you give an example of what output the above code produces?

For example, if this is $text

$text = "This is some text<br>It is broken up by break tags<br> and then it is broken up by spaces<br>This is some text.";


Then @paragraphs would be

$paragraphs[0] = "This is some text"
$paragraphs[1] = "It is broken up by break tags"
$paragraphs[2] = " and then it is broken up by spaces"
$paragraphs[3] = "This is some text."


Then @words would be (because I'm only taking $paragraph[0])

$words[0] = "This"
$words[1] = "is"
$words[2] = "some"
$words[3] = "text"


So my goal is to check to see if $paragraph[0], is less than the overall length of the line width and, if it is, print the entire string at $paragraph[0] on the line, then print the remaining @paragraphs.

If $paragraph[0] cannot fit on the first line, find out how many words can get on the line, print the first line, and then print the rest of all the @paragraphs.

So for the above example, if $line_width = 10 this would be the result:

This is some
text
It is broken up by break tags
and then it is broken up by spaces
This is some text.


Thanks for the questions and I hope I answered them. If not, please let me know. Thanks again.

FishMonger
10-17-2007, 07:10 PM
What profiling have you done that leads you to believe that using arrays is a problem?

The problem in the code fragment that you posted is with its logic, not the array.

Without seeing more of your code so I can to put this fragment into perspective, which you don't seem to want to show, I can only make general recommendations.

Profile your script to see where the problems are located.
http://www.stonehenge.com/merlyn/UnixReview/col49.html
http://search.cpan.org/dist/Devel-Profile/Profile.pm

Proper use of css can easily handle the line wrapping.

One possible aide for the line wrapping would be Text::Wrap
http://search.cpan.org/~muir/Text-Tabs+Wrap-2006.1117/lib/Text/Wrap.pm

FishMonger
10-17-2007, 10:10 PM
use Text::Wrap;

$Text::Wrap::columns = 20;
$Text::Wrap::separator = "\n";

my $text = 'This is some text<br>It is broken up by break tags<br> and then it is broken up by spaces<br>This is some text.';
$text =~ s/\s*<br>\s*/ /g;

print wrap("", "", $text);

Outputs:
This is some text
It is broken up by
break tags and then
it is broken up by
spaces This is some
text.