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

TCl numeric list 2

Status
Not open for further replies.

tclwannabe

Programmer
Jan 16, 2012
3
US
I need help with writing a procedure to compare two numeric lists in tcl. I am new to the language and therefore I just need some guidance.
 
Hi

tclwannabe said:
compare two numeric lists
In which way ?

For example this procedure compares two lists and returns -1, 1 or 0 when the first list is longer, the second list is longer or their lengths are equal respectively :
Code:
[b]proc[/b] lcmp [teal]{[/teal]a b[teal]}[/teal] [teal]\[/teal]
[teal]{[/teal]
  [b]set[/b] diff [teal][[/teal][b]expr[/b] [teal][[/teal][b]llength[/b] [navy]$a[/navy][teal]]-[[/teal][b]llength[/b] [navy]$b[/navy][teal]]][/teal]

  [b]if[/b] [teal]{[/teal][navy]$diff[/navy][teal]==[/teal][purple]0[/purple][teal]}[/teal] [teal]{[/teal]
    [b]return[/b] [purple]0[/purple]
  [teal]}[/teal] [b]elseif[/b] [teal]{[/teal][navy]$diff[/navy][teal]<[/teal][purple]0[/purple][teal]}[/teal] [teal]{[/teal]
    [b]return[/b] [purple]1[/purple]
  [teal]}[/teal] [b]else[/b] [teal]{[/teal]
    [b]return[/b] [teal]-[/teal][purple]1[/purple]
  [teal]}[/teal]
[teal]}[/teal]


Feherke.
 
consider this:
Code:
% set a {1 2 3}
1 2 3
% set b {4 5 6}
4 5 6
% foreach x $a y $b {puts $x,$y}
1,4
2,5
3,6
%

_________________
Bob Rashkin
 
thanks for your help both of you. The thing I am trying to do is to compare two numeric lists and then another set of two lists and if they both are equal then I do the next action. I understand the solution you have shared with me but the problem is with writing a procedure that will take four lists compare a particular set of two and then give me a result.
 
basically for equality elements of the lists have to be same.
 
Hi

Not sure what "particular set of two" should be, this procedure takes 4 parameters then compares 1st with 2nd and 3rd with 4th. It returns true if the compared lists contain the same elements in the same order.
Code:
[b]proc[/b] lcompare [teal]{[/teal]a b c d[teal]}[/teal] [teal]\[/teal]
[teal]{[/teal]
  [b]foreach[/b] x [navy]$a[/navy] y [navy]$b[/navy] [teal]{[/teal] [b]if[/b] [teal]{[/teal][navy]$x[/navy][teal]!=[/teal][navy]$y[/navy][teal]}[/teal] [teal]{[/teal] [b]return[/b] [purple]0[/purple] [teal]}[/teal] [teal]}[/teal]
  [b]foreach[/b] x [navy]$c[/navy] y [navy]$d[/navy] [teal]{[/teal] [b]if[/b] [teal]{[/teal][navy]$x[/navy][teal]!=[/teal][navy]$y[/navy][teal]}[/teal] [teal]{[/teal] [b]return[/b] [purple]0[/purple] [teal]}[/teal] [teal]}[/teal]
  [b]return[/b] [purple]1[/purple]
[teal]}[/teal]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top