This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

array comparing, same results in new array

Help for those learning Tcl or writing their own scripts.
Post Reply
C
ChooChoo
Voice
Posts: 6
Joined: Thu Oct 22, 2009 12:03 pm

array comparing, same results in new array

Post by ChooChoo »

hi

i have 3 "2-dimensional" arrays.

array1(0,0-X) - 0-X max value is in var $limit1
array2(1,0-X) - 0-X max value is in var $limit2
array3(2,0-X) - 0-X max value is in var $limit3

i want same entries to be put in a new array, with the positions where it is in the original arrays

newarray(value,posarray1,posarray2,posaray3)

so entrys would be

Code: Select all

index   value pos1 pos2 pos3
0       blub  2    *   6
1       bla   3    8   9
....

if a value is not in all 3 arrays the value marked with * should be "25000"
if a value is only in 1 array it should not be in the new array.

so the string "blub" is in array1(0,2) and in array3(2,6)
string "bla" is in array1(0,3) and array2(1,8) and in array3(2,9)

is there any way to do this (as fast as possible)?
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

The following code will create an array called newarray which is an exact duplicate of an existing array called oldarray

Code: Select all

foreach {name value} [array get oldarray] {
  set newarray($name) $value
}
You could add additional logic within the foreach loop which would only duplicate the name/value pair in the newarray based on some aspect of $value (or even $name or both). For example :-

Code: Select all

foreach {name value} [array get oldarray] {
  if {[string equal -nocase bla $value]} {
    set newarray($name) $value
  }
}
I don't know if there is a simpler and/or faster method
I must have had nothing to do
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

I would like to add a further comment. There is no such thing as a two dimensional array in Tcl (not yet anyway).

Your code is an emulation of a two dimensional array which relies on the fact that Tcl arrays are associative. That is to say that the array element name can be any string and "1,8" for example is a string. It looks like a two dimensional array but it isn't, it only has one index/key (element name).
I must have had nothing to do
C
ChooChoo
Voice
Posts: 6
Joined: Thu Oct 22, 2009 12:03 pm

Post by ChooChoo »

i know thats its not a real 2 dim array thats why i put it in "".

the problem is that the values in the arrays are dynamic.

ill try your suggestion and report success or more likely my failing.
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

Yes I agree, it will probably fail.

Unless you are able to better compose a question in terms of script logic, I very much doubt if you will be getting a competent specific answer.

The only items you put in quotes seemed to be the array element values "bla" and "blub" and not the array element names, not that they need to be in quotes as things stand.

Variable values are often dynamic (change), that would be one good reason to call them variables. This includes array variables. An array element name does not change, it simply exist or not. I think what you mean is the array sizes are dynamic. This does not really have the same significance in Tcl that it has elsewhere.

Let me give you one example of the confusion that you communicate :-

newarray(value,posarray1,posarray2,posarray3)

The name of the array is newarray, the name of the array element is some sort of string concatenation of the value in the other arrays plus the element names in the said other arrays (positions as you call it). However, what is the value of this element of newarray?

Code: Select all

set newarray(value,posarray1,posarray2,posarray3) ???
This would be my suggestion :-

The newarray element name is the value from the other arrays and the newarray element value is a list of the element names where it occurs in the other arrays

value array1 array2 array3
bla 1,3 2,8 1,9
blub 0,2 * 0,6

So, for example, the value blub occurs in array1(0,2) and array3(0,6) but does not occur at all in array2

With the correct code, this could yield

newarray(bla) {1,3 2,8 1,9}
newarray(blub) {0,2 "" 0,6}

Even this assumes certain things :-

1. The value blub is not duplicated in the original arrays (occurs in more than one element of any one of the arrays)

2. The values in the original arrays do not contain spaces (this is possible to deal with, but potentially more difficult)

I'm sorry to be negative about your question but it does indicate a lack of understanding of Tcl arrays. Probably you are coming to Tcl from an alternative language where arrays are what might be called more 'normal' in structure/behaviour. Hopefully the Tcl experts won't shoot me for saying that.
I must have had nothing to do
Post Reply