Re: Arrays
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: Arrays

From: Gerald W. Lester <Gerald.Lester@cox.net>
Date: Fri Dec 30 2005 - 20:04:26 CET

krithiga81@yahoo.com wrote:
> Gerald
> Thanks. I checked the inifile package and it is for opening ini
> files. My config file is a plain text file where there are section
> which I need to have arrays for each section and then access the
> elements in the arrays based on an index. Also I need to pass these to
> another procedure. How do I accomplish this? I have put my code I have
> so far.

Rereading your previous post, it looks your config files looks like:
[OPTIONS]
LASTFILE 0
LICENSE yes
LASTRECORD .

In other words a inifile (which is just a plain text file) without the "="
separating the key from the value.

Assuming you can't, for whatever reason, change the format of the config
file to include the "=" and assuming you want the array to be equivelant to:

array set OPTIONS {
     LASTFILE 0
     LICENSE yes
     LASTRECORD .
}

Then the following (modified from the inifile package of TclLib) should work
for you:

proc loadfile {channel} {
     set cur {}
     set com {}
     set char {#}
     seek $channel 0 start

     foreach line [split [read $channel] "\n"] {
         set line [string trim $line]
         if { [string match "$char*" $line] } {
             ##
             ## Comment line, so ignore
             ##
             continue
         } elseif { [string match {\[*\]} $line] } {
             ##
             ## Section line so process
             ##
             set cur [string trim [string range $line 1 end-1]]
             if { $cur == "" } { continue }
             catch {unset ::$cur}
             array set ::$cur {}
         } elseif {[string equal $cur {}] || [string equal $line {}]} {
             ##
             ## Empty line or line before first section, so ignore
             ##
             continue
         } else {
             ##
             ## key value pair
             ##
             set break [tcl_endOfWord $line 0]
             set key [string range $line 0 [incr break -1]]
             set value [string trim [string range $line [incr break 1] end]]
             set ::${cur}($key) $value
         }
     }
}

If it was not such an odd time of year, I would have assumed this was a
freshman level computer science homework.
Received on Tue Jan 3 03:09:50 2006