egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[SOLVED] split at given position and repeat

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
raider2k
Op


Joined: 01 Jan 2008
Posts: 140

PostPosted: Thu May 21, 2009 2:01 pm    Post subject: [SOLVED] split at given position and repeat Reply with quote

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
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Thu May 21, 2009 2:37 pm    Post subject: Reply with quote

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
View user's profile Send private message
raider2k
Op


Joined: 01 Jan 2008
Posts: 140

PostPosted: Thu May 21, 2009 2:49 pm    Post subject: Reply with quote

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 Smile
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Thu May 21, 2009 3:03 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber