Have an idea?

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

Creating a working constructed list using unverified perl

I am having troubles with a constructed lists. I have three "variables"
UserID, Car and Color. Each user can own 1 up to 3 cars as in the table below.

UserID    Car1    Color1    Car2    Color2    Car3    Color3
Susan    Ferrari    Red     Opel     Blue          
Mark    Nissan    Black         
John    Subaru    Red    Lamborghini    Orange    Maserati    Grey

I have created three lists:

The first one (CarList) has [%Car1%],   [%Car2%] and [%Car3%] as entries.

The second one (ColorList) has [%Color1%],   [%Color2%]  and  [%Color3%] as entries.  

The third one "CarColorList" has
"[%Car1%] ; <i>[%Color1%]</i>" ;
"[%Car2%] ; <i>[%Color2%]</i>";
"[%Car3%] ; <i>[%Color3%]</i>"   
as entries.

I would like to create a construct list whose parent list is "CarColorList" which shows 1 car if the user has one car, 2 cars if he has 2 cars and 3 cars if 3 cars are owned.

As an example, if the user is Mark, only  "[%Car1%];<i>[%Color1%]</i>" should appear, while if the owner is John "[%Car1%];<i>[%Color1%]</i>  [%Car2%];<i>[%Color2%]</i> and [%Car3%];<i>[%Color3%]</i>  should appear.

I have wrote the following unverified pearl code

 Begin Unverified Perl

my %Values = ();
my $ i = 0;

for($i = 1; $i <= LISTLENGTH("CarColorList"); $i++)
if(!exists $Values{(LISTLABEL(" CarColorList  ", $i))} && (LISTLABEL("CarColorList", $i) ne ";"))
$Values{uc(LISTLABEL("CarList1", $i))} eq LISTLABEL("CarColorList", $i);
      ADD(" CarColorList", $i);

End Unverified 

However, the code has some issues since when the user owns 2 cars it shows 3 entries with the last one being just a ";". If, instead, the user owns 1 car only the color appears.

Thanks a lot in advance!
asked Mar 25 by Mattia
I think where your code is likely failing is that you are checking your list labels against ";", but the list labels would actually look like ";<i></i>" in their empty state.  Give that a shot and see if it resolves your problem.

If you want to go a small step further, could you check the list label of CarList rather than CarColorList?  You wouldn't have to bother with the semicolon or HTML at all that way.
As another note, I'm not sure that the %Values code is doing anything.  If you remove all references to that variable, does your code still work the same?

Your solution to the original question

Please only use this to answer the original question. Otherwise please use comments.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:

To avoid this verification in future, please log in or register.