| View previous topic :: View next topic |
| Author |
Message |
raider2k Op
Joined: 01 Jan 2008 Posts: 140
|
Posted: Thu May 21, 2009 2:01 pm Post subject: [SOLVED] split at given position and repeat |
|
|
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 |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Thu May 21, 2009 2:37 pm Post subject: |
|
|
I'd probably do it something like this:
| Code: | 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: | set len [llength $thelist]
set i 0
while {$i < $len} {
puthelp "PRIVMSG #thechannel :[join [lrange $thelist $i [incr i 7]]]"
incr i
} |
_________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
raider2k Op
Joined: 01 Jan 2008 Posts: 140
|
Posted: Thu May 21, 2009 2:49 pm Post subject: |
|
|
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  |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Thu May 21, 2009 3:03 pm Post subject: |
|
|
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: | % 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: | % 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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
|