cancel
Showing results for 
Search instead for 
Did you mean: 

Count Occurences within in an Array

03-31-2010 11:29 AM
richiew03 Explorer
966 views 9 comments
0 Likes
SAP Managed Tags
Subscribe

Hi,

I have an array with numbers, I want to count how often the number 5 is stored in it, does anyone know the syntax?

thanks,

Rich

0 Likes

Accepted Solutions (0)

Answers (2)

Answers (2)

0 Likes

Please re-post if this is still an issue or purchase a case and have a dedicated support engineer work with you directly:

http://store.businessobjects.com/store/bobjamer/DisplayProductByTypePage&parentCategoryID=&categoryI...

richiew03
Explorer
0 Likes

OK I solved this myself using a loop, which loops round and increments the element number in the array up to the size of the array, and a counter which increments each time the condition is met:

while loopcount <10

do (loopcount:=loopcount +1;

If SelfArray1[loopcount] = 5 then counter:=counter +1);

Former Member
0 Likes

Here is simple solution that I've used...


LEN(ToText({@Array},"0")) - LEN(REPLACE(ToText({@Array},"0"),"5",""))

The ToText function is there because the LEN function only works on strings

HTH,

Jason

Former Member
0 Likes

As long as all of the values in the array are one digit, Jason's formula will work. It will not work correctly if there are multiple-digit values with a 5 somewhere in the string of digits (e.g., the value 55 will cause the formula to count 2 fives)...

You might be able to change it to something like


(LEN(ToText({@Array},"00000000.0")) - LEN(REPLACE(ToText({@Array},"00000000.0"),"00000005.0",""))) / 10

HTH,

Carl

richiew03
Explorer
0 Likes

Hi Sorry, how does the formula work? you are uing the {@] for the array but surely that is for a formula field? Do I have to do something to the array in a formula first?

Thanks,

richard

Former Member
0 Likes

Nope. Most arrays I see are arrays because they were created with a formula.

If the the array is stored as a single vale in the DB, then just drop the in, in place of the the {@Array}

Jason

richiew03
Explorer
0 Likes

Hi Jason,

Sorry, I think I am mising something really basic here,

I have an Array created in a subreport declared:

shared numbervar array RespondentArray1;

there is then various code which poulates it depending on which IF conditions are met as it cycles through the records.

In my main report I want to count the occurneces of say the number 5. But I was expecting to drop the array name only (RespondentArray1) into where you have {@Array}. But I get an error if I do that. The formula where the array is declared and populates is in a subreport.

Can you please advise of what I should be replacing in your code?

My looping would work, but I dont think it is very efficient especially as I have lots of counts to do on various numbers,

thanks,

Richard.

Former Member
0 Likes

Did you re-declare the shared variable in the main report's formula?


shared numbervar array RespondentArray1; 

HTH,

Carl

richiew03
Explorer
0 Likes

Thanks for your help with this Carl, My declation is as follows:

Shared numbervar array RespondentArray1;

LEN(ToText(RespondentArray1,"0")) - LEN(REPLACE(ToText(RespondentArray1,"0"),"5",""))

I get the mesasge "the array must be a subscipt for example array<i>"

Thanks,

Rich