Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Shuffling rows in a table/array question

  • rebekahhall54
  • rebekahhall54's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 4 months ago #209420 by rebekahhall54
Shuffling rows in a table/array question was created by rebekahhall54
Hello, I am trying to shuffle the rows of an array type question which I have recoded to perform as a conjoint analysis/ discrete choice question using a table.

I am trying to make it so the order of the rows in the table is randomised. I have found a few similar queries on the forum (e.g. forums.limesurvey.org/forum/can-i-do-thi...s-in-array-by-column ) but have no luck applying them to my own survey. The built-in randomise function does not work because of how I have had to code the question.

Is there a way to randomise the rows of a table? I have attached the example question (EXAMPLE 1). If this is not possible, I will rework the question and use a standard table with randomised rows instead (EXAMPLE 2), I am assuming this would use the shuffle array function but any on advice on how to do this would be really appreciated.

Thanks,
Rebekah
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 4 months ago #209423 by holch
Replied by holch on topic Shuffling rows in a table/array question
OK, so if the built in way doesn't work because of the way you have had to code it, then it would be important to understand what you did. Because how should we know how you had to code the question without the code?

You are talking about examples, but there are none attached.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.
  • rebekahhall54
  • rebekahhall54's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 4 months ago #209424 by rebekahhall54
Replied by rebekahhall54 on topic Shuffling rows in a table/array question
Very sorry, I uploaded but must not have attached them correctly.

File Attachment:

File Name: EXAMPLE1.lsq
File Size:15 KB

File Attachment:

File Name: EXAMPLE2.lsq
File Size:2 KB
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 4 months ago #209425 by holch
Replied by holch on topic Shuffling rows in a table/array question
We always as for LSS files for a reason:

1. LSQ and LSG files are language sensitive. This means we need to guess the base language correctly.
2. We need to create a survey, we need to create groups, etc. Make it easy for those that help in the forum: Provide a LSS file.
The LSS file has also the advantage, that we can see some general settings of your survey (e.g. if it is all in one, group by group, question by question, etc.). And no, we do not want to see your whole survey, just the relevant parts for your issue. I know it might be a little bit more of work for you, but it makes it a lot easier for the helpers: copy your survey, delete the irrelevant parts, export it as LSS and send it here.

Of course, you can always just create a little sample survey as well.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.
  • rebekahhall54
  • rebekahhall54's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 4 months ago #209427 by rebekahhall54
Replied by rebekahhall54 on topic Shuffling rows in a table/array question
Thanks for making me aware of this, please find the .lss file attached

File Attachment:

File Name: Examplequestion.lss
File Size:29 KB
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 4 months ago #209434 by Joffm
Replied by Joffm on topic Shuffling rows in a table/array question
Hi,
you don't want to shuffle neither rows nor columns in an array.
Your array consists of only one row.

What you want to do is to change the question text.
You see this text is hard coded as HTML table.

Depending on your model there could be a way to insert variable parts into the questin text by Expression Manager.
But therefore we need a bit more information, a better example of your model.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • rebekahhall54
  • rebekahhall54's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 4 months ago #209437 by rebekahhall54
Replied by rebekahhall54 on topic Shuffling rows in a table/array question
Hi Joffm,

Thank you, I see what you are saying. I am thinking it will be easier to rearrange the question to a table with a radio for the answers. This means I would like to change question 'example2' in the LSS file to do the following things:

Randomise the order rows excluding the top header
Randomise the order of columns 2 and 3 with column 1 fixed.

I have attached a picture to try and demonstrate what I mean. If this is overly complicated randomising the rows only would be good enough.

Really appreciate any advice you could offer!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 4 months ago #209459 by tpartner
Replied by tpartner on topic Shuffling rows in a table/array question
The second question is a list-radio without answer options. Can you provide a sample survey with all answer options and indicate in your mock-up where the answer options are to be placed?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • rebekahhall54
  • rebekahhall54's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 4 months ago #209461 by rebekahhall54
Replied by rebekahhall54 on topic Shuffling rows in a table/array question
Hi there,

This version has the full radio list of answers. These would remain under the table as a list as they appear by default in the example lss file attached.

Thanks,
Rebekah

File Attachment:

File Name: Example v2.lss
File Size:16 KB
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 4 months ago - 3 years 4 months ago #209462 by Joffm
Replied by Joffm on topic Shuffling rows in a table/array question
In the meantime I got a solution according to your first example in "examplequestions.lss"
Well, here it is.
First you create a random string of the numbers of your cards in a question of type "short text" (let's call it H0)
insert this javascript snippet.
Code:
<script type="text/javascript" charset="utf-8">
 
function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;
 
  // While there remain elements to shuffle...
  while (0 !== currentIndex) {
 
    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;
 
    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }
 
  return array;
}
 
 
$(document).on('ready pjax:scriptcomplete',function(){
// Fill the array
     var arr = [];
// create an array with the number of your cards (here 20)
     for (i = 1; i < 21; i++) {
       w=("000" + i).slice(-3);
       arr.push(w);
     }
     arr = shuffle(arr);
// Limit the array to the first (here 20) elements
     anumbers = arr.slice(0,20).join('');
     $('#question{QID} input[type="text"]').val(anumbers);
     $('#question{QID}').hide();
     $('#ls-button-submit').trigger('click');
   });
</script>

You get something like this.

You see this string contains all numbers from 1 to 20, left padded like "001", "002, ...
In the later display we take the first and the second, the third and the fourth, ... number to select the displayed cards.

Next.
In a question of type "long text" you enter your cards as default answer. (let's call it H1)
Like

...

Each attribute of same length (here 20 characters). and a total length per row of 80 characters.

Now it is easy to select the row according to the number and each of the four attributes
So for the first number in the string:
1st attribute: {trim(substr(substr(H1, (substr(H0, 0, 3) - 1) * 82, 80),0,20))}
2nd attribute: {trim(substr(substr(H1, (substr(H0, 0, 3) - 1) * 82, 80),20,20))}
3rd attribute: {trim(substr(substr(H1, (substr(H0, 0, 3) - 1) * 82, 80),40,20))}
4th attribute: {trim(substr(substr(H1, (substr(H0, 0, 3) - 1) * 82, 80),60,20))}

So for the second number in the string:
1st attribute: {trim(substr(substr(H1, (substr(H0, 3, 3) - 1) * 82, 80),0,20))}
2nd attribute: {trim(substr(substr(H1, (substr(H0, 3, 3) - 1) * 82, 80),20,20))}
3rd attribute: {trim(substr(substr(H1, (substr(H0, 3, 3) - 1) * 82, 80),40,20))}
4th attribute: {trim(substr(substr(H1, (substr(H0, 3, 3) - 1) * 82, 80),60,20))}

You see the logic?
And the "82"? This is because you have to consider the added carriage return and linefeed character.

You will get questions like:


And because you created 20 numbers you will be able to display 10 questions with different options.
To adapt it is up to you.

Have a look at the example.

File Attachment:

File Name: limesurvey...9169.lss
File Size:46 KB


Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 4 months ago by Joffm.
The topic has been locked.
  • rebekahhall54
  • rebekahhall54's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 4 months ago #209591 by rebekahhall54
Replied by rebekahhall54 on topic Shuffling rows in a table/array question
Thank you this works great for what I need and seems like a lot more effort than I naively anticipated when I posted the question so really appreciate your time spent helping me!
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose