I have an ini file with information like:
slExpsave_0 = 0.23;
slExpsave_1 = 0.40;
slExpsave_2 = -0.85;
...
I need to use them in a loop;
...
for(var i=0; i<3; i++)
{
slExpArray[i].value = slExpsave_[i];
}
But it doesn't work.
What is the reason for this?
I'm guessing this is Javascript, so you should use an array if you want to access them within a loop:
slExpsave_ = [0.23, 0.40, -0.85];
Does this help?
Let me explain my problem more clearly.
As I did it manually, It worked as I wanted:
slExpsave_0 = 0.23;
slExpsave_1 = 0.40;
slExpsave_2 = -0.85;
slExpArray[0].value = slExpsave_0; // output is 0.23;
slExpArray[1].value = slExpsave_1; // output is 0.40;
slExpArray[2].value = slExpsave_2; // output is -0.85;
when I tried to do it automatically by put in the for loop:
the first one I tried:
for(var i=0; i<3; i++)
{
slExpArray[i].value = "slExpsave_" + i;
}
// output is slExpsave_0, slExpsave_1, slExpsave_2
the second was:
for(var i=0; i<3; i++)
{
slExpArray[i].value = slExpsave_[i];
}
// doesn't work.
Related
I would like to create more then one image with a loop. Yet, it doesn't seem to work this way. The problem is that an image can't have a number as a name. Is there a way to do this?
for(int i = 0; i < getObjects().size(); i++) {
Image i = new Image(getObjects().get(i).getImagePath()));
}
Try making an ArrayList of type Image and you can store all your images there
ArrayList<Image> imageArrayList = new ArrayList<Image>();
for(int i = 0; i < getObjects().size(); i++) {
imageArrayList.add(new Image(getObjects().get(i).getImagePath());
}
If you are just starting with java don't want this to be as confusing when returning to the code maybe use something like this
ArrayList<Image> imageArrayList = new ArrayList<>();//Creates the list that will store images
for(int i = 0; i < getObjects().size(); i++) {
Image image = new Image(getObjects().get(i).getImagePath());//Create the image you want to store
imageArrayList.add(image);//Add that image to the arraylist
}
I'm currently writing a statistic spreadsheet script for my guild, which reads out the class of one person and counts it on the statistic sheet.
For some reason the for loops aren't working. When I execute the script, it does nothing. Everything before the for loop seems to work. I have used the debugger, and set a debug point from the point of the for loop and the window is opening and closing after like 1 second.
This is my code as of now:
function addToStatistik() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var source_sheet = spreadsheet.getSheetByName("Raid Montag");
var source_range_names = source_sheet.getRange("C4:C13");
var source_range_setup_boss1 = source_sheet.getRange("M4:M13");
var target_sheet = spreadsheet.getSheetByName("Statistik");
var target_range_names = target_sheet.getRange("A4:A31");
var target_range_boss1 = target_sheet.getRange("K4:S31");
target_sheet.getRange(2,1).setValue("Debug1"); //testing stuff
for (var i=0; i < source_range_names.length; i++) {
for (var j=0; j < target_range_names.length; j++) {
if (source_range_names[i][0] == target_range_names[j][0]) {
if (source_range_setup_boss1[i][0].indexOf("War") > -1) {
target_sheet.getRange(9,5).setValue("TEST");
}
}
}
}
}
Someone can find any errors in there? I can't find anything and google also isnt helping me.
You are getting the range, but not the values. This line:
var source_range_names = source_sheet.getRange("C4:C13");
gets a range, but not any values.
Should be:
var source_range_names = source_sheet.getRange("C4:C13").getValues();
The outer loop never loops. There is no length of a range.
for (var i=0; i < source_range_names.length; i++) {
You don't need to change the above line, but currently the variable source_range_names is a range, and not a 2D array of values.
Before you iterate you need to get the values of the range, to achieve this you need to use the method getValues() or getDisplayValues():
function leFunction() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var source_sheet = spreadsheet.getSheetByName("Raid Montag");
var source_range_names = source_sheet.getRange("A1:C13");
var values_range_names = source_range_names.getDisplayValues();
Logger.log(values_range_names);
for (var i=0; i < values_range_names.length; i++) {
// Do Something
}
}
.proto file structure
message repetedMSG
{
required string data = 1;
}
message mainMSG
{
required repetedMSG_id = 1;
repeated repetedMSG rptMSG = 2;
}
I have one mainMSG and in it too many (suppose 10) repetedMSG are present.
Now i want to delete any particular repetedMSG (suppose 5th repetedMSG )from mainMSG. For this i tried 3 ways but none of them worked.
for (int j = 0; j<mainMSG->repetedMSG_size(); j++){
repetedMSG reptMsg = mainMsg->mutable_repetedMSG(j);
if (QString::fromStdString(reptMsg->data).compare("deleteMe") == 0){
*First tried way:-* reptMsg->Clear();
*Second tried Way:-* delete reptMsg;
*Third tried way:-* reptMsg->clear_formula_name();
break;
}
}
I get run-time error when i serialize the mainMSG for writing to a file i.e. when execute this line
mainMSG.SerializeToOstream (std::fstream output("C:/A/test1", std::ios::out | std::ios::trunc | std::ios::binary)) here i get run-time error
You can use RepeatedPtrField::DeleteSubrange() for this. However, be careful about using this in a loop -- people commonly write code like this which is O(n^2):
// BAD CODE! O(n^2)!
for (int i = 0; i < message.foo_size(); i++) {
if (should_filter(message.foo(i))) {
message.mutable_foo()->DeleteSubrange(i, 1);
--i;
}
}
Instead, if you plan to remove multiple elements, do something like this:
// Move all filtered elements to the end of the list.
int keep = 0; // number to keep
for (int i = 0; i < message.foo_size(); i++) {
if (should_filter(message.foo(i))) {
// Skip.
} else {
if (keep < i) {
message.mutable_foo()->SwapElements(i, keep)
}
++keep;
}
}
// Remove the filtered elements.
message.mutable_foo()->DeleteSubrange(keep, message.foo_size() - keep);
I get data from a $.ajax call but cant work correctly with the data of it. Here is my code.
function OnSuccessResultSet(data, status) {
var output;
for (var i in data.recordset) {
output += "<li><a href='#'>";
for (var j = 0; j < metaName.length; j++) {
var testVar = metaName[j];
output += " <h2>" + data.recordset[i].testVar+ "</h2>";
alert(data.recordset[i].testVar);
alert(testVar);
alert(data.recordset[i].LABEL);
};
output += "</a></li>";
}
$(output).appendTo("#content1");
$('#content1').listview('refresh');
}
The first alert gives me an undefined back. Second alert gives me LABEL back and third one gives me the value of LABEL. My metaName has all attribute values for my element from recordset. I fill also my metaName array with a $.ajax call.
I dont find my mistake. :/
I think you need to use bracket notation instead of dot notation as the member operator here as the key you are looking for is stored in the variable testVar
alert(data.recordset[i][testVar]);
Ex
function OnSuccessResultSet(data, status) {
var output, testVar;
for (var i in data.recordset) {
output += "<li><a href='#'>";
for (var j = 0; j < metaName.length; j++) {
testVar = metaName[j];
output += " <h2>" + data.recordset[i][testVar]+ "</h2>";
};
output += "</a></li>";
}
$(output).appendTo("#content1");
$('#content1').listview('refresh');
}
I am brand new to coding, and I cannot for the life of me, through any amount of voodoo or googling, understand why this simple code will not work in Google Scripts:
var student = new Object; // name & student's hours
var studentNames = new Array;
for (var i=0; i <= allSheets[1].getLastRow(); i++); {
studentNames[i] = allSheets[1].getRange((i+3),1).getValue();
student[studentNames[i]] = allSheets[1].getRange(i+3, 3).getValue();
}
For some strange reason it just fills the array studentNames with "null", and the Object student will only show one key.
If I bypass the for loop like so,
studentNames[0] = allSheets[1].getRange(3,1).getValue();
studentNames[1] = allSheets[1].getRange(4,1).getValue();
studentNames[2] = allSheets[1].getRange(5,1).getValue();
studentNames[3] = allSheets[1].getRange(6,1).getValue();
everything works perfectly!
If someone could tell me what I am doing wrong I would be much obliged.
remove the semicolon at the end of your for loop
for (var i=0; i <= allSheets[1].getLastRow(); i++); <- this one
cheers : )
There is an extra semicolon on line 4:
for (var i=0; i <= allSheets[1].getLastRow(); i++); {
should be:
for (var i=0; i <= allSheets[1].getLastRow(); i++) {
The semicolon ends up closing the body of the for loop, so the block {...} ends up executing only once.