Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
No Split Words

Posted by muller01 (muller01), 27 January 2003
Hi , Im quite new to tcl and have a problem that i hope somebody could help me out.

I am trying to split up data for example,
I have a variable with text data that consists of 500 characters.
i need to split up this data into chunks a max of 80 characters , but i cant have any split words.

I would obviously have to read ahead and split every character into words, i am not sure of what the best way to do this in tcl would be.

any help on this matter would be much appreciated.
thanks
Ray

Posted by admin (Graham Ellis), 27 January 2003
Not as hard a problem as you might think (mind, it's easy for me to say!) if you use regular expressions - just look for strings of up to 80 characters followed by a space, and replace them with the same 80 characters folloed by a new line.  I suspect that's your real goal, but if you do want to split the string, you can then do so at the new line character.

Sample program (I've split into lines of up to 30 characters):

Code:
set in "This is a block of text that I want to insert line breaks into every so often but only at word ends"
regsub -all {(.{1,30})\s} "$in " "\\1\n" out
puts -nonewline $out


Sample output:

Code:
bash-2.04$ tcl tada.tcl
This is a block of text that I
want to insert line breaks
into every so often but only
at word ends
bash-2.04$


Posted by muller01 (muller01), 27 January 2003
Hi ,Graham ,
Thanks for your quick response,
I have tried your sample code but cant seem to get it to work. What I am trying to do is for a  variable with text data that consists of 500 characters.  
i need to split up this data into chunks a max of 80 characters and for each 80 characters create a message (put value into a variable) , but i cant have any split words.  
What you have sent is ideal but i cant seem to get it to work. I have even pasted the code into a text file , and ran it exactly as you said but the result i  get back just displays the full line in the result.

any help on this matter is much appreciated,
thanks again Graham



Posted by admin (Graham Ellis), 27 January 2003
Application understood  

Bit of a suprise that it didn't work for you, as I wrote and tested the code and what you saw was cut and pasted straight off my screen.   Two questions:
a) What version of Tcl are you running?   The Regular Expression I used is of the type supported in current releases, and if you have an older release you may need to rewrite it.
b) Are you outputting via a browser?  If so, adding in the new lines isn't going to do very much good display wise, as browsers render a new line character as just a white space

Now - onto the splitting into separate variables ...  start with the code I provided (or modify it if you have a matured version of Tcl) so that you replace a space with a new line at every point that you want a fresh variable to start.  Then split and loop through the individual member strings of the resulting list:
Code:
foreach message [split $out \n] {
         commands to run on each $message
}


Posted by muller01 (muller01), 27 January 2003
Once again , thanks a mil for your  reply graham,
I downloaded one of the latest version of tcl and the code works fine and its exactly what i need graham, however the version of tcl is version 8.0 and tclx and it dosent seem to work on that version.
Unfortunately i need to use this version. any suggestions on what i may need to modify on the regsub command to achieve similar results.

Kind Regards
Ray

Posted by admin (Graham Ellis), 27 January 2003
OK ... then you're using a version of Tcl that dates from around 1996 or 1997.   Being an embeddable language, we do come across mature versions from time to time.

What did I have in that regular expression?

.{1,80}
You could replace that by
.?   80 times over (write a loop - don't code it by hand

\s
you could replace by
[ \r\t\n]

And that should reduce it to an "ordinary" regular expression that will work back to 8.0 and earlier.

I can't spot anything else that needs changing - but I don't have an 8.0 on my machine to test with.

Regular expressions (old and new flavours) are pretty well covered in many of the books - have a look at my web site
http://www.wellho.net/resources/tclbooks.html and
let me know which you have, and I'll let you know page and chapter numbers that explain it far more fully that I can do here

Posted by muller01 (muller01), 29 January 2003
Hi Graham, Thanks for uour replies. I got a chance to try the code you sent without using a loop for 9 characters but got some strange results back as follows:

Code :

set in "this is a test message rthis is a test message nhis is a test message"
regsub -all {(.?.?.?.?.?.?.?.?.?.?)[ \r\t\n]} "$in " "\\1\n" out
puts -nonewline $out


this is a
est
message r
his is a
est
message
his is a
est
message

it looks like it is removing "r t or n " whichever is last from the beginning of the words.

Posted by admin (Graham Ellis), 29 January 2003
All to do with quoting ... try \\r \\t, etc.

Sorry it's a brief reply - I'm on a hotel line that costs per minute tonight!

Posted by muller01 (muller01), 30 January 2003
Thanks for your reply again graham, The following code seems to work ok but i'm not sure if it right or not :
here is the code and the result . Could u tell me exactly what the resular expression is doing here as i am not fully sure. e.g Why use .? "$in " " \\1\n" out


Once again thanks a lot for your help.


Code

set in "This is a test to see if it works ok. This is a test to see if it works ok."

regsub -all {(.?.?.?.?.?.?.?.?.?.?)[ \\ ]}  "$in " " \\1\n" out
puts -nonewline $out

Result :


This is a
test to
see if it
works ok.
This is a
test to
see if it
works ok.


Posted by admin (Graham Ellis), 30 January 2003
Ah - regular expressions are a very powerful technology - a world unto themselves.

Have a look at http://www.regularexpression.info/tcl.html for Tcl specifics and http://www.regularexpression.info/els.html for details of how each of the element types works.


Posted by muller01 (muller01), 1 February 2003
Does the code look ok to you Graham,
or do you think there could be a problem.

Ray


Posted by admin (Graham Ellis), 1 February 2003
Well ... I just ran

Code:
set in "This is a test to see if it works ok. This is a test to see if it works ok."

regsub -all {(.?.?.?.?.?.?.?.?.?.?)[ \t\n\r]}  "$in " " \\1\n" out
puts -nonewline $out


and it worked fine. I'm going to mark this post "no smileys" in case that damages the way the code displays, and edit my post if it looks wrong!

No - looks right!



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2021: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho