Page 1 of 1

Counting String Cases Based on Multipe Target Strings

Posted: Thu Feb 25, 2016 3:27 am
by diggetybo
Hey I rewrote this post, because I don't think my initial example was very good or concise. Let me get down to it,

I'm looking to add a criterion to the target string argument in the @instr command, so that the resulting integer value is calculated not just by how many times a singular target string is counted, but rather multiple targets in terms of an "and" logical context. I could use the following if I wanted a logical "or":

Code: Select all

%targets "a/b c/d" for %i {%targets} !string_count = !string_count+@instr(%list,%i) next
This way, !string_count would go up by 1 every time either of the members of %targets is found. However, for my purposes I need something with more exclusivity, more of a logical "and". Like if %list has "a/b" AND "c/d" then !string_count goes up by 1.


What should I do in this situation?

Re: Counting String Cases Based on Multipe Target Strings

Posted: Thu Feb 25, 2016 9:18 am
by diggetybo
Good news,

All this time explaining my question helped me hone in on a possible solution:

Code: Select all

!instr_1 = @instr(%list, "a/b") !instr_2 = @instr(%list, "c/d") if !instr_1 >= 1 and !instr_2 >=1 then !string_count = !string_count+1 endif
I'll leave this code here in case anyone else was looking for a resolution.

Re: Counting String Cases Based on Multipe Target Strings

Posted: Thu Feb 25, 2016 9:21 am
by EViews Gareth
Glad we could help!

Re: Counting String Cases Based on Multipe Target Strings

Posted: Thu Feb 25, 2016 10:42 pm
by diggetybo
Good news and bad news,

Bad news is I misunderstood the way @instr worked, that's totally my fault. It actually returns the character position within the string, not a count. Good news is, I did find a workaround that uses @instr if you wanted to count things in a string:

Code: Select all

vector counter_vector svector list_vector for %i {%targets} for !i = 1 to @rows(list_vector) if @instr(list_vector(!i),%i) > 1 then counter_vector(!i) = counter_vector(!i)+@instr(list_vector(!i),%i)-(@instr(list_vector(!i),%i)-1) else counter_vector(!i) = counter_vector(!i)+@instr(list_vector(!i),%i) endif next
So as you can see, it subtracts off the position to leave you with +1 for each time %target is found. All in all this code actually did end up working for me, although it doesn't meet the logical AND requirements. I wonder though, is there an easier way?