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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with variables

Status
Not open for further replies.

Jboy123

MIS
Jun 8, 2004
24
0
0
CH
Hi

Am hoping someone can help with some probs I'm having with variables. I'm more of a perl scripter and am fairly new to tcl. I have a script comprised of various procs and a main. The main reads in a property file and sets several variables based on contents of that file then calls procs.

There are 3 variable types I'm interested in:

$vhostname
$aliaslist
$portlist

I've got foreach loop that loops through the list of $vhostnames and for each iteration sets the value of $aliaslist and $portlist. I'm intending that the values of $aliaslist and $portlist will be equal to 2 variables of same name read in from the property file. In the property file these variable names are prefixed with the value of $vhostname. Perhaps this will make more sense:

If $vhostname=myvhost1 there would be 2 further variables in the property file as follows:

myvhost1_aliaslist
myvhost1_portlist

Likewise if $vhostname=myvhost2 the other variables would be :

myvhost2_aliaslist
myvhost2_portlist

Okay, here's how I am trying to acheive by using concat and join but obviously getting it wrong. (Apologies in advance to any seasoned tcl'rs if this is truly horrible but I'm learning:)

foreach vhostname $vhostnames {

# Set the values of $aliaslist $portlist based on
# value of $vhostname

set concatalias [concat $vhostname _aliaslist]
set concatport [concat $vhostname _portlist]
# Use join to remove spaces in variable name
set aliases [join $concatalias ""]
set ports [join $concatport ""]

# Now set $aliaseList $portList to values supplied in property file global variables
set aliaslist [list $aliases]
set portlist [list $ports]

puts "Value of alias list is $aliaslist"
puts "Value of port list is $portlist"

}

The two final puts, instead of returning the values of the variables specified in the property file are simply returning the name of the variable e.g. myvhost1_aliaslist. I can understand why based on the code I've written but would really appreciate any help to return values not the name.

Many thanks in advance
Jules
 
I can't really tell what you're trying to do. Let's say the value of vhostname is "abc". That is:
"set vhostname" would return "abc". Then you want the VALUE of concatalias to be "abc_aliaslist"? I'd do that by

set concatalias $vhostname
append concatalias _aliaslist

Now you have a variable named "concatalias" whose value is "abc_aliaslist".
Is that what you want? Then what?

Bob Rashkin
rrashkin@csc.com
 
Hi there

Many thanks for the reply - sorry my example isn't very clear.

My script reads in a property file that contains several key value pairs seaparated by '='. While reading in the file I split each line by '=' and thus assign values to variables.

One of the variables that I've assigned values to is for example $abc_aliaslist

$abc_aliaslist will for example have following values 1 2 3

What I'm trying to achieve (using your example) is that the variable "concatalias" would have the values 1 2 3 (i.e the values of the $abc_aliaslist rather than just name "abc_aliaslist"

Hope this makes a little more sense?

Thank-you
 
Please excuse me if I go over stuff you already know because I need to try to make this consistent.

Let's say your file has:
vhostname=George
var1=abc
var2=def
var3=ghi
var4=jkl
var5=mno

Then you read in that data:
set fid [open <filename> read]
set linelist [split [read $fid] \n]
close $fid

Then you loop through the list to set the variables:
foreach line $linelist {
set nmlist [split $line =]
set [lindex $nmlist 0] [lindex $nmlist 1]
}

Now you have those 5 variables set to their "namelist" values (to use OLD Fortran nomenclature). So far so good. However, if I get what you're trying to do, some of the values in the namelist will be lists, say:
var3="1 2 3"

This does not require any change to the reading or looping. Now, I'm guessing that what you want to do here is to make another list, say concatalias, whose vaues are:
George_1 George_2 George_3

If that is the case, I would do:
foreach alias $var3 {
set temp $vhostname
append temp _$alias
lappend concatalias $temp
}

Bob Rashkin
rrashkin@csc.com
 
Bob

Many thanks for your perserverance. What you've done is close but not quite what I'm after. Based on your example above the file that I read in, for example, could look like below. The values in the namelist will be lists as follows:

vhostnames="george bill bob"
george_aliaslist="georgie jorge juergen"
bill_aliaslist="billie will william"
bob_aliaslist="robert bobby ted"

If $vhostnames has the value "george" ther will always be a corresponding $george_aliaslist and if $vhostnames has the value "bill" there will always be a $bill_aliaslist and so on. The problem is that I don't know what variables are going to be in the file until runtime.

What I need to do at the simplest level is loop through $vhostnames and when george is the current $vhostname I want to be able to set a variable (for example $aliaslist) that contains the values of $george_aliaslist i.e. "georgie jorge juergen" and then then next iteration of the loop would be against "bill" and I would want my variable $aliaslist to have the values "billie will william" and the next iteration $aliaslist would get values "robert bobby ted" etc.

This is why I was trying to do the whole concat thing above to match the name of the variable in the file but this was of course just returning me the name of the variable but not the values in it.

Does this make any more sense??

Many thanks again.
Jules
 
OK. I think I have it.
I think you understand how to loop through whatever list you want so let's concentrate on the wierd variable assignment. Here's what I did to test it out:

% set vhostnames "george bill bob"
george bill bob
% set george_aliaslist "georgie jorge juergen"
georgie jorge juergen
% set bill_aliaslist "billie will william"
billie will william
% set bob_aliaslist "robert bobby ted"
robert bobby ted

(this is the set up)

% set vh [lindex $vhostnames 0]
george

(this simulated the first loop step)

% set vn $vh
george
% append vn _aliaslist
george_aliaslist

(now I have the correct variable name, only it's the value of a variable!)

% eval {set $vn}
georgie jorge juergen

(this is hopeful but not useful)

% set vn2 [eval {set $vn}];#this is the key step
georgie jorge juergen
% set vn2
georgie jorge juergen
%

(vn2 is the list you want!)

Bob Rashkin
rrashkin@csc.com
 
You can skip the "set vn $vh" step and just "append vh _aliaslist"


Bob Rashkin
rrashkin@csc.com
 
Bob

You are a star! Many many thanks for you help. "Eval" - of course!

I have one other question regarding global variables. I'll put the question below but please say if you think I should put in another thread.

All part of the same script. The script is comprised of various procs and a main. I read in the property file in the main and then call the various procs. My code is very slightly different but using your code above as an example:

foreach line $linelist {
set nmlist [split $line =]
set [lindex $nmlist 0] [lindex $nmlist 1]
}

So I read each line and build up a 'namelist'. I want these variables to be available to the the procs. I've done some reading and I need to make these variables global. Correct? To achieve this I thought I have to make them global in the main and then declare them as globals in the relevant procs. Does this sound right so far?

I think I also mentioned that I don't know what is in the properties file until runtime so I thought I should build up a list of the variables retrieved from my file and then in the proc loop through this this to make them global. See pseudo code below


# Main here

foreach line $linelist {
set nmlist [split $line =]
# Globalise all variables
global $nmlist
set [lindex $nmlist 0] [lindex $nmlist 1]
# Add all variables to list
lappend varlist $nmlist

}

# Call the proc

myProc

# The proc would then look like this

myProc {} {

# First thing it would do is make varaiables global
foreach variable $varlist {
global $variable
}
#then do some of the lovely stuff you helped me with before
}

This is not working - is this not possible or am I just not doing it correctly (again!). Any suggestions welcome.

Many many thanks once again
Jules
 
I'm not sure if you meant to say this the way you did but you don't declare variables to global in the main script. The main script is identically in the global scope. So you declare global variables to be global in the proc's that will use them (get and/or update their values).

Being careful that what goes in the following list are the NAMES of the variables and not their values, here's a way I have successfully passed the globals dynamically:

first, in the main you build a list:

set gblst {----insert globals here----}

then, in each proc:
global gblst
eval global $gblst


Bob Rashkin
rrashkin@csc.com
 
Bob

Thanks for everything. All working just fine now!

Thanks again
Jules
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top