Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

using not answered in Perl

I am using Perl for skipping based upon values but how would I write this to skip if a particular question in an entire loop was not answered?  For example, rather than A02==1 I want to skip if Not Answered (A02)...
Begin Unverified Perl
    my $clist = 'household2';
    my $selectQ = 'A02';
    #Run
    my $len = LISTLENGTH($clist);
    for(my $i = 1; $i <= $len; $i++){
        my $item = LISTVALUE($clist, $i);
        if (GETVALUE("$selectQ".'.'.$item) == 1 {
            return 0;
            }
        }
    return 1;
End Unverified
asked Jan 21 by sallen Bronze (2,315 points)
How about this?

if (!ANSWERED($selectQ)) {
    return 1;
}
Thanks!  I will try that.
I am using this but it still seems to show when A04B is not answered. Should I remove the things after $selectQ?
Begin Unverified Perl
    my $clist = 'household2';
    my $selectQ = 'A04B';
    #Run
    my $len = LISTLENGTH($clist);
    for(my $i = 1; $i <= $len; $i++){
        my $item = LISTVALUE($clist, $i);
        if (!ANSWERED("$selectQ".'.'.$item)){
            return 0;
            }
        }
    return 1;
End Unverified
I don't see why it would need to be placed in the for loop or appended with "$item."  Should be able to just add it after the "Run."
Sorry I dont know what you mean? If this question hasn't been answered yet in the previous loops, I want this to show. so like this?
Begin Unverified Perl
    my $clist = 'household2';
    my $selectQ = 'A04B';
    #Run
    my $len = LISTLENGTH($clist);
    for(my $i = 1; $i <= $len; $i++){
        my $item = LISTVALUE($clist, $i);
        if (!ANSWERED($selectQ)){
            return 0;
            }
        }
    return 1;
End Unverified
"return 0" causes the skip to not happen.  Do you mean to swap the two return statements?
Oh, yes, I will swap those.
I am using this code since I want the skip to happen if A04B has NOT been answered. Is that correct? It still isn't working the way I expect.
Begin Unverified Perl
    my $clist = 'household2';
    my $selectQ = 'A04B';
    #Run
    my $len = LISTLENGTH($clist);
    for(my $i = 1; $i <= $len; $i++){
        my $item = LISTVALUE($clist, $i);
        if (ANSWERED($selectQ)){
            return 0;
            }
        }
    return 1;
End Unverified

1 Answer

0 votes
 
Best answer
Swapping the return statements is not equivalent to swapping the "!" on and off.  Think about how the for loop is going to run, keeping in mind that the code is going to end as soon it reaches any "return" statement.

Skip if the conditional is true for ANY of the looped items:

for (...) {
    if (...) {
        return 1;
    }
}
return 0;


Skip if the conditional is false for ANY of the looped items:

for (...) {
    if (!...) {
        return 1;
    }
}
return 0;


Do NOT skip if the conditional is true for ANY of the looped items (i.e., skip if the conditional is false for ALL of the looped items):

for (...) {
    if (...) {
        return 0;
    }
}
return 1;


Do NOT skip if the conditional is false for ANY of the looped items (i.e., skip if the conditional is true for ALL of the looped items):

for (...) {
    if (!...) {
        return 0;
    }
}
return 1;
answered Jan 24 by Zachary Platinum Sawtooth Software, Inc. (206,075 points)
selected Jan 25 by sallen
Thanks. The 2nd example is what I want and I believe that is what is being used here but the dep. question is getting skipped even though A04B hasn't yet been answered...
Begin Unverified Perl
    my $clist = 'household2';
    my $selectQ = 'A04B';
    #Run
    my $len = LISTLENGTH($clist);
    for(my $i = 1; $i <= $len; $i++){
        my $item = LISTVALUE($clist, $i);
        if (!ANSWERED($selectQ)){
            return 1;
            }
        }
    return 0;
End Unverified
Did you mean to remove the "$item" from the conditional?
I thought above you mentioned it didnt need that part.  Sorry if I am not getting this easily.
Sorry for the confusion.  You've got a very large study with a lot of moving parts; it becomes challenging to provide definitive help without seeing the big picture.  If the select question is in a loop and you want to check each iteration of the loop, then you will need to keep the ".X" part of the variable name.
OK. Thanks. I appreciate your help. Yes, I wanted to check each iteration. I put this back in and it seems to be working so fingers crossed it will hold up in testing.
...