Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading parameter file 1

Status
Not open for further replies.

Filipvdr

Programmer
Mar 11, 2010
25
BE
Hello, i'm new at this so this could be a stupid question.

So i need to read parameters out of a file

paramater.txt looks like this:
user = OWBREPOS
paswoord = OWBREPOS
host = Filip
service = FILIP2
project = MY_PROJECT
moduleA = BI_module

it should be
$user with value OWBREPOS
$host with value Filip

thanks in advance
 
Hi

Personally I would use an array :
Code:
[b]set[/b] fil [teal][[/teal][b]open[/b] [green][i]"paramater.txt"[/i][/green] r[teal]][/teal]

[b]while[/b] [teal]{[/teal][teal]![[/teal][b]eof[/b] [navy]$fil[/navy][teal]][/teal][teal]}[/teal] [teal]{[/teal]
  [b]set[/b] part [teal][[/teal][b]split[/b] [teal][[/teal][b]gets[/b] [navy]$fil[/navy][teal]][/teal] [green][i]"="[/i][/green][teal]][/teal]
  [b]set[/b] conf[teal]([[/teal][b]string[/b] trimright [teal][[/teal][b]lindex[/b] [navy]$part[/navy] [purple]0[/purple][teal]]])[/teal] [teal][[/teal][b]string[/b] trimleft [teal][[/teal][b]lindex[/b] [navy]$part[/navy] [purple]1[/purple][teal]]][/teal]
[teal]}[/teal]

[b]close[/b] [navy]$fil[/navy]

[b]puts[/b] [green][i]"user : $conf(user)"[/i][/green]
[b]puts[/b] [green][i]"host : $conf(host)"[/i][/green]
Code:
user : OWBREPOS
host : Filip

Feherke.
 
works great, thanks.
next step is to make the parameter file as following:

# General Parameters
user = OWBREPOS
host = filip

# Other Parameters

==> so i need something to say: skip the white lines and the lines with a #
==>i could use your function trimleft i think? any hints are welcome, i will try this first myself
 
Hi

You could just use the previous code and ignore the garbage accumulated in $conf.
Code:
[b]set[/b] fil [teal][[/teal][b]open[/b] [green][i]"paramater.txt"[/i][/green] r[teal]][/teal]

[b]while[/b] [teal]{[/teal][teal]![[/teal][b]eof[/b] [navy]$fil[/navy][teal]][/teal][teal]}[/teal] [teal]{[/teal]
  [highlight][b]set[/b] line[/highlight] [teal][[/teal][b]gets[/b] [navy]$fil[/navy][teal]][/teal]
  [highlight][b]if[/b] [teal]{[/teal][navy]$line[/navy][teal]==[/teal][green][i]""[/i][/green] [teal]||[/teal] [teal][[/teal][b]string[/b] range [navy]$line[/navy] [purple]0[/purple] [purple]0[/purple][teal]]==[/teal][green][i]"#"[/i][/green][teal]}[/teal] [teal]{[/teal] [b]continue[/b] [teal]}[/teal][/highlight]
  [b]set[/b] part [teal][[/teal][b]split[/b] [highlight][navy]$line[/navy][/highlight] [green][i]"="[/i][/green][teal]][/teal]
  [b]set[/b] conf[teal]([[/teal][b]string[/b] trimright [teal][[/teal][b]lindex[/b] [navy]$part[/navy] [purple]0[/purple][teal]]])[/teal] [teal][[/teal][b]string[/b] trimleft [teal][[/teal][b]lindex[/b] [navy]$part[/navy] [purple]1[/purple][teal]]][/teal]
[teal]}[/teal]

[b]close[/b] [navy]$fil[/navy]

Feherke.
 
nice, thanks :)

my goal is to create a parameter file with differenct sections
the global variables i always need to connect

then i have mapping variables, or table variables
and in tcl script A i need global + mapping
but in script B i need global + table

any suggestions how i should handle this?


 
Hi

Filipvdr said:
and in tcl script A i need global + mapping
but in script B i need global + table
You mean, to not load the entire configuration, just the given sections ?

So how are the sections delimited ? Only those comments ? Personally I prefer the ini notation with brackets ( [] ), like [section name].

Feherke.
 
indeed, load the given sections

parameter file looks like this atm:

################################################################################
# Oracle Warehouse Builder Parameter File #
# TEST #
# OMBPlus #
# OMBPlus #
################################################################################

################################################################################
# GLOBALE VARIABELs #
################################################################################

user = OWBREPOS
password = OWBREPOS
host = Filip
service = FILIP2
project = MY_PROJECT
port = 1522
moduleA = DWH_MODULE

################################################################################
# MAPPING VARIABELs #
################################################################################

module = name
 
Hi

I do not really like that, as there is no clear difference between section names and comments. Anyway.
Code:
[b]proc[/b] loadconf [teal]{[/teal]name section[teal]}[/teal] [teal]\[/teal]
[teal]{[/teal]
  [b]global[/b] conf

  array unset conf

  [b]set[/b] needed [purple]0[/purple]

  [b]set[/b] fil [teal][[/teal][b]open[/b] [navy]$name[/navy] r[teal]][/teal]

  [b]while[/b] [teal]{[/teal][teal]![[/teal][b]eof[/b] [navy]$fil[/navy][teal]][/teal][teal]}[/teal] [teal]{[/teal]
    [b]set[/b] line [teal][[/teal][b]gets[/b] [navy]$fil[/navy][teal]][/teal]
    [b]if[/b] [teal]{[/teal][navy]$line[/navy][teal]==[/teal][green][i]""[/i][/green] [teal]||[/teal] [teal][[/teal][b]string[/b] range [navy]$line[/navy] [purple]0[/purple] [purple]1[/purple][teal]]==[/teal][green][i]"##"[/i][/green][teal]}[/teal] [teal]{[/teal] [b]continue[/b] [teal]}[/teal]
    [b]if[/b] [teal]{[/teal][teal][[/teal][b]string[/b] range [navy]$line[/navy] [purple]0[/purple] [purple]0[/purple][teal]]==[/teal][green][i]"#"[/i][/green][teal]}[/teal] [teal]{[/teal]
      [b]set[/b] needed [purple]0[/purple]
      [b]foreach[/b] one [navy]$section[/navy] [teal]{[/teal]
        [b]if[/b] [teal]{[/teal][teal][[/teal][b]string[/b] first [navy]$one[/navy] [navy]$line[/navy][teal]]!=-[/teal][purple]1[/purple][teal]}[/teal] [teal]{[/teal] [b]set[/b] needed [purple]1[/purple] [teal]}[/teal]
      [teal]}[/teal]
      [b]continue[/b]
    [teal]}[/teal]
    [b]if[/b] [teal]{[/teal][navy]$needed[/navy][teal]==[/teal][purple]0[/purple][teal]}[/teal] [teal]{[/teal] [b]continue[/b] [teal]}[/teal]
    [b]set[/b] part [teal][[/teal][b]split[/b] [navy]$line[/navy] [green][i]"="[/i][/green][teal]][/teal]
    [b]set[/b] conf[teal]([[/teal][b]string[/b] trimright [teal][[/teal][b]lindex[/b] [navy]$part[/navy] [purple]0[/purple][teal]]])[/teal] [teal][[/teal][b]string[/b] trimleft [teal][[/teal][b]lindex[/b] [navy]$part[/navy] [purple]1[/purple][teal]]][/teal]
  [teal]}[/teal]

  [b]close[/b] [navy]$fil[/navy]
[teal]}[/teal]

loadconf [green][i]"paramater.txt"[/i][/green] [teal]{}[/teal]

[b]puts[/b] [teal][[/teal]array names conf[teal]][/teal]

loadconf [green][i]"paramater.txt"[/i][/green] [teal]{[/teal][green][i]"GLOBALE VARIABELs"[/i][/green][teal]}[/teal]

[b]puts[/b] [teal][[/teal]array names conf[teal]][/teal]

loadconf [green][i]"paramater.txt"[/i][/green] [teal]{[/teal][green][i]"MAPPING VARIABELs"[/i][/green][teal]}[/teal]

[b]puts[/b] [teal][[/teal]array names conf[teal]][/teal]

loadconf [green][i]"paramater.txt"[/i][/green] [teal]{[/teal][green][i]"GLOBALE VARIABELs"[/i][/green] [green][i]"MAPPING VARIABELs"[/i][/green][teal]}[/teal]

[b]puts[/b] [teal][[/teal]array names conf[teal]][/teal]
Code:
port service host moduleA project password user
module
port service module host moduleA project password user

Feherke.
 
yes, you're right. this way you can't make comments
what is a better way?

I was thinking about:
################################################################################
# GLOBAL VARIABELs #
################################################################################

[START_GLOBAL]

user = OWBREPOS
password = OWBREPOS
host = Filip
service = FILIP2
project = MY_PROJECT
port = 1522

[END_GLOBAL]

if you have better suggestion, name it
 
Hi

Well, unless you want multilevel data structure, like this :
Code:
[START_GLOBAL]

[START_GLOBAL_HUMAN_INFO]
user = OWBREPOS
password = OWBREPOS
[END_GLOBAL_HUMAN_INFO]

[START_GLOBAL_MACHINE_INFO]
host = Filip
service = FILIP2
port = 1522
[END_GLOBAL_MACHINE_INFO]

project = MY_PROJECT

[END_GLOBAL]
I see no need to mark the end of the section. It simply ends where the next one starts.

So I would modify the file like this :
Code:
################################################################################
#                 GLOBALE VARIABELs                   #            
################################################################################

[highlight][GLOBALE VARIABEL][/highlight]
user = OWBREPOS
password = OWBREPOS
host = Filip
service = FILIP2
project = MY_PROJECT
port = 1522
moduleA = DWH_MODULE

################################################################################
#                 MAPPING VARIABELs                   #            
################################################################################

[highlight][MAPPING VARIABEL][/highlight]
module = name
And the script like this :
Code:
[b]proc[/b] loadconf [teal]{[/teal]name section[teal]}[/teal] [teal]\[/teal]
[teal]{[/teal]
  [b]global[/b] conf

  array unset conf

  [b]set[/b] needed [purple]0[/purple]

  [b]set[/b] fil [teal][[/teal][b]open[/b] [navy]$name[/navy] r[teal]][/teal]

  [b]while[/b] [teal]{[/teal][teal]![[/teal][b]eof[/b] [navy]$fil[/navy][teal]][/teal][teal]}[/teal] [teal]{[/teal]
    [b]set[/b] line [teal][[/teal][b]gets[/b] [navy]$fil[/navy][teal]][/teal]
    [b]if[/b] [teal]{[/teal][navy]$line[/navy][teal]==[/teal][green][i]""[/i][/green] [teal]||[/teal] [teal][[/teal][b]string[/b] range [navy]$line[/navy] [purple]0[/purple] [purple]0[/purple][teal]]==[/teal][green][i]"#"[/i][/green][teal]}[/teal] [teal]{[/teal] [b]continue[/b] [teal]}[/teal]
    [b]if[/b] [teal]{[/teal][teal][[/teal][b]string[/b] match [teal]{[/teal][teal]\[?*\]*[/teal][teal]}[/teal] [navy]$line[/navy][teal]][/teal][teal]}[/teal] [teal]{[/teal]
      [b]set[/b] needed [purple]0[/purple]
      [b]foreach[/b] one [navy]$section[/navy] [teal]{[/teal]
        [b]if[/b] [teal]{[/teal][teal][[/teal][b]string[/b] first [navy]$one[/navy] [navy]$line[/navy][teal]]==[/teal][purple]1[/purple][teal]}[/teal] [teal]{[/teal] [b]set[/b] needed [purple]1[/purple] [teal]}[/teal]
      [teal]}[/teal]
      [b]continue[/b]
    [teal]}[/teal]
    [b]if[/b] [teal]{[/teal][navy]$needed[/navy][teal]==[/teal][purple]0[/purple][teal]}[/teal] [teal]{[/teal] [b]continue[/b] [teal]}[/teal]
    [b]set[/b] part [teal][[/teal][b]split[/b] [navy]$line[/navy] [green][i]"="[/i][/green][teal]][/teal]
    [b]set[/b] conf[teal]([[/teal][b]string[/b] trimright [teal][[/teal][b]lindex[/b] [navy]$part[/navy] [purple]0[/purple][teal]]])[/teal] [teal][[/teal][b]string[/b] trimleft [teal][[/teal][b]lindex[/b] [navy]$part[/navy] [purple]1[/purple][teal]]][/teal]
  [teal]}[/teal]

  [b]close[/b] [navy]$fil[/navy]
[teal]}[/teal]

Feherke.
 
proc loadconf {name section} \
{
global conf
array unset conf
set needed 0
set fil [open $name r]
while {![eof $fil]} {
set line [gets $fil]
if {$line=="" || [string range $line 0 0]=="#"} { continue }
if {[string match {\[?*\]*} $line]} {
set needed 0
foreach one $section {
if {[string first $one $line]==1} { set needed 1 }
}
continue
}
if {$needed==0} { continue }
set part [split $line "="]
set conf([string trimright [lindex $part 0]]) [string trimleft [lindex $part 1]]
}
close $fil
}
loadconf "C:/parameter.txt" {}
puts [array names conf]
# loadconf "paramater.txt" {"STARTGLOBAL"}
# puts [array names conf]
# loadconf "paramater.txt" {"STARTMODULES"}
# puts [array names conf]
# loadconf "paramater.txt" {"STARTGLOBAL" "STARTMODULES"}
# puts [array names conf]

this is my code now..
but it gives an error: "conf isn't an array
 
Hi

And what happens if you [tt]set[/tt] it immediately before ?
Code:
[b]proc[/b] loadconf [teal]{[/teal]name section[teal]}[/teal] [teal]\[/teal]
[teal]{[/teal]
  [b]global[/b] conf
  [highlight]array [b]set[/b] conf [teal]{}[/teal][/highlight]
  array unset conf
  [b]set[/b] needed [purple]0[/purple]
  [b]set[/b] fil [teal][[/teal][b]open[/b] [navy]$name[/navy] r[teal]][/teal]
  [b]while[/b] [teal]{[/teal][teal]![[/teal][b]eof[/b] [navy]$fil[/navy][teal]][/teal][teal]}[/teal] [teal]{[/teal]
    [b]set[/b] line [teal][[/teal][b]gets[/b] [navy]$fil[/navy][teal]][/teal]
    [b]if[/b] [teal]{[/teal][navy]$line[/navy][teal]==[/teal][green][i]""[/i][/green] [teal]||[/teal] [teal][[/teal][b]string[/b] range [navy]$line[/navy] [purple]0[/purple] [purple]0[/purple][teal]]==[/teal][green][i]"#"[/i][/green][teal]}[/teal] [teal]{[/teal] [b]continue[/b] [teal]}[/teal]
    [b]if[/b] [teal]{[/teal][teal][[/teal][b]string[/b] match [teal]{[/teal][teal]\[?*\]*[/teal][teal]}[/teal] [navy]$line[/navy][teal]][/teal][teal]}[/teal] [teal]{[/teal]
      [b]set[/b] needed [purple]0[/purple]
      [b]foreach[/b] one [navy]$section[/navy] [teal]{[/teal]
        [b]if[/b] [teal]{[/teal][teal][[/teal][b]string[/b] first [navy]$one[/navy] [navy]$line[/navy][teal]]==[/teal][purple]1[/purple][teal]}[/teal] [teal]{[/teal] [b]set[/b] needed [purple]1[/purple] [teal]}[/teal]
      [teal]}[/teal]
      [b]continue[/b]
    [teal]}[/teal]
    [b]if[/b] [teal]{[/teal][navy]$needed[/navy][teal]==[/teal][purple]0[/purple][teal]}[/teal] [teal]{[/teal] [b]continue[/b] [teal]}[/teal]
    [b]set[/b] part [teal][[/teal][b]split[/b] [navy]$line[/navy] [green][i]"="[/i][/green][teal]][/teal]
    [b]set[/b] conf[teal]([[/teal][b]string[/b] trimright [teal][[/teal][b]lindex[/b] [navy]$part[/navy] [purple]0[/purple][teal]]])[/teal] [teal][[/teal][b]string[/b] trimleft [teal][[/teal][b]lindex[/b] [navy]$part[/navy] [purple]1[/purple][teal]]][/teal]
  [teal]}[/teal]
  [b]close[/b] [navy]$fil[/navy]
[teal]}[/teal]

Feherke.
 
Hi

Filipvdr said:
same errormessage at same line
By "same line" you mean
[ul]
[li]the same line by number ( now containing [tt]array set conf {}[/tt] )[/li]
[li]the same line by content ( just moved one line down after insertion )[/li]
[/ul]
By the way, are you using that $conf somewhere else in your script ?

Feherke.
 
the same line by content

proc loadconf {name section} \
{
global conf
array set conf {}
puts "Test"
array unset conf
puts "Test2"...

it only displays Test

I use $conf when i declare my variables
set user $conf(user)
set host $conf(host)
set pass $conf(password)
set service $conf(service)
set project $conf(project)
set moduleA $conf(mod)
set port $conf(port)

thanks!!!
 
i have no idea, i make my tcl files in notepad++ and i run them via OMBPlus (i have to make some scripts for oracle warehouse builder)
 
Hi

Ok, got you.

Will you need to call loadconf multiple times in the same script, with different parameters ? If not, you can simply remove the [tt]array unset[/tt] line. It is there just to clear $conf in case it was used previously with other parameters.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top