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.

[SOLVED!] building a FIFO (revolving) varable string

Help for those learning Tcl or writing their own scripts.
Post Reply
d
dj-zath
Op
Posts: 134
Joined: Sat Nov 15, 2008 6:49 am
Contact:

[SOLVED!] building a FIFO (revolving) varable string

Post by dj-zath »

Hi everyone!

This ones going to take a little explaining..

the code comes in 2 parts.. in 2 seperate "modules" in my eggdrop..

the second part takes a varable (string) and then "arrays" it and, if "matches" the current $arg, returns 0 and then outputs a line to the chatroom.. this part of the script works FLAWLESS

heres the element of the code I'm referring to.. its a PART of a much larger command set and it would be meaningless to display the whole command set; this part is enough to get the jist, however...

Code: Select all


} elseif {([catch {foreach I [split $DataPQ "\r\n"] {if {($I == $arg)} {return "0";};};}])} {
    putquick "NOTICE $nick :Request query rejected - Query Item has just been played!";
};

that was the EASY part!

now, what I need to do is generate $DataPQ.. which is simply a list of 10 numbers of the "last played items" which are derived from another varable ($DetPY) on a "once-per-entry" basis at the time each item is played...

again, this is just a partial..

Code: Select all

set DataPY "$VarD\r\n";
set DataPQ [append DataPQ $DataPY];
this creates a "appended" list of "DataPYs".. again, easy..

($DataPY will be a number of a "playing" item)

Now, the HARD part...

I need to create this list in a way so that each item can be "singled out" AND that the varable $DataPQ holds SPECIFICALLY 10 items- and for each item that enters thereafter, will cause the FIRST item to be ejected!

example:
assume 10 items..
"100 101 102 103 104 105 106 107 108 109 110"

now comes in 111...

"101 102,103 104 105 106 107 108 109 110 111"

and so on and so on...

admittingly, I'm spent a considerable amout of time on this "issue" and that I'm now cranky and tired over it.. any advice and suggestions would be appreciated!

thanks!

-DjZ-
:) :)
Last edited by dj-zath on Thu Aug 19, 2010 9:57 am, edited 1 time in total.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Use lsearch to search for the entity in the list, rather than a foreach-loop..

Use lappend to append items at the end of the list..

Use lrange to select a sublist not containing the first item..

This code assumes we keep DataPQ in a valid list structure rather than a newline terminated format...

Code: Select all

#see if $value is in our list DataPQ:
if {[lsearch -exact $DataPQ $value] < 0} {
  #it's not in the list...

  #add $value to our list DataPQ:
  lappend DataPQ $value

  #remove all but the last 10 items of the list:
  set DataPQ [lrange $DataPQ end-9 end]
}
NML_375
d
dj-zath
Op
Posts: 134
Joined: Sat Nov 15, 2008 6:49 am
Contact:

Post by dj-zath »

Hi Nml!

thanks for the reply!

I have tried to play with "lappend" and creating a list... and couldn't get it working hehe In looking at your code, however, I do see what I wasn't doing right (so to speak)

I'll let you know what I come up with.. again, thanks for the example..

(I have also "modularized" the initial code- and broke it up into smaller parts- each for its particular funchtion(s), and, thus, I was "remiving some bugs" and learning more TCL in the process hehe)

-DjZ-
:) :)
d
dj-zath
Op
Posts: 134
Joined: Sat Nov 15, 2008 6:49 am
Contact:

Post by dj-zath »

DING DING DING!

Although Nml's code didn't work initally, it DID prove to offer some suggestions to the part of code that DOES work.. and VERY WELL!

I'll explain:

first, here's the "cleaned up" version of the working code (note: the conditionals are based on code before this..)

Code: Select all

if {($VarB != "-1")&&($VarD != [expr $PromoCnt + $VoiceCnt])&&([lsearch -exact $DataPY $VarD] < "0")} {
  set VarA [lappend DataPY "$VarDÇÇ"];
  set VarA [lrange $VarA end-9 end];
  set DataPY [string map {"\{" "" "\}" ""} $VarA];
};
the first thing to note is I eliminated "DataPQ" and used DataPY for both "playing" and "been played" since they contain the SAME items initially.. so this saves some extra operations

the other problem I had with Nml's code was that I could not get the list to display/read correctly AFTER the "lrange" command since it stripped out EVERYTHING- including my "markers"! -OR- the command inserted brackets for each result.. which would not read in the array.. I either could not get them to parse correctly in the later-appointed array or another "lsearch" operation for some reason.. I added a string map command to strip off the brackets.

after a few hours of pulling out hair, I went BACK to my orginal code- but instead of using "\r\n" as a marker, I used "ÇÇ" instead.. and then made the appropriate corrections throughout my code (I still could NOT get the "lsearch" command to work after the lreange command however)

and now, the second part:

(note: this is just a snippet from the master command set that pretains to this particular function)

Code: Select all

} elseif {([catch {foreach I [split $DataPY "ÇÇ"] {if {($I == $arg)} {return "0";};};}])} {
   putquick "NOTICE $nick :Request query rejected - Query item playing or just been played!";
this works VERY well...]

and now, that I have working varables of "last-played items" I can put them into the random playlist generators to prevent "dupes".. thats NEXT! :-)

a note:

THIS version of the code... :

Code: Select all

} elseif {[lsearch -exact $DataPY $arg] > 0} {
   putquick "NOTICE $nick :Request query rejected - Query item playing or just been played!";
... did NOT work at all when using the code from Nml..
(though I can't understand WHY the list didn't work properly- it just would NOT parse/detect; always resulted in "0" or null string)

but, I'm happy in the fact that now the "anti-abuse" system is up and running.. now trolls can't:
1, pick a song to play over and over,
2. can't pick a song to play after the song played or
3. pick a song thats playing to be play again..

Thanks Nml for your invaluable insight! you REALLY helped me get this project off the ground!

I now have the first and only version of an eggdrop that can DIRECTLY control the Ots RAC system and be intelligent to do it securely, and with some SMARTS to it! in short, you are DA MAN! oh, and Speechles and Caesar too! ALL of you's.. GREAT WORK! AWESOME!

-DjZ-
:) :)
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

First off...
Purge "string map {"\{" "" "\}" ""} ..." from your memory, and curse anyone who even tries to make you remember it again...

Next... why bother with "markers" or whatever you prefer calling them?
The list structure (list, not arrays...) simply works out of the box - without bothering to add, parse, and split some character of your choosing...

Also, if you attach something to the string you append to the list, rather obviously the lsearch will fail...
NML_375
Post Reply