Have an idea?

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

Trying to concatenate in perl for dynamic text display

Earlier, I infer city/state from zip code. I ask a question to confirm do you live in [city, state].
If they say no I ask them to type in their city name (LOC1_r1_c1) and then select their state from a dropdown (loc_r1_c2).

The goal is to display the following:
If we got the correct information (CorrectCity=2) then I display the city/state deduced. This works.
If we got the incorrect information then I want to concatenate the typed in city and the state from the dropdown with a ', ' in between so it reads 'City, State'.

I am unable to get the concatenate portion to work.  Everything else works, but the concat just shows either nothing or '1' depending on how i try to configure things.

[% Begin Unverified Perl
#setting string for concatenation
my $tempcity = GETVALUE("LOC1_r1_c1");
my $tempstate = LISTLABEL("STATEList", GETVALUE("LOC1_r1_c2"));
my $newloc    = $tempcity    .    ', '    .    $tempstate;

#Condition for displayed text
if    (GETVALUE("CITYCORRELATION")    ==    2)
    {
        print $newloc;
    }
    elsif    (GETVALUE("CorrectCity")    ==    2)
    {
        return LISTLABEL("CITYSTATEList", GETVALUE("CITYSTATE"));
    }

End Unverified %]
asked Aug 19, 2020 by Nwiggin Bronze (1,310 points)

1 Answer

0 votes
Your concatenation is fine, but you need to use "return" instead of "print."
answered Aug 19, 2020 by Zachary Platinum Sawtooth Software, Inc. (171,950 points)
I have tried that, and that is when I get the '1' to show up instead of the text I'm after.
Try adding this to line 16:

return "Hello world";


When you run the script, does that text get printed out?
That prints out "Hello world".

"At the start of 2020, how would you have rated the overall quality of life in Hello world?"

As I mentioned.  If I'm in the state where CORRECTCITY = 2 then it prints out the appropriate text......................................................

OMG I'm a complete ID10T!! I just realized that it is impossible for CITYCORRELATION = 2. There is a series of skip patterns that make it impossible (well, extremely unlikely) for that to happen.  So I updated the if statement and it works.

[% Begin Unverified Perl
#setting string for concatenation
my $tempcity = GETVALUE("LOC1_r1_c1");
my $tempstate = LISTLABEL("STATEList", GETVALUE("LOC1_r1_c2"));
my $newloc    = $tempcity    .    ', '    .    $tempstate;

#Condition for displayed text
if    (GETVALUE("CorrectCity")    ==    1)
    {
        return $newloc;
    }
    elsif    (GETVALUE("CorrectCity")    ==    2)
    {
        return LISTLABEL("CITYSTATEList", GETVALUE("CITYSTATE"));
    }
End Unverified %]


The update is changing
if    (GETVALUE("CITYCORRELATION")    ==    2)

to
if    (GETVALUE("CorrectCity")    ==    1)


I go into such detail, not because I expect this specific problem for someone, but more as to provide a reminder to people searching the forums to make sure the basics are done correctly!
...