I'm trying to figure out how I can access an item in an array by it's index number. I've written a script which will generate an array, with some number of variables. There's an old script on the imageJ mailing list archive (shown below) which is able to print given index value for a known value within the array, but is there a way to find the value within the array itself? I.e. if I have the user input the number of values that should be in the array, can I have the macro call up the values in the array from that?
My array generator:
Dialog.create("Time Point Input");
Dialog.addNumber("How many time points?", 0)
Dialog.addString("What are your time points (comma separated, no spaces)?:",0);
Dialog.show();
time = Dialog.getNumber();
points = Dialog.getString();
Fpoints = newArray(points);
Where the readouts could be something like:
time = 4
points = 5,10,12,27
Fpoints[0] = 5
Fpoints [1] = 10
Fpoints [2] = 12
Fpoints [3] = 27
Calling up index from array number value example code:
arr = newArray(1,5,3,12);
i = index(arr, 5);
print("index = "+i);
function index(a, value) {
for (i=0; i<a.length; i++)
if (a[i]==value) return i;
return -1;
}
Thank you!
I am not 100% sure if I get your question right.
but is there a way to find the value within the array itself?
The problem is that you cannot create an Array with points since it's a String.
Try something linke:
Fpoints = split(points, ',');
Then you can iterate over Fpoints with a loop, or use you index function to get an index of a given value.
for (i = 0; i < Fpoints.length; i++) {
print(Fpoints[i]);
}
Related
var amazon = activeSpreadsheet.getSheetByName('Amazon');
var lastRow1 = amazon.getLastRow();
var array1 = amazon.getRange('A2:A' + lastRow1).getValues();
var source = activeSpreadsheet.getSheetByName('ProductDetails');
var lastRow2 = source.getLastRow();
var array2 = source.getRange('B2:B' + lastRow2).getValues();
n = 2;
x = 0; // match
z = 0; // non-match
for (var i = 0; i < array2.length; i++){
for (var j = 0; j < array1.length; j++){
if (array2[i] !== array1[j]){
z = z + 1;
}
else {
x = 9999
}
}
newsheet.getRange([n],[5]).setValue(z);
newsheet.getRange([n],[6]).setValue(x);
if (z > x) {
newsheet.getRange([n],[1]).setValue(array2[i]);
n == n++;
z = 0;
x = 0;
}
else if (z < x) {
z = 0;
x = 0;
}
}
My project is written in GAS (google app scripts) which is essentially, for all intents and purposes JS with variation in libraries.
Basically I am grabbing an element in the array2 and passing it through a loop to match to array1. For every time it does not match it adds 1, and when it matches (should only match once if it has any matches) it stores an arbitrary large number (larger than length of array1) and compares them.
As you can see I've written out to display these values and I always get z = 5183 (length of array1) and x = 0 (meaning no non-matches found). Therefore even if something exists in array 2 and 1 it will always write it to the cell.
What should happen is if there is a match, z= 5182 and x= 9999 (or arbitrary large number) and since 5182 < 9999 it doesn't do anything.
Is my scope wrong? Or am I not writing the If/Else correctly? Or is it something else?
Your code performs a strict comparison between the elements of two Arrays. That's fine, in general. However, for those specific Arrays those elements are also Arrays, which means strict (in)equality is checking to see if those are the exact same array object in memory. See this question for more information.
You probably wanted to do a value-based comparison, which means you need to compare the specific element of that inner array (i.e., index again). if (array2[i][0] !== array1[j][0]) {...} will check the 1st element of the inner array.
Looking at the instantiation of array1 and array2, we see that these are indeed 2D arrays from a single-column Ranges, so there will be only 1 element in each inner array. You can reduce the level of indexing necessary by flattening these arrays when you read them:
const array1 = sheet.getRange(...).getValues().map(function (row) { return row[0]; });
const array2 = ...;
I'm also not sure why you are passing in arrays to Sheet#getRange - you should pass in 1-4 arguments in manners consistent with the method signatures detailed in the Apps Script documentation.
Note that there are much better algorithms for checking if a value exists in a given array - you re-scan all of the 2nd array for every value of the first array. You should conduct thorough research into this topic before asking a new question on how to improve your algorithm.
Lastly, you should implement the best practice of using batch methods - you currently call setValue in a loop. Consider storing the results to write in an array, and then writing with Range#setValues once your loop has completed. There are many questions available for you to review on this topic.
I have an array of ints (I.E: 12 , 55 , 33) the length and contents of that array are variable.
In a for each loop, I need to take every single array index and create a set number of ui images based on its number, for example if the first array element = 5, I need to create 5 images, then move on to the next element and do the same, and so on...
Can anybody help me with this please?
foreach (int numImages in array)
{
for (int i = 1; i <= numImages; i++)
{
CreateImage();
}
}
Simple. I Need Help Setting All The Values Of An array to 0 in GameMaker: Studio.
This is because i need to test if i have not modified the array by using
if array[id] != 0 {
//Enter code
}
Of course, there are several ways, choose the best depending on the circumstances.
If you haven't filled anything in the array yet, adding a new item to a certain index initializes all previous values with "0":
var array;
array[length-1] = 0; //everything upto length-1 is filled
If you did already create the array and wish to reset it you should loop over it:
for (var i = array_get_length_1d(array) - 1; i >= 0; --i) {
array[i] = 0;
}
If you do not care about the original memory locations, and you can create a complete new array, creating a new one in place of the old one can be slightly faster:
array = 0; //destroys the old array
array[length - 1] = 0; //recreates like in the first option
i'm a beginner in c, i have attempted to insert a value into an array as i scan another array for a value higher than a threshold, if a value is over the set threshold in the array being searched then insert a number into the other array...
for (i = 0; i<lines[i][1]; i++) {
if (lines[i][1] > 6500) {
array[];
}
so what i mean is, if there is a value in lines[i][1] higher than 6500, then insert number "1" into array[].
However, with previous attempts it just overwrites the array rather than stacks on top of previous values.. i have another for loop attempting to do the same thing while searching another array.
for (i = 0; i<lines[i][0]; i++) {
if (lines[i][0] > 6500) {
array[];
}
The ideal output would be something like: 1 for values higher than in lines[i][0] and 2 for values higher than in lines[i][1], "array[] = {1,1,1,2,2,2,2,1,1,1,};"
and the values are inserted into the array as arrays are being scanned.
Please help... thank you
Just start a counter to keep track of the position where you last inserted the item on the destination array. For example:
int destPosition=0;
for (i = 0; i<lines[i][0]; i++) {
if (lines[i][0] > 6500) {
array[destPosition]=1;
destPosition++;
}
}
I have a trouble. I got my dear var myVar:int and an arr:Array. I want to use them to execute my function.
s1 and indice1 are array and integer values as I was defined in my program.
getIndex(s1, indice1);
function getIndex(arr:Array, index:int = 0):void {
for (var n:int = 0; n <= arr.length; n++) {
if (String(arr[n]).indexOf(">") >= 0) {
index = n;
trace(n);
arr[n] = String(arr[n]).substring(1);
}
}
}
now i get rigth results for my Array but i have a 0 for my index var.
Can someone help me?
I mean i need a dynamic way to assing index values of differents arrays to diferents indices so I can use my function to retrieve and index and save it for any Array I have
Your question is pretty unclear.
However, to answer (what I THINK you are asking),
You can not access variable index outside of the function. This is because the type int is not saved as a reference in AS3. To get index outside, you will have to do either of the following:
a) Assign the value of index to a global variable e.g.:
var gIndex:int;
function getIndex(arr:Array,index:int = 0):void{
//function contents
gIndex=index;
}
//This way you can access index as gIndex;
b) Return the variable index from the function
function getIndex(arr:Array,index:int = 0):int{
//function contents
return index;
}
//this way you can access index as getIndex(s1, indice1);
Your search string is available in arr[0] So that it returns 0.
Ok, I solve it.
Well it isn't what I want to but look.
I decided to return n value from my function and assing it to my var.
indice1 = getIndex(s1);
function getIndex(arr:Array){
for(var n:int=0; n<= arr.length; n++){
if( String(arr[n]).indexOf(">") >= 0){
return n;
arr[n]= String(arr[n]).substring(1);
}
}
}
So I got n value on my var and that's it. thanks Anyway =D