|
nostra Wanderer
Joined: 23 May 2001 Posts: 68 Location: Sweden
|
Posted: Fri Jun 11, 2004 1:01 pm
Comparing two lists and returning value |
I have two lists that I want to compare and if there is a match I want to return the value in the "record variable list" (1, 2).
Record variable list
one | 1
two | 2
String list
one
two
three
This is what I got so far:
#var new_variable %db( @record_list, @string_list)
Problem is that this trigger doesn't work for "string lists", that is, if the @string_list contains more than one value it fubars.
Any ideas?
Cheers,
Mikael |
|
|
|
Mike Z Red Beginner
Joined: 11 Jun 2004 Posts: 11 Location: USA
|
Posted: Fri Jun 11, 2004 4:36 pm |
@test is dbvar, such as
1:one
2:two
3:three
4:four
@list is string list, such as
3,4,5
Here's your script:
#FORALL @results {#VAR results %delitem( %i, @results)}
#LOOPDB @test {#if (%ismember( %key, @list)) {#VAR results %additem( %val, @results)}}
The first line (#FORALL) clears the variable @results.
The second line (#LOOPDB) checks each key in @test to see if it's in the list, if it is, it adds the matching value to @results.
To show the results, add a third line:
#SHOW %expandlist( @results, ", ") |
|
|
|
LightBulb MASTER
Joined: 28 Nov 2000 Posts: 4817 Location: USA
|
Posted: Fri Jun 11, 2004 6:11 pm |
quote: Problem is that this trigger doesn't work for "string lists", that is, if the @string_list contains more than one value it fubars.
What trigger? I didn't see a pattern and the only command was a variable command, not a trigger command.
It's not clear, but it appears you want @new_variable to be a stringlist which contains the vals from @record_list which correspond to the keys listed in @string_list. You'll have to obtain these vals one at a time. You can either use #FORALL to go through @string_list and find the matching val for each item:
#FORALL @string_list {#ADDI new_variable {%db( @record_list, @string_list)}
or you can use #LOOPDB to go through @record_list and match each key against @string_list, adding the val to @new_variable whenever there is (very similar to Mike's):
#LOOPDB @record_list {#IF
%ismember( %key, @string_list) {#ADDI new_variable {%val}}}
If every item in @string_list will also be a key in @record_list (as appears to be your intention), then the #FORALL method would be more efficient.
As usual, both the question and the answers would have been clearer if you had given us real variable names and values and the script you are using it in.
Mike, there's no need to use #FORALL, or any other loop, to clear a variable. Just assign a null value to it. Much faster than deleting one item at a time.
#VAR results {} |
|
|
|
|
|
|
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
|
|