Counting String Cases Based on Multipe Target Strings

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Moderator, EViews Jason, EViews Matt

diggetybo
Posts: 152
Joined: Mon Jun 23, 2014 12:04 am

Counting String Cases Based on Multipe Target Strings

Postby diggetybo » Thu Feb 25, 2016 3:27 am

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?
Last edited by diggetybo on Thu Feb 25, 2016 9:22 am, edited 1 time in total.

diggetybo
Posts: 152
Joined: Mon Jun 23, 2014 12:04 am

Re: Counting String Cases Based on Multipe Target Strings

Postby diggetybo » Thu Feb 25, 2016 9:18 am

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.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13604
Joined: Tue Sep 16, 2008 5:38 pm

Re: Counting String Cases Based on Multipe Target Strings

Postby EViews Gareth » Thu Feb 25, 2016 9:21 am

Glad we could help!

diggetybo
Posts: 152
Joined: Mon Jun 23, 2014 12:04 am

Re: Counting String Cases Based on Multipe Target Strings

Postby diggetybo » Thu Feb 25, 2016 10:42 pm

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?


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 1 guest