This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Spliting text in two lines

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Spliting text in two lines

Post by Madalin »

Evening, can anyone help me with a code that will split the text in two lines (getting all the text in does 2 lines) if its bigger than a max length ? Thanks
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

In PHP there's a function called wordwrap that dose this nicely. Here's how I got about the same results in TCL.

Code: Select all

proc wordwrap {text max} {
	set len [string length $text]
	while {$len} {
		if {$len > $max} {
			set cut [string range $text 0 [expr $max - 1]]
			set output [string range $cut 0 [string last " " $cut]]
			set text [string range $text [string length $output] end]
			incr len -$max
		} else {
			set output [string range $text 0 $len]
			set len 0
		}
		puts "output: $output"
	}
}
results:

Code: Select all

% set text "Evening, can anyone help me with a code that will split the text in two lines (getting all the text in does 2 lines) if its bigger than a max length ? Thanks"
Evening, can anyone help me with a code that will split the text in two lines (getting all the text in does 2 lines) if its bigger than a max length ? Thanks
% wordwrap $text 80
output: Evening, can anyone help me with a code that will split the text in two lines
output: (getting all the text in does 2 lines) if its bigger than a max length ? Thank
% wordwrap $text 50
output: Evening, can anyone help me with a code that will
output: split the text in two lines (getting all the text
output: in does 2 lines) if its bigger than a max length
output: ? Thanks
% wordwrap $text 100
output: Evening, can anyone help me with a code that will split the text in two lines (getting all the text
output: in does 2 lines) if its bigger than a max length ? Thanks
Notice that it doesn't break words so it looks nicely.

PS: The code at the end of the Character hiding and showing topic isn't working or completly forgot about that topic? :roll:

PS #2: On Rosetta Code there's wrapParagraph with another approach on this.
Once the game is over, the king and the pawn go back in the same box.
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

Code: Select all

proc Trivia247:wraptext {chan text characters} {
   
	set len [string length $text] 
	putlog $len
 
   while {$len} { 
      if {$len > $characters} { 
         set cut [string range $text 0 [expr $characters - 1]] 
         set output [string range $cut 0 [string last " " $cut]] 
         set text [string range $text [string length $output] end] 
         incr len -$characters 
      } else { 
         set output [string range $text 0 $len] 
         set len 0 
      }
		putlog "$output"
		putlog "[string length $output]"
		
      putserv "PRIVMSG $chan :$output"
   } 
}

DCC CHAT:
[18:09:03] 1499
[18:09:03] BONUS +14:
[18:09:03] 44

[18:09:03] 0

[18:09:03] 0
[18:09:03] InmcemanmamavutmlocmprimamparticiparemamRomanieimlamunmturneumfinalmalmCampionatuluimMondialInmc
[18:09:03] 300
Channel:
<Androo> BONUS +14:
<Androo> InmcemanmamavutmlocmprimamparticiparemamRomanieimlamunmturneumfinalmalmCampionatuluimMondialInmc
<Androo> 1st Hint: *** +12 Bonus
<Androo> 2nd Hint: d** +10 Bonus
<Androo> 3rd Hint: *aa +8 Bonus
Don`t why... from your example it should work.
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

That script tries to split the text in a correct place, at the nearest space.

The string you try is long, but has no spaces.

That is not a valid string, there is no single "word" that long:)
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
User avatar
heartbroken
Op
Posts: 110
Joined: Thu Jun 23, 2011 11:15 pm
Location: somewhere out there

Post by heartbroken »

tcllib/textutil could be an alternative: https://core.tcl.tk/tcllib/doc/trunk/em ... split.html
Life iS Just a dReaM oN tHE wAy to DeaTh
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

It looks that way because of the ANTI HACK part.
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

heartbroken: Works only if i stop the antihack part. So when the questions is "word word word" works, but when its in a single word it brocks.

Anyone with an idea on how to make anything split using the antihack option?

Antihack = it adds a random letter each question between the spaces of the words, so if you try to copy - paste the question it would be "impossible" to read.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Basically the line you are mentioned is most likely from a trivia bot and is "obfuscated" by adding a character between words and a special color so that regular legit users can see the question without the extra colored characters, but anyone else why tries to be cheeky or copy/paste the question would have a hard time "decoding" it back to a normal question.

If you own the bot (based on your claim that you can start/stop the "obfuscation" process) that is spitting the trivia questions why do you need this? What exactly do you want to achieve here? Want to cheat at your own game? :lol:

Since the "special" character used to "obfuscate" the code (in the given example is "m") is also found in the other words used in the question reversing the process is kind off an impossible task, unless you know exactly at what positions it was added. For example an extra 'm' is added at 2, 5, 8, and so on. So if you know the pattern then is easy to replace the special character with a space and then recreate the question into a "word word word" that with the code I mentioned would get the result you wanted.
Once the game is over, the king and the pawn go back in the same box.
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

Yes its from a trivia script i wrote from scratch.

I was thinking of something to add the letters after the split, but never got it working.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

As i was looking on the forums for something I stumbled onto a very old discussion (this) where user and strikelight (two forum users, long time idle sadly) debated over a pice of code user made to wrap some text output. Here is the final code with the changes they discussed:

Code: Select all

proc wordwrap {str {len 70} {splitChr { }}} {
	set out [set cur {}]; set i 0
	foreach word [split [set str][set str ""] $splitChr] {
		if {[incr i [string len $word]]>$len} {
			lappend out [join $cur $splitChr]
			set cur [list $word]
			set i [string len $word]
		} else {
			lappend cur $word
		}
		incr i
	}
   lappend out [join $cur $splitChr]
}
Once the game is over, the king and the pawn go back in the same box.
Post Reply