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] split at given position and repeat

Help for those learning Tcl or writing their own scripts.
Post Reply
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

[SOLVED] split at given position and repeat

Post by raider2k »

subject this time:

a list, containing different number of objects everytime
want it to be split after (in example) 8 objects, output those first 8 objects to channel and repeat that step for the next 8 objects until all the objects have been sent to channel.

example:

list contains: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

how i want it to output in channel:
numbers: 1 2 3 4 5 6 7 8
numbers: 9 10 11 12 13 14 15 16
numbers: 17 18 19 20 21 22 23 24

i came across this one today

http://forum.egghelp.org/viewtopic.php?t=11522

while searching the forum but it splits by using a given string length, so its not what helps me working with a list. also tried to understand the code and maybe change it to my needs but im totally lost and need help please to be honest.
Last edited by raider2k on Thu May 21, 2009 2:58 pm, edited 1 time in total.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

I'd probably do it something like this:

Code: Select all

foreach [list a b c d e f g h] $thelist {
 puthelp "PRIVMSG #thechannel :$a $b $c $d $e $f $g $h"
}
Other options would be using lrange, such as:

Code: Select all

set len [llength $thelist]
set i 0
while {$i < $len} {
 puthelp "PRIVMSG #thechannel :[join [lrange $thelist $i [incr i 7]]]"
 incr i
}
NML_375
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

lets try example 2 of yours since example one looks nice but wouldnt be working with a list containing a different number of objects everytime

edit:

you are a genius nml375 :>
thanks for helping again :)
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

The first example would work just fine for any valid tcl-list, irregardless of length. The only "kink" would be that the "value-less" iteration variables would contain "" in the last loop iteration..

Code: Select all

% set thelist [list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
% foreach [list a b c d e f g h] $thelist {
 puts stdout "PRIVMSG #thechannel :$a $b $c $d $e $f $g $h"
}
PRIVMSG #thechannel :1 2 3 4 5 6 7 8
PRIVMSG #thechannel :9 10 11 12 13 14 15 16
PRIVMSG #thechannel :17 18 19 20 21 22 23
Compared to:

Code: Select all

% set thelist [list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
% set len [llength $thelist]
% set i 0
% while {$i < $len} {
  puts stdout "PRIVMSG #thechannel :[lrange $thelist $i [incr i 8]]"
}
PRIVMSG #thechannel :1 2 3 4 5 6 7 8 9
PRIVMSG #thechannel :9 10 11 12 13 14 15 16 17
PRIVMSG #thechannel :17 18 19 20 21 22 23
Notice how both examples handle the last iteration just fine, despite being only 7 items left in the list. Which way to go, mainly depends on whether you need to access each list item separately, or as a block, within the loop body.
NML_375
Post Reply