egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

array comparing, same results in new array

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
ChooChoo
Voice


Joined: 22 Oct 2009
Posts: 6

PostPosted: Sat Oct 24, 2009 1:35 pm    Post subject: array comparing, same results in new array Reply with quote

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:
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)?
Back to top
View user's profile Send private message
arfer
Master


Joined: 26 Nov 2004
Posts: 436
Location: Manchester, UK

PostPosted: Sat Oct 24, 2009 3:35 pm    Post subject: Reply with quote

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

Code:

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:

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
Back to top
View user's profile Send private message
arfer
Master


Joined: 26 Nov 2004
Posts: 436
Location: Manchester, UK

PostPosted: Sat Oct 24, 2009 3:51 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
ChooChoo
Voice


Joined: 22 Oct 2009
Posts: 6

PostPosted: Sat Oct 24, 2009 4:21 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
arfer
Master


Joined: 26 Nov 2004
Posts: 436
Location: Manchester, UK

PostPosted: Sun Oct 25, 2009 7:23 am    Post subject: Reply with quote

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:

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
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber