0

how can splitt a text in variables always after 50 characters?

so is splitt only in a new line after 50 charachters

set length [::textutil::adjust $text -length 50 -strictlength true]

Regards

edit:

input is this text in variable $Plot

LaRochelle, a former pirate captain, is caught by the British. To get his 
ship back, he works as a spy against other pirates, first of all Blackbeard 
and Providence. He works on some ships, crossing the Caribbean sea, with the 
intention


set pieces [regexp -all -inline {.{1,50}} $Plot]
set 0 [lindex [lindex $pieces 0] 0]
set 1 [lindex [lindex $pieces 1] 1]
putnow "PRIVMSG $channel :$0"

output is only:

<testbot> LaRochelle,
<testbot> British.

Unfortunately no longer..

revoque
  • 15
  • 3
  • You get what you ask for. The invocation of `regexp` divides your text into several 50-character pieces. Then you ask for the first item in the first piece, and get "LaRochelle,", and for the second item in the second piece, and get "British.". Were you expecting something else? – Peter Lewerin Oct 28 '17 at 15:14
  • yes 0-50 pieces, not only LaRochella, this is 11 pieces, where's the rest? 50 Pieces is this: LaRochelle, a former pirate captain, is caught by – revoque Oct 28 '17 at 15:35
  • Look at what you have in the `pieces` variable, or use the expressions `lindex $pieces 0` and `lindex $pieces 1`respectively. `lindex $pieces 0` for example gives you `LaRochelle, a former pirate captain, is caught by `, since that is the first item of the list in `pieces`, since `pieces` is a list of fragments from your text, each up to 50 characters long. – Peter Lewerin Oct 28 '17 at 17:01
  • oh thx, i see now – revoque Oct 28 '17 at 17:50

2 Answers2

1

You could use regsub to add a newline after every 50th character.

set text [string repeat 123456 48]
set formatted [regsub -all {.{50}} $text "&\n"]
puts $formatted
12345612345612345612345612345612345612345612345612
34561234561234561234561234561234561234561234561234
56123456123456123456123456123456123456123456123456
12345612345612345612345612345612345612345612345612
34561234561234561234561234561234561234561234561234
56123456123456123456123456123456123456
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Its this only split in next line? correct? I need it in variable, it is no longer directly after each. there are other things in between – revoque Oct 28 '17 at 07:46
1

The simplest method is to use regexp -all -inline for the splitting, as it has a result of a list of all matches (and submatches if present) meaning that it can deliver desired results pretty directly:

set pieces [regexp -all -inline {.{1,50}} $inputString]

The RE is .{1,50} (in braces; technically unnecessary in Tcl, but almost always a good idea), which means “one to fifty characters, as much as possible (because of greedy matching)” and we get as many of those as we can.


If you want to constrain to word boundaries, the RE would be best changed to \m.{1,50}\M.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • Once you've got a list, you can iterate over it with `foreach` or `[join $pieces "\n"]` to make a string with newlines between the element text. – Donal Fellows Oct 28 '17 at 11:06