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.

looping?

Old posts that have not been replied to for several years.
Locked
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

here's the problem I can't seem to figure out...

I'm working with ini files in a script for storing info

[info]
1=blabla
2=blabla
7=blabla
12=blabla

now the problem is how to make the bot search trought [info] and output the info of 1,2,7 and 12, one on each line...(those numbers might be different in each section)

anyone able to help me on this?
Elen sila lúmenn' omentielvo
J
Jules_74

Post by Jules_74 »

Something like this?

Code: Select all

set read_ini [open $filename r]
while {![eof $read_ini]} {
	set data [gets $read_ini]
	if {[eof $read_ini]} {break}
	if {[string compare [lindex [split $data =] 0] $id] == 0} {
		putlog "Info for $id : [lrange [split $data =] 1 end] "
	}
}
close $read_ini
<font size=-1>[ This Message was edited by: Jules_74 on 2002-05-18 12:40 ]</font>
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

when working with inifiles the bot does not read to the end of the file. It read one specified [section] and one [item] under that.

[section]
item=value

so what I want it to do is to read one section multiple times and output the value of the different "items" , one on each lines... like if the items are nicknames...
it would reply : Nick 1(the item) is st00pid(the value of the item)

thx anyway :wink:
Elen sila lúmenn' omentielvo
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

If you could paste an example of the layout of the INI file (maybe some real data, with many 4 or 5 entries under 3 sections) and how you want the data (from example posted) presented, it will be clearer.
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

thx but it does not mather anymore.. I just made alot of procs :wink:
Actually the "items" are limited so I just had to make 25 small procs.. works fine.
Elen sila lúmenn' omentielvo
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

It would be simpler to get make a generic INI parser.

Code: Select all

proc read_ini {ini section item} {
  set fp [open $ini r]
  set insec 0
  set ret ""
  while {![eof $fp]} {
    set in [gets $fp]
    if {($insec)  || ([string match "[${section}]" $in])} {
      set insec 1
      if {[string match "[*]" $in]} { break }
      if {$item == [lindex [split $in =] 0]} {
        set ret [lindex [split $in =] 1]
        break
      }
    }
  }
  catch {close $fp}
  return $ret
}
My Tcl is a little rusty, but this should parse a INI file, and return a item, under a certain section, while ignoring items named the same, under other sections.
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

already got that :wink:
The problem was not the reading part... but how to make one command read through the same section 25 times and output the value of the items, one item on each line in say.. a privmsg

..but as I said.. I used several small procs and that works fine actually. Easier to modify the bot's reply on each item :smile:

Thx anyway :smile:

_________________
Elen sila lúmenn' omentielvo

<font size=-1>[ This Message was edited by: Papillon on 2002-05-21 09:39 ]</font>
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Code: Select all

proc read_ini_section {ini section} {
  set fp [open $ini r]
  set insec 0
  set ret {}
  while {![eof $fp]} {
    set in [gets $fp]
    if {($insec)  || ([string match "[${section}]" $in])} {
      set insec 1
      if {[string match "[*]" $in]} { break }
      if {[llength [split $in =]] == 2} {
lappend ret [list [lindex [split $in =] 0][lindex [split $in =] 1]] }
    }
  }
  catch {close $fp}
  return $ret
}
This is a generic INI section reader.

It can be used to populate a Tcl array, or simply use a foreach loop to use the return value.

EG

Code: Select all

array set array1 [read_ini_section file.ini section1]
EG

Code: Select all

foreach {name value} [read_ini_section file.ini section1] {
  puthelp "PRIVMSG $chan :"${name}" is set to "${value}""
}
Locked