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

Searching only the first element of list

Status
Not open for further replies.

infi1nity

Programmer
May 13, 2010
5
GB
Hi,
I am quite new to TCL and get confused a lot with lists and arrays. I have a problem wherein I need to search for a particular pattern in a list, extract the values from the list (The list has three elements) and build an array. The patterns to search are stored in an array.
E.g
List L= {A RED 1} {B BLACK 2} {C GREEN 3} {D PINK 4}
Array =A.index=10,C.index=4,A.RED=Anything,C.GREEN=Something

The aim is to build an array with values and index positions obtained from the search i.e.,If there is a match between the array names and the first and second element of the list (i.e., A.RED, B.BLACK, C.GREEN):
i. Index extracted from the third element of the list (1, 2, 3, 4)
ii. Value extracted from index values of the array (A.index, B.index).

To make it more clear in the above example there is a match for A.RED and C.GREEN therefore the final array would look like:
final(1)=10 (Index=1 because A.RED is 1 in the list)
final(3)=4 (Index=1 because C.GREEN is 3 in the list)
I have tried a variety of lsearch, lindexes but some teething problems still appear. Can someone help me with this ?
 
I don't see what you mean for the contents of your array.

But, let's start with your list:
List L= {A RED 1} {B BLACK 2} {C GREEN 3} {D PINK 4}
So, presumably you have done something like:
Code:
set L {{A RED 1} {B BLACK 2} {C GREEN 3} {D PINK 4}}
Now you want to search L for a match on both the first and second values of the sub-list? Does that mean that, for instance, it is possible to have {A Orange 42} as the contents of a sub-list of L?

_________________
Bob Rashkin
 
Hi Bob,
Let me explain it a bit more. Let's say we have the list:
set L {{A RED 1} {B BLACK 2} {C GREEN 3} {D PINK 4}}

Then I also have an array
set arrayA(A.RED.idx) 20
set arrayA(B.BLACK.idx) 50
set arrayB(C.BLUE.idx) 70

My aim is to build an array i.e., the output of the code is to have an array whose index and values are obtained by comparing the first and second element of the list with the index element of the arrayA i.e.,
List [A.RED] Matched with every element of Array index. Since there is a match for A.RED.idx (idx is constant for all searches) we proceed to built the final array the output. So in this case,
set finalArray(1) 20 ; where
1 is the index element obtained from the list..
20 is the value from arrayA whose index named matched to that of the List
 
OK. Let's look at what you have:
Code:
% set L {{A RED 1} {B BLACK 2} {C GREEN 3} {D PINK 4}}
{A RED 1} {B BLACK 2} {C GREEN 3} {D PINK 4}
% set L
{A RED 1} {B BLACK 2} {C GREEN 3} {D PINK 4}
% set arrayA(A.RED.idx) 20
20
% set arrayA(B.BLACK.idx) 50
50
% set arrayA(C.BLUE.idx) 70
70
%

When you say,
idx is constant for all searches
do you mean that it's irrelevant? That is, A.RED.5 match {A RED 1} and A.RED.22 does as well? Anyway, assuming "yes",

I would make arrayA into a list for easy searching:
Code:
% set lstArr [array get arrayA]
C.BLUE.idx 70 B.BLACK.idx 50 A.RED.idx 20
%
Now, in order to make lstArr2 look like L:
Code:
% foreach i $lstArr {lappend lstArr2 [split $i .]}
% set lstArr2
{C BLUE idx} 70 {B BLACK idx} 50 {A RED idx} 20
%

Now I would look for the right combo's in both lists:
Code:
foreach {a b} $lstArr2 {
    foreach x $L {
        if {[lrange $a 0 1]==[lrange $x 01]} {
            set finalArray([lindex $a 2]) $b
        }
    }
}


_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top