Cancel
Showing results for
Search instead for
Did you mean:

# Tcl - biggest value

Solution Partner Phenom

hello,

how can I pick biggest value from 3 or more variables?

for example:

A=1

B=2

C=3

What function can I use/ or best practise?

Thank you

---------------------------------------------
#♫ PB, 5ax, itnc, nx, vericut ♫ #
9 REPLIES 9

# Re: Tcl - biggest value

Solution Partner Phenom

Juraj

There must be several ways for this...

If there is more variables to compare, and in different cases there might be more or less of them, I would create array and sort it.

I think this is similar question:

https://stackoverflow.com/questions/14769783/how-can-i-sort-a-tcl-array-based-on-the-values-of-its-k...

and a bigger discussion here:

https://www.tek-tips.com/viewthread.cfm?qid=940827

Marek Pawlus, NCmatic

Development: C#, Tcl/Tk, CSE

# Re: Tcl - biggest value

Phenom

Can you be a little bit more specific? What looks like your variable?

you variable is equal to "1" or "A=1"?

What Tcl version are you using ?

Production: NX11.0.2.7,VERICUT 8.1.2
Development: Tcl/Tk, Gnuplot
www.adgroupe.com

# Re: Tcl - biggest value

Solution Partner Phenom

Hi,

I thank you @Marek_Pawlus,

I was thinking about something with list, sort and so on

But I was just curious if there is some other method

@mkn

Postbuilder  11

example:

global a b c d

set a 1

set b 2

set c 3

(I set these value, they can be different everytime)

So I want set to "d" the biggest one.

So I have to decide what one from a,b,c is the biggest.

/In this case result is c.

And then I just need set d \$c

---------------------------------------------
#♫ PB, 5ax, itnc, nx, vericut ♫ #

# Re: Tcl - biggest value

Phenom

Postbuilder  11

this is Greek to me...

You have can be found a solution. Here is how I would do... (Tcl 8.4)

```proc getMax {list} {
return [lindex [lsort -real \$list] end]
}

proc MOM____ {} {
global a b c d

set a 1
set b 2
set c 3
set x 20
set y 30
set z 40

set infoVar [info vars {[a-c]}]

set vars {}
foreach val \$infoVar {
lappend vars [set \$val]
}

set valMax [getMax \$vars]

foreach var \$infoVar val \$vars {
if {\$val == \$valMax} {
set d \$val
MOM_output_literal "set d \$\${var} ; # Variable global '\$var' in '\$infoVar' is the biggest value... equal to: \$val"
break
}

}}```
Production: NX11.0.2.7,VERICUT 8.1.2
Development: Tcl/Tk, Gnuplot
www.adgroupe.com

# Re: Tcl - biggest value

Solution Partner Phenom

thank you time I will pick something from it

---------------------------------------------
#♫ PB, 5ax, itnc, nx, vericut ♫ #

# Re: Tcl - biggest value

Gears Esteemed Contributor

there is also "lsort" for lists (See arguments for -integer / -real / etc.)

Ken Akerboom Sr CAx Systems Engr, Moog, Inc.
Production: NX10.0.3.5 MP16/TC11.2
I'd rather be e-steamed than e-diseaseled

# Re: Tcl - biggest value

Legend

Hi Juraj,

In case you do not have a solution yet, here you have another option to find the biggest value out.

```set A 1000
set B 10
set C 200

set aux_0 0

set max_value 0

set array {\$A \$B \$C}

for {set i 1} {\$i <= [llength \$array] } {incr i} \
{
set aux_1 [expr [lindex \$array \$aux_0]]
if { \$aux_1 > \$max_value } \
{
set max_value \$aux_1
}
incr aux_0
}

puts "\n \$max_value"```

Salvador Peregrina
Manufacturing Team Leader| NX 12.0.2

# Re: Tcl - biggest value

Solution Partner Phenom

this look simple to implement. thank you

---------------------------------------------
#♫ PB, 5ax, itnc, nx, vericut ♫ #

# Re: Tcl - biggest value

Phenom

```set a 1000
set b 10
set c 200

lappend tmp \$a \$b \$c
set max [lindex [lsort -real -decreasing \$tmp] 0]```