show function return value

Discussions about editing and cleaning data
Forum rules
New release: CSPro 8.0
Post Reply
AriSilva
Posts: 591
Joined: July 22nd, 2016, 3:55 pm

show function return value

Post by AriSilva »

Presently the show function returns the sequential index value of the cases presented in the list.
But, if the list has a "where" condition, this return code does not have any meaning. For example, if one shows the list of the persons in a household having more than 12 years old and asks "who is the head", returning an index number related to the cases in the list does not help, since one would have to search the hoster using some kind of algorithm to see who is the real head of the household.
Maybe the function should return the real index of the cases, or at least have an option to do that.
Or I really did not understand how I should use the show function.
Best
Ari
Best
Ari
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: show function return value

Post by josh »

Yes, in many cases the way that functions is not ideal. Unfortunately it has worked the way for many years and changing it would break a lot existing applications. You need to write a loop that uses the same condition to translate the return value of show into the real index. You could probably use seek to simplify it.

I never use show, partly for this reason, but mostly because show uses the values instead of the labels which usually isn't what I want. I use showarray and then I stick the actual index in the last column of the array but I don't add a title for that column so it doesn't get displayed. Here is an example:
    // Copy from household roster
    // into householdMembersArray
    numeric ai = 1;
    
do numeric ri = 1 while ri <= totocc(HOUSEHOLD_MEMBERS_ROSTER)

        
if (AGE(ri) > 12) then
            householdMembersArray(ai,
1) = strip(NAME(ri));
            householdMembersArray(ai,
2) = getlabel(SEX, SEX(ri));
            householdMembersArray(ai,
3) = maketext("%d", AGE(ri));
            householdMembersArray(ai,
4) = maketext("%d", ri);
            
inc(ai);
        
endif;
    
enddo;
    householdMembersArray(ai,
1) = "";
    
    
numeric picked =
        
showarray(householdMembersArray,
              
title("Name", "Sex", "Age"));
    
if picked > 0 then
        
numeric pickedRosterIndex = tonumber(householdMembersArray(picked, 4));
        
// do something with pickedRosterIndex
    endif;
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: show function return value

Post by josh »

Greg points out that you can use seek with @ to convert from the index in the list to the roster index:
numeric selected_index = show(PERSON_REC,NAME,RELATIONSHIP,SEX,AGE,where AGE > 12);

numeric roster_index = seek(PERSON_REC where AGE > 12, @selected_index);
The second argument to seek is normally the start index but when you add @ in front of it means find the nth occurrence that matches the condition.
AriSilva
Posts: 591
Joined: July 22nd, 2016, 3:55 pm

Re: show function return value

Post by AriSilva »

That was good indeed, I´ll try both.
Thanks
Ari
Best
Ari
Post Reply