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.

Remove a concrete blank character

Help for those learning Tcl or writing their own scripts.
Post Reply
j
juanamores
Master
Posts: 317
Joined: Sun Mar 15, 2015 9:59 am

Remove a concrete blank character

Post by juanamores »

I have a variable that stores the artist name and the song separated by a hyphen.
The variable can return this format: Artist-Song
or
this format: Artist - Song

I have made this script that works only for one of the options.

Example:

Code: Select all

bind pub n|n !t testing
	
proc testing {nick uhost handle chan text} {	
set liveme "Janis Joplin-Piece Of My Heart"	
 putquick "PRIVMSG $chan :\<artistName\>[lindex [split $liveme "-"] 0]\<\/artistName\>"
 putquick "PRIVMSG $chan :\<songName\>[lindex [split $liveme "-"] 1]\<\/songName\>"
  }
Returns what I want:
17:34             @mybot ¦ <artistName>Janis Joplin</artistName>
17:34             @mybot ¦ <songName>Piece Of My Heart</songName>
But...
If the variable contains the other format, spaces are left after the artist name and before the song name.

Example2:

Code: Select all

bind pub n|n !t testing
	
proc testing {nick uhost handle chan text} {	
set liveme "Janis Joplin - Piece Of My Heart"	
 putquick "PRIVMSG $chan :\<artistName\>[lindex [split $liveme "-"] 0]\<\/artistName\>"
 putquick "PRIVMSG $chan :\<songName\>[lindex [split $liveme "-"] 1]\<\/songName\>"
  }
Return this I do NOT want:
17:34 @mybot ¦ <artistName>Janis Joplin </artistName>
17:34 @mybot ¦ <songName> Piece Of My Heart</songName>
I need the format to always be the same regardless of the value of the variable.
I need always to achieve this:
<artistName>Janis Joplin</artistName>
<songName>Piece Of My Heart</songName>
EDIT: Fixed!

Code: Select all

bind pub n|n !t testing
	
proc testing {nick uhost handle chan text} {	
set liveme ""Janis Joplin - Piece Of My Heart""	
set liveme1 "\<artistName\>[lindex [split $liveme "-"] 0]\<\/artistName\>"
set liveme2 "\<songName\>[lindex [split $liveme "-"] 1]\<\/songName\>"

set liveme1 [string map [list " </" "</"] $liveme1]
set liveme2 [string map [list "> " ">"] $liveme2] 
 	
  putmsg $chan "$liveme1"
  putmsg $chan "$liveme2"
  }
If you know of any more abbreviated or better, please share it. :)
If you do not understand my ideas is because I can not think in English, I help me with Google Translate. I only speak Spanish. Bear with me. Thanks :)
s
silentziler
Voice
Posts: 11
Joined: Wed Sep 09, 2015 10:57 am

Re: Remove a concrete blank character

Post by silentziler »

Alternative:

regsub -all { <} $liveme1 {<} liveme1
regsub -all {> } $liveme2 {>} liveme2

instead of using [string map]
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

First format:

Code: Select all

% set liveme "Janis Joplin - Piece Of My Heart"
Janis Joplin - Piece Of My Heart
% set pos [lsearch $liveme "-"]
2
% lrange $liveme 0 [expr $pos - 1]
Janis Joplin
% lrange $liveme [expr $pos + 1] end
Piece Of My Heart
Second format:

Code: Select all

% set liveme "Janis Joplin-Piece Of My Heart"
Janis Joplin-Piece Of My Heart
% lsearch $liveme "-"
-1
% set liveme [join [split $liveme "-"] " - "]
Janis Joplin - Piece Of My Heart
% set pos [lsearch $liveme "-"]
2
% set liveme1 [lrange $liveme 0 [expr $pos - 1]]
Janis Joplin
% set liveme2 [lrange $liveme [expr $pos + 1] end]
Piece Of My Heart
And a code that accept both formats and gives the same result:

Code: Select all

set liveme "Janis Joplin-Piece Of My Heart"

set pos [lsearch $liveme "-"]
if {$pos == -1} {
	set liveme [join [split $liveme "-"] " - "]
	set pos [lsearch $liveme "-"]
}

set liveme1 [lrange $liveme 0 [expr $pos - 1]]
set liveme2 [lrange $liveme [expr $pos + 1] end]
Works regardless of the format, meaning with or without space.
Once the game is over, the king and the pawn go back in the same box.
j
juanamores
Master
Posts: 317
Joined: Sun Mar 15, 2015 9:59 am

Post by juanamores »

That's why I like programming so much, and especially eggdrop.

The same results can be obtained in several ways.

Thanks to silentziler and caesar for sharing alternatives. :D
If you do not understand my ideas is because I can not think in English, I help me with Google Translate. I only speak Spanish. Bear with me. Thanks :)
Post Reply