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
 
i think i just read the file once in the same script with same parameters

is the following possible:
Parameter file:
MODULES:
name: ALL
or
name: A,B,C

when importing the variables, when all i call all modules in my project in Oracle WB
when A,B,C i split them into variables with the comma?

or is it easier to say:
MODULES:
name1: A
name2: B
name3: C

you've been a great help already, i'm going to test your last suggestion now
 
Ok, will formulate that later on.

loadconf "C:/parameter.txt" {}
puts [array names conf]
==> this piece of code should make the procedure loadconf load all variables, right?


proc loadconf {name section} \
{
global conf
# array set conf {}
set needed 0
set fil [open "C:/parameter.txt" 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]
==> error: can't read name: no such variable
 
Yes. But i still get error: can't read the variable name
 
Hi

Filipvdr said:
But i still get error: can't read the variable name
In which line ? Because in you last code is not even used.

This loads all sections if no section is specified :
Code:
[b]proc[/b] loadconf [teal]{[/teal]name section[teal]}[/teal] [teal]\[/teal]
[teal]{[/teal]
  [b]global[/b] conf
  array [b]set[/b] conf [teal]{}[/teal]
  [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]
      [highlight][b]if[/b] [teal]{[/teal][navy]$section[/navy][teal]==[/teal][teal]{}}[/teal] [teal]{[/teal][/highlight]
        [highlight][b]set[/b] needed [purple]1[/purple][/highlight]
      [highlight][teal]}[/teal] [b]else[/b] [teal]{[/teal][/highlight]
        [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]
      [highlight][teal]}[/teal][/highlight]
      [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.
 
ok, thanks,but can you tell me why he gives me an error:
can't read name, no such variable
on line: set fil [open $name r]

loadconf "C:/parameter.txt" {}
puts [array names conf]

while this path really exists!


When i enter the path in the procedure:
set fil [open "C:/parameter.txt" r]
it works, but then it got stuck on "can't read section, no such variable"

 
Hi

The only thing I can think to is that you accidentally altered the formal parameter in the [tt]proc[/tt] declaration.

Hmm... Or being an embedded interpreter, maybe the environment tries to use $name for something else. I would try to rename it to something else.

Feherke.
 
when i change the name i get the same error
but when i declare in the proc itselves my location of my file it hasn't a problem with that

so it has to have to something with how the parameters are distributed below the procedure, in to the procedure

i also get the same error for section
i can set the file location in the proc itself ,and the section also.

now this is my code:
--------------------

proc loadconf {} \
{
set locationfile "C:/parameter.txt"
set section ""
global conf
array set conf {}
set needed 0
set fil [open $locationfile r]

while {![eof $fil]} {
set line [gets $fil]
if {$line=="" || [string range $line 0 0]=="#"} { continue }
if {[string match {\[?*\]*} $line]} {
puts "Test"
if {$section=={}} {
puts "Test2"
set needed 1
} else {
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 {}
puts [array names conf]

OUTPUT:
Test
Test2
Test
Test2
invalid command name "}"
 
Hi

I put those operations into a [tt]proc[/tt] to make it easier to call it multiple times in a script. If that is not needed, you can just take them out from the [tt]proc[/tt] so no more parameter messing be needed.

Feherke.
 
ok,nice! it works now
now i can start the real work: programming OMBPlus scripts for oracle warehouse builder,familiar with that?

my code to read my parameter file, connect to the repository and to give me a list of tables of a specific module (all thanks to you :D)

set section "GLOBALE VARIABEL MAPPING VARIABEL"
# set section "GLOBALE VARIABEL"
global conf
array set conf {}
set needed 0
set fil [open "C:/parameter.txt" r]
while {![eof $fil]} {
set line [gets $fil]
puts $line
if {$line=="" || [string range $line 0 0]=="#"} { continue }
if {[string match {\[?*\]*} $line]} {
if {$section=={}} {
puts "Test2"
set needed 1
} else {
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
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)
OMBCONNECT $user/$pass@$host:$port:$service
puts "Connected"
OMBCC '//$project//$moduleA'
set Tablelist [OMBLIST TABLES]
set i 1
foreach tableName $Tablelist {
puts " inspecting: $tableName"
incr i
}
OMBDCC

#OMBCOMMIT
#OMBDISCONNECT


 
Hi

Nope, I heard about OMBPlus only today from you.

One more thing. There is a difference between these two :
Code:
[gray]# list with 4 elements : "GLOBALE", "VARIABEL", "MAPPING" and "VARIABEL"[/gray]
[b]set[/b] section [green][i]"GLOBALE VARIABEL MAPPING VARIABEL"[/i][/green]

[gray]# list with 2 elements : "GLOBALE VARIABEL" and "MAPPING VARIABEL"[/gray]
[b]set[/b] section [teal]{[/teal][green][i]"GLOBALE VARIABEL"[/i][/green] [green][i]"MAPPING VARIABEL"[/i][/green][teal]}[/teal]
Currently it works, because the script checks if the given section name is contained by the current line starting at the second character of the line. If the condition would be stricter, would fail. By stricter condition I mean :
Code:
[b]foreach[/b] one [navy]$section[/navy] [teal]{[/teal]
[gray]#  if {[string first $one $line]==1} { set needed 1 }[/gray]
  [b]if[/b] [teal]{[/teal][navy]$line[/navy][teal]==[/teal][green][i]"[/i][/green][lime][i]\[[/i][/lime][green][i]$one[/i][/green][lime][i]\][/i][/lime][green][i]"[/i][/green][teal]}[/teal] [teal]{[/teal] [b]set[/b] needed [purple]1[/purple] [teal]}[/teal]
[teal]}[/teal]

Feherke.
 
oh ok,

now i will explain what i tried to explain a few posts before

i let people let in the parameter file but i'm looking for the best method

method A:
module1 = name1
module2 = name2
module3 = name3
...
OR!

method B:
modules = name1,name2,name3,...

i need to know how many modules there are if i want to loop them, which method should be the best?

(in method B, i will have to split them first)
 
Hi

As you saw above, a space separated list of words can be directly treated as a list. So if you change the commas ( , ) to spaces ( ) like this :
Code:
[method B2]
modules = name1[highlight] [/highlight]name2[highlight] [/highlight]name3
The code you have now should work, and this :
Code:
loadconf [green][i]"paramater.txt"[/i][/green] [teal]{[/teal][green][i]"method B2"[/i][/green][teal]}[/teal]

[b]for[/b] [teal]{[/teal][b]set[/b] i [purple]0[/purple][teal]}[/teal] [teal]{[/teal][navy]$i[/navy][teal]<[[/teal][b]llength[/b] [navy]$conf[/navy][teal]([/teal]modules[teal])][/teal][teal]}[/teal] [teal]{[/teal][b]incr[/b] i[teal]}[/teal] [teal]{[/teal]
  [b]puts[/b] [green][i]"$i : [lindex $conf(modules) $i]"[/i][/green]
[teal]}[/teal]
will output this :
Code:
0 : name1
1 : name2
2 : name3
The other way is more "complicated". And I would suggest a minor change even there : use identical keyword.
Code:
[method A2]
[highlight]modules[/highlight] = name1
[highlight]modules[/highlight] = name2
[highlight]modules[/highlight] = name3
Then you will have to modify the last line of the loader code, replacing the [tt]set[/tt] with [tt]lappend[/tt] :
Code:
[highlight][b]lappend[/b][/highlight] 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]
Then the same [tt]for[/tt] loop as above will have the the same output as above.

Note that I strongly recommend using structured data when possible. I mean lists and arrays instead on simple variables.

Feherke.
 
ok thanks.. i can read the variables but i have an OMB/logical problem:

OMBCONNECT $user/$pass@$host:$port:$service
puts "Connected"
OMBCC '//$project' (move in the tree to project)
for {set i 0} {$i<[llength $conf(modules)]} {incr i} {
puts "$i : [lindex $conf(modules) $i]"
set modulevar [lindex $conf(modules) $i]
OMBCC '$modulevar' (move to that module)
set Mappinglist [OMBLIST MAPPINGS]
set i 1
foreach mappingName $Mappinglist {
puts " reconciling: $mappingName"
incr i
}
OMBCC '..'
}

but it just searches the first module
 
ofcourse, i used the same i (variable) in the second for loop
as you see, my programming skills are weak actually
 
Next question:

Now i say:
DWH_Module, BI_MODULE, Module TEST_MODULE
DIM_CUSTOMERS_FRANCE_MAP, DIM_TIJD_MAP

but DIM_TIJD_MAP only belongs to BI_MODULE, i have to make a check..

OUTPUT:

0 : DWH_MODULE
reconciling: DIM_CUSTOMERS_FRANCE_MAP
reconciling: DIM_TIJD_MAP
1 : BI_MODULE
reconciling: DIM_CUSTOMERS_FRANCE_MAP
reconciling: DIM_TIJD_MAP
2 : TEST_MODULE
reconciling: DIM_CUSTOMERS_FRANCE_MAP
reconciling: DIM_TIJD_MAP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top