#10488

Stefan
Participant

Merge two list together side-by-side RANDOM-ized

Hi folks, I was asked how to
random the two list together side-by-side.

Here is my answer:


//EmEditor Script (JavaScript)
// Merge two list together side-by-side RANDOM-ized
// shuffle intermix stochastic stochastically
// (see examples) Writes result to new document.

//=======================================================
//============ U S E R S E T T I N G S ==================

// Separate your two input list by at least 5 #'s
// Example input:
// JAMES
// JOHN <<< Left side in output
// WILLIAM
// RICHARD
// CHARLES
// ########## <<< Five or more #'s, but not less!
// MARY
// PATRICIA
// LINDA <<< Right side in output
// BARBARA
// ELIZABETH
Separator = "#####";



// Delimiter to be used for separating
// consecutive values on a single line.
// Join both input lists side-by-side,
// separated by that sign in the output
// Example output:
// WILLIAM + ELIZABETH
// CHARLES + PATRICIA
// RICHARD + BARBARA
// JAMES + MARY
// JOHN + LINDA
// or
// JOHN + BARBARA
// JAMES + PATRICIA
// RICHARD + ELIZABETH
// CHARLES + LINDA
// WILLIAM + MARY
// or
// WILLIAM + PATRICIA
// JAMES + BARBARA
// RICHARD + ELIZABETH
// CHARLES + LINDA
// JOHN + MARY
// or...
//
Delimeter = " + ";



//===========================================================
//===========================================================
//============ T H E S C R I P T ==========================

document.selection.SelectAll();
sel = document.selection.Text; //alert(sel);
Lines = sel.split("rn"); //Using Windows Line Enders here
SeparatorFound = false;
arrONE = new Array();
arrTWO = new Array();
arrOUT = new Array();

for(L = 0, Ls = Lines.length; L < Ls; L++){
if (Lines[L].indexOf(Separator) != -1){
SeparatorFound = true; continue; //SKIP
}
if (SeparatorFound==false){
arrONE[L] = Lines[L];
}else{
arrTWO[L] = Lines[L];
}
}

//Randomize the order of the array:
arrONE.sort( function() {return 0.5 - Math.random()} );
arrTWO.sort( function() {return 0.5 - Math.random()} );
//Join the arrays side-by-side:
for(idx=0,Len=arrONE.length; idx<Len; idx++){
arrOUT[idx] = arrONE[idx] + Delimeter + arrTWO[idx];
}
//============ O U T P U T =================================
OUT = arrOUT.join("rn");
//alert(OUT);

//Works with EmEditor:
editor.NewFile();
document.write(OUT);

//The End
//===========================================================

shuffle intermix stochastic stochastically random randomized