View previous topic :: View next topic |
Author |
Message |
juanamores Master
Joined: 15 Mar 2015 Posts: 317
|
Posted: Tue Jun 13, 2017 11:49 am Post subject: Remove a concrete blank character |
|
|
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: | 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:
Quote: | 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: | 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:
Quote: | 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:
Quote: | <artistName>Janis Joplin</artistName>
<songName>Piece Of My Heart</songName> |
EDIT: Fixed!
Code: | 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  |
|
Back to top |
|
 |
silentziler Voice
Joined: 09 Sep 2015 Posts: 10
|
Posted: Wed Jun 21, 2017 6:37 pm Post subject: Re: Remove a concrete blank character |
|
|
Alternative:
regsub -all { <} $liveme1 {<} liveme1
regsub -all {> } $liveme2 {>} liveme2
instead of using [string map] |
|
Back to top |
|
 |
caesar Ass Kicker

Joined: 14 Oct 2001 Posts: 3450 Location: Area 51
|
Posted: Thu Jun 22, 2017 12:41 am Post subject: |
|
|
First format:
Code: |
% 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: |
% 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: |
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. _________________ You may say anything about me, but at least don't misspell my name. xD |
|
Back to top |
|
 |
juanamores Master
Joined: 15 Mar 2015 Posts: 317
|
Posted: Thu Jun 22, 2017 9:46 am Post subject: |
|
|
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.  _________________ 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  |
|
Back to top |
|
 |
|