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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use Text::Wrap? 2

Status
Not open for further replies.

jriggs420

Programmer
Sep 30, 2005
116
US
I need to format a string of data, but I don't want to start each string w/ a new line (don't separate long string into a series of short strings). Basically I want the function of the module, only with a single string. I can't simply replace the /n char with // because this causes problems with the formatting. Also, I don't want to break up the words....
Maybe something like==Count 75 spaces if a word goto beginning of word insert spaces until char count is 80. repeat to end of string. Does this make sense?
TIA

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
maybe you could post some sample data and the desired output/formatting of your sample data. That might help explain it better.
 
I agree with Kevin, sample data would be good. I'm a bit confused - is there a reason you don't want to use Text::Wrap?
 
I would prefer to use text::wrap since it already does most of what I want. But it inserts the newline char which I don't want. Replace/remove the \n totally fubars the output. It's hard to put up data since wrap is automatic in this box. I will try to explain. Assume $columns=10

$instring =one two three four five six(27 chars)
"three" won't fit so it is the first split point so convert to $instring =one two three four five six;
which would appear (in vi) as
one two
three four<=10 chars exactly
five six :
with no conversion would appear:
one two th
ree four f
ive six
It would be simple if I wasn't concerned with splitting the words. I can't be the first person to try to do this. Also, I need to use core modules, if any.


A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
Are there any imbedded newline characters in the data?
 
nope, just one long string

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
Is this similar to what you're looking for?
Code:
my $str = 'one two three four five six';
my $max_len = 10;
my $delim = '@//~';

use Text::Wrap;
local $Text::Wrap::columns = $max_len + 1;
local $Text::Wrap::separator = $delim;

my $wrapped = wrap("", "", $str);
my @lines = map {"$_". ' 'x($max_len - length($_))} split($delim, $wrapped);
print join '', @lines;
 
rharsh,

That is exactly it! Many thanks yet again...If you don't mind me asking, did you come up with that on your own, or did you find a similar version somewhere? What does '@//~'; represent? I don't think I would have gotten that ever! I was thinking about looping through the string one character at a time, building an array, and then performing the necessary operations as needed. Your solution is ten times more workable though, thank you!

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
You're welcome, glad it worked for you. I did come up with it on my own, but I'm sure there's other similar examples floating around.

The string '@//~' doesn't really represent anything - it's an arbitrary set of characters that, I hope, won't show up in your text anywhere. You could probably use the default separator ("\n") since you said there weren't be any newlines in your text. but I wanted to be sure the string I used to delimit the strings wasn't in your text.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top