Google App Script breaking singular element into 1D array with several elements - arrays

I've created a custom script in GAS (for Google Sheets) so I could join several data sources into one unique display. I added a .splice in the middle so I could cut out all null elements inside the arrays (which meant removing blank rows in the return array).
Here is it how the code goes:
function COMPILER(){
var i;
var c;
var r;
var display = [];
for (i = 0; i < arguments.length; i++) {
for (c = 0; c < arguments[i].length;c++) {
display.push(arguments[i][c]);
};};
for (r = display.length-1; r >= 0; r--) {
if (display[r][1]==""){
display.splice(r, 1)
};};
return display
};
The code works fine with 2+D arrays and with 1D arrays that have 2+ elements; but when its working with 1D arrays that have only one element, it unintendedly breaks down the strings into several elements.
Instead of returning:
ar1
ar2
It returns
a
r
1
a
r
2
How could I solve this?

I think/assume this is what you're trying to do:
function COMPILER()
{
var display = [];
for(var i = 0, numArguments = arguments.length; i < numArguments; ++i)
{
//console.log([i, arguments[i], Array.isArray(arguments[i])]);
if(Array.isArray(arguments[i]))
{
for(var j = 0, len = arguments[i].length; j < len; ++j)
{
if(arguments[i][j] != "")
{
display.push(arguments[i][j]);
}
}
}
else if(arguments[i] != "")
{
display.push(arguments[i]);
}
}
return display;
}
I can't confirm/test cause I don't know how you're calling COMPILER.

Related

Google Apps Script .setvalues For loop not working

The goal of the loop is to fill each cell over 797 rows across 5 columns A, B, C, D and E with a formula whose cell reference increments by 1.
E.g. Column A rows 6 onwards will have formula "=indirect("'Data Repository'!A3++")"
Column B rows 6 onwards will have formula "=indirect("'Data Repository'!B3++")"
What happens when I run the function however is it only fills in column A. I've checked the execution transcript and execution succeeded is logged after the first column has been filled up. I've tried various variations to no avail.
Below is the last variation I've tested:
function indirect(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Fleet - Weekly V3");
var formulaArray = [];
var columns = ["A","B","C","D","E"];
var row = 2;
var text = '=indirect(\"\'Data Repository\'!';
var headerRow = 6;
var column;
for(i = 0; i < 5; i++) {
column = parseInt(i) + 1;
formula = text + columns[i];
for(i = 0; i < 797; i++) {
row += 1;
if (formulaArray.length == 797) {
sheet.getRange(headerRow, column).offset(0, 0, formulaArray.length).setValues(formulaArray);
} else {
formulaArray.push([formula + row + '")']);
}
Logger.log(formulaArray.length);
}
Logger.log(i)
formulaArray = [];
}
}
Here is where you might be making an error - you need to create the variable i (var i = 0 instead of just i = 0) and if you're nesting loops, you need to have different variables increasing (first loop use i, then nest with j, then nest in that with k etc as needed)
for(var i = 0; i < 5; i++) {
column = parseInt(i) + 1;
formula = text + columns[i];
for(var j = 0; j < 797; j++) {
Untested but I believe it should work if you just substitute that in.
Your problem is in your loops. You are using the 'i' variable twice. Change the for loop that you have nested to iterate over the variable 'j' or something other than 'i'.

Changing data in array with substr in loop

I have a list of words in myArray, I need to make them all have 12 symbols, no more, no less. So first of all I weed out all words shorter than 12 symbols. IT WORKS. But then I must to cut extra symbols in words longer than 12 symbols. I use array12[j].substr(0,12); and its not working.
My question is: How to change data in array "on the fly" - in a loop? Trace I use after substr returns me same array long words, without cut.
function myArrayLoopFunction()
{
for (var i:int = 0; i < myArray.length; i++) // this works fine
{
if (myArray[i].length >= 12) {
array12.push(myArray[i]);
}
}
for (var j:int = 0; j < array12.length; j++)
{
if(array12[j].length > 12 )
{
array12[j].substr(0,12); //doesnt work
trace(array12[j]);
}
}
}
You can do it in one loop. I think your mistake is that you're not assigning the result of the substr method back to your array:
function myArrayLoopFunction()
{
for (var i:int = 0, str:String; i < myArray.length; i++)
{
str = myArray[i];
if (str.length >= 12) {
myArray[i] = str.substr(0, 12);
}
}
}
You're not assigning the substring to anything.
array12[j].substr(0,12);
should be
array12[j] = array12[j].substr(0,12);

How to compare a certain value of a character array in a specific index to another character array in a specific index

I'm having trouble comparing two character arrays. We are asked to make a program that will get the common and combine characters using 2 arrays. The combine function will combine all the inputs in the array and if there are common letters, the function will only display the letter once.
Example:
q w e r r
e r t y u
Output: q w e r t y u
while the common function will only display the common characters
Example:
q w e r r
e r t y u
Output: e r
I need looping statements and dynamic allocations specifically malloc. If there is no need to use boolean data type then pls don't use. Use if otherwise.
The prototypes of the two functions are:
char*comb(char*,char*) //For combine method
char*comm(char*,char*) //For common method
Let's say the first array is Array1 and the second one is Array2.
In order to create the Combine function, you should create a third array named Array3.
public void Combine()
{
bool exist = false;
for (int i=0; i<Array1.lengh; i++)
{
for (int j=0; j<Array3.lengh; j++)
{
if (Array[i] != Array[j])
exist = false;
else {
exist = true;
break;
}
}
if (!exist)
{
Array3.add(Array1[i]);
// output Array1[i] depends on the program you're using
}
}
}
See the logic how it should be and try to create Common function.
Here is the combine function, you can pass char arrays as string parameters to the function as following
combine("qwerr", "ertyu"); // returns "qwertyu"
function combine() {
var jString, jArr, cArr;
jString = "";
cArr = [];
for (var i = 0, ii = arguments.length; i < ii; i++) {
jString += arguments[i];
}
jArr = jString.split("");
cArr.push(jArr[0]);
for (var j = 0, jj = jArr.length; j < jj; j++) {
if (test(j)) {
cArr.push(jArr[j]);
}
}
function test(j) {
var x = true;
for (var k = 0, kk = cArr.length; k < kk; k++) {
if (cArr[k] == jArr[j]) {
x = false;
break;
}
}
return x;
}
return cArr.join("");
}
Function for common array can be created using same concept.

AS3: How to check if a value already exists in the Array before adding with FOR loop?

I believe is something simple but obviously not simple enough :). Any ideas how to check if a value already exists in the Array before adding the value using FOR loop?
I have this so far and it doesn't work as I want to because the Array can contain duplicate values!
var n:int = 5;
var cnt:int;
for (var i = 0; i < n; i++)
{
cnt = randomThief();
for (var a = 0; a < loto5.length; a++)
{
if (loto5[i] == cnt)
{
loto5[i] = cnt;
}
}
}
You can use the indexOf() method of the Array class to check if the value exists like this :
var index:int = loto5.indexOf(cnt);
indexOf() returns a -1, if the value doesn't exist. Here is an example of how to do a check :
if (loto5.indexOf(cnt) >= 0)
{
// do something
}
for (var a = 0; a < loto5.length; a++)
{
cnt = randomThief();
if (loto5.indexOf(cnt) == -1) //if cnt isn't in array do ...
{
trace (cnt+" is not in Array");
loto5[a] = cnt;
}
}
Works, simple and beauty :)

input an array output bigger array related to input

suppose I have a arrays from each i want to produce b these are just examples
a=[4]=> b=[0,4]
a=[3,1]=>b=[0,3,3,4]
a=[2,2]=>b=[0,2,2,4]
a=[2,1,1]=>b=[0,2,2,3,3,4]
a=[3,4,2,5]=>b=[0,3,3,7,7,9,9,14]
I mean when getting 4 it should produce from 0 and then add it to it's content for example 4
or in a[2,1,1] first it will produce 0 and then it see that the next one in a is 1 so after again producing it it will compute 2+1 and assign it.so the output always will be twice size of the input.
i want a pseudo code for it my problem is that when it will repeat I can not write it.
I used JavaScript like syntax.
var a = new Array(3,4,2,5);
var b = new Array();
var bArrayIndex = 0;
b[bArrayIndex] = 0;
bArrayIndex++;
for(i = 0; i < a.length; i++) {
b[bArrayIndex] = b[bArrayIndex-1] + a[i];
if(i < a.length - 1) {
b[bArrayIndex+1] = b[bArrayIndex];
}
bArrayIndex+=2;
}
for(i = 0; i < b.length; i++) {
document.write(b[i] + " ");
}

Resources