Update List with Loop Arguments Scala - arrays

I'm trying create new random List based on other List. But this code doesn't work to update the var
import scala.util.Random
import scala.math
val randSymbol = List(1,2,2,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,8)
val _finalSymbol = List(List(0,0,0,0,0),List(0,0,0,0,0),List(0,0,0,0,0))
var i:Int = 0
var a:Int = 0
for (i <- 0 to 2){
_finalSymbol(i) = new List
for (a <- 0 to 4){
var iRandIndex = floor(Random.nextInt() * randSymbol.length).toInt
var iRandSymbol = randSymbol(iRandIndex)
_finalSymbol(i)(a) = iRandSymbol
}
}

Try something like this:
val _finalSymbol = (0 to 2) map { _ => Random.shuffle(randSymbol).take(5) }
And do yourself a favor: buy a scala book.
It's not javascript. At all.

Related

Pushing Distinct Values to Array and Finding New Length

Trying to sort and push only the distinct values into a new array and then find the length of the resulting array. See code below. The UniqueTeams.length is coming out to be 1, when it should be 5 (I thought).
var teams = [[formSS.getRange("F8").getValue(),
formSS.getRange("F10").getValue(),
formSS.getRange("F12").getValue(),
formSS.getRange("F14").getValue(),
formSS.getRange("F16").getValue()]];
var uniqueTeams = removeDups(teams);
if(uniqueTeams.length < 5){
{SpreadsheetApp.getUi().alert(uniqueTeams);
return;}
}
function removeDups(array) {
var outArray = [];
array.sort();
outArray.push(array[0]);
for(var n in array){
if(outArray[outArray.length-1]!=array[n]){
outArray.push(array[n]);
}
}
return outArray;
}
uniqueTeams = Alabama (2),Maryland (10),Cleveland St (15),BYU (6),Liberty (13)
uniqueTeams.length = 1
Get unique elements
function remdup() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const t = sh.getRange('F8:F16').getValues().flat();
let teams = [t[0], t[2], t[4], t[6], t[8]];
let s = new Set(teams)
teams = [...s];
Logger.log(teams.join(','))
}
Set
Cooper's answer it the best ultimate solution. But if you want just to fix your code you can try to change the line:
var uniqueTeams = removeDups(teams);
with:
var uniqueTeams = removeDups(teams[0]);
Since the teams is a 2D array [[...]].

Array Map multidimensional, get values from 3 array Java

Let say I have 3 arrays,
compCD = ["909", "908", "080", "901"];
contNO = ["09999", "08888", "00777", "00666"];
pomNum = ["A01", "A02", "A03", "A04"];
How can I insert into jsonMap each separated Values like
[
{
"compCD" = "909",
"contNO" = "09999",
"pomNum" = "A01"
},{
"compCD" = "908",
"contNO" = "08888",
"pomNum" = "A02"
}
]
we can assume that each array size is the same. The first of each array will bi insert first to map.
How we can solve those with minimum loop.
var compCD = ["909", "908", "080", "901"];
var contNO = ["09999", "08888", "00777", "00666"];
var pomNum = ["A01", "A02", "A03", "A04"];
var jsonMap=[];
for(var i=0; i<compCD.length; i++) {
jsonMap.push(
{compCD:compCD[i], contNO:contNO[i], pomNum:pomNum[i]}
);
};
console.log(jsonMap);
var compCD = ["909", "908", "080", "901"];
var contNO = ["09999", "08888", "00777", "00666"];
var pomNum = ["A01", "A02", "A03", "A04"];
var jsonMap=compCD.map(
(currentValue, index)=>[currentValue, contNO[index], pomNum[index]]
);
console.log(jsonMap);

Scala : append Array to List

I'm trying to append Array to List When writing data into a csv file as follows.
But, each Array was not appended to the List.
How can I append each Array to a List.
Please help me with this.
import java.io.StringWriter
import au.com.bytecode.opencsv.CSVWriter
import scala.collection.JavaConversions._
import java.io.FileWriter
import java.io.BufferedWriter
var out = new BufferedWriter(new FileWriter("/Users/TestCom/Desktop/test_4.csv"));
var writer = new CSVWriter(out);
var outputSchema = Array("name","0h","20h")
var listOfRecords = List(outputSchema)
var numIters = 0
for(numIters <- 0 to 1){
var testVal1 = numIters + 1
var testVal2 = numIters + 2
var eachArr = Array(testVal1, testVal2)
listOfRecords ++ eachArr
}
writer.writeAll(listOfRecords)
out.close()
Change your line that does listOfRecords ++ eachArr to be listOfRecords = listOfRecords ++ eachArr.
The list is immutable so you must reassign after appending.

How to sort array of custom objects by customised status value in swift 3?

lets say we have a custom class named orderFile and this class contains three properties.
class orderFile {
var name = String()
var id = Int()
var status = String()
}
a lot of them stored into an array
var aOrders : Array = []
var aOrder = orderFile()
aOrder.name = "Order 1"
aOrder.id = 101
aOrder.status = "closed"
aOrders.append(aOrder)
var aOrder = orderFile()
aOrder.name = "Order 2"
aOrder.id = 101
aOrder.status = "open"
aOrders.append(aOrder)
var aOrder = orderFile()
aOrder.name = "Order 2"
aOrder.id = 101
aOrder.status = "cancelled"
aOrders.append(aOrder)
var aOrder = orderFile()
aOrder.name = "Order 2"
aOrder.id = 101
aOrder.status = "confirmed"
aOrders.append(aOrder)
Question is: How will I sort them based on status according to open, confirm, close and cancelled?
You have to provide a value that will yield the appropriate ordering when compared in the sort function.
For example:
extension orderFile
{
var statusSortOrder: Int
{ return ["open","confirmed","closed","cancelled"].index(of: status) ?? 0 }
}
let sortedOrders = aOrders.sorted{$0.statusSortOrder < $1. statusSortOrder}
In your code you should make an array to store each aOrder with aOrders.append(aOrder) at each aOrder defination.
Then sort it with below code, refor this for more.
aOrders.sorted({ $0.status > $1.status })
The answer for swift 3 is as following
Ascending:
aOrders = aOrders.sorted(by:
{(first: orderFile, second: orderFile) -> Bool in
first.status > second.status
}
)
Descending:
aOrders = aOrders.sorted(by:
{(first: orderFile, second: orderFile) -> Bool in
first.status < second.status
}
)

Can i display multiple copies of a movieclip inside a array at once

Is there a way to make the code below work properly? When I use this code it only shows one movieclip:
var tempHead:head001 = new head001();
var mcArr:Array = new Array( tempHead );
var firstHead:MovieClip = mcArr[0];
firstHead.y = 30;
addChild(firstHead);
var secondHead:MovieClip = mcArr[0];
secondHead.y = 180;
addChild(secondHead);
`
You were just assigning a reference to the MovieClip. That'y, its not working.
First take instance of head001 class using new operator as much you want and store it to an array, then you can access very easily.
var tempHead: head001;
var mcArr: Array = new Array();
for (var i: uint = 0; i < 2; i++) {
tempHead = new head001();
addChild(tempHead);
mcArr.push(tempHead);
mcArr[i].y = mcArr[i].height * i;
}

Resources