Coldfusion ArraySet() v. user defined cffunction - coldfusion

I replaced this user-defined CF function that I found.
<cffunction name="initializeValues" returntype="array" output="false">
<!--- Initialize all elements of an array to zero --->
<cfargument name="inArray" required="yes" type="array">
<cfargument name="inCount" required="yes" type="numeric">
<cfloop index="i" from="1" to="#inCount#">
<cfset inArray[i] = 0>
</cfloop>
<cfreturn inArray>
</cffunction>
with the built-in CF9 function
ArraySet(arrayName, startingIndex, endingIndex, 0)
however, the final results differed somehow and threw an exception on the user page.
How do these functions differ?

You need to provide more specifics. What do you mean by "differed somehow" and what exception was thrown?
Without knowing more, one primary difference is that ArraySet modifies the array "in place". Whereas the cffunction does not. With udf's arrays are passed "by value", so the function has no effect on the original array object. Instead you must capture the returned array. (Of course, then it does not make any sense to require an array argument in the first place, but .. that is a different topic.)
arr = initializeValues([], 10);
writeDump(arr);
Since ArraySet modifies the array in place, it returns true/false. Perhaps you are mistakenly capturing the result of ArraySet and overwriting your array object?
// wrong: overwrites the array
arr = [];
arr = ArraySet(arr, 1, 10, 0);
writeDump(arr);
// correct
arr = [];
ArraySet(arr, 1, 10, 0);
writeDump(arr);

Related

Check inequality of two inputs in Cypress

I have to check whether a variable is less than a constant value in Cypress. How can we achieve this?
e.g: check whether a given variable is less than 30.
I'm making a few assumptions here, but, something like the following should work.
// define constant
const maxValue = 30;
cy.get('foo') // get element with variable text
.then(($el) => { // use the yielded element in a .then()
// get the text of the element using JQuery's `.text()`
// and compare the value using Chai's `lessThan`.
expect($el.text()).to.be.lessThan(maxValue);
});
References for inline comments:
JQuery's text()
Chai's lessThan (alias for below)

C++ passing by reference in constructor

So I am making a program in C++ and my main method has to construct an object that takes as a parameter a vector.
This is my code:
int main() {
vector<Seller> *staff = new vector<Seller>;
for (int i = 0; i < 50; i++) {
staff->push_back(Seller(i));
}
BookStore store(*staff);
deque<Book> books;
books = store.getBooks();
}
So, these are some pretty simple Object-Oriented concepts I think.
My goals are:
First, initializing an empty vector of sellers. A Seller is an object that has a constructor:
Seller(int i);
And represents, of course, a seller.
Then, I want to fill in the vector with actual Sellers. These are constructed in the for loop.
Then, I want to create a Store, which takes as an argument the sellers that work there.
Finally, I create a new deque called books, and I assign to it the value of books in the Store class. The initialisation of the Books deque is done in the constructor of the Store:
Store::Store(vector<Seller> &sellers) {
this->sellers = sellers;
this->books = deque<Book> (100, "Harry Potter");
}
So this is the code and I am wondering if I am making a mistake in the passing arguments to new constructors part.
I am a bit confused when passing by reference so I am asking for a bit on help on that part. I have two main questions:
1) Are there any errors there, considering how I want to run my program? Consider also that in the rest of the main method (not included here) I constantly change the value of the books deque.
2) Is there any way to replace an element in a deque without having to erase and insert?
Is there a built-in function replace? If not, is the below code going to work if I just want to replace a value in the deque?
For example, if the deque is like that:
3 4 5 2
And it (an iterator) has value 2.
Then I want the deque to become:
3 4 6 2
When doing:
books.erase(it);
books.insert(it, 6);
Thanks for any tips or help!
OK, here a short analysis.
Firstly, the unique real error I found: staff is defined as a pointer and is given a value with new but is never released. You should anyway avoid using raw pointers, so either create the object on the stack:
vector<Seller> staff{};
or use a smart pointer
auto staff = make_unique<vector<Seller>>{};
(you will then have to learn something about the ownership semantics, so as you still are a beginner I'd recommend the first solution).
Then, notice how the line
this->sellers = sellers;
in Store::Store will make a copy of the sellers vector, which probably is not what you meant. If you wanted your store to reference the variable created on main(), you should redefine your Store as
class Store {
// ...
vector<Seller>& sellers;
//...
};
and the constructor as
Store::Store(vector<Seller> &sellers) :
sellers{sellers} // reference member variables must be given a value before the body of the constructor begins
{
books = deque<Book> (100, "Harry Potter");
}
For the same reason, your line
books = store.getBooks();
will make a copy of the deque (but maybe in this case it was intended).
Finally, C++ offers many container manipulating functions under the <algorithms> library. Take a look at the reference. But if you already have an iterator to the element you want to replace, you do not need such algorithms, just write:
*it = 6;

Array of Variable Length Arrays

Normally, we can write this :
const char* names1[] = { "Stack", "LongerThanStack" };
const char* names2[] = { "Overflow", "LongerThanOverflow", "O" };
How to make a new array names that contains the above two arrays ?
Two dimensional arrays should do this.
I tried the following code, but G++ didn't accept it because of the variable length of the inner array.
const char* names[2][] = {{ "Stack", "LongerThanStack" },
{ "Overflow", "LongerThanOverflow", "O" }};
Unless you have a pressing need not to, do it like this:
std::vector<std::vector<std::string>> names = {{ "Stack", "LongerThanStack", "S" }, { "Overflow", "LongerThanOverflow", "O" }};
Each array's dimensions exept the first one must have bounds in multidimensional array. Valid code:
char* names[][3] = {{ "Stack", "LongerThanStack", "S" },
{ "Overflow", "LongerThanOverflow", "O" }};
Consider however use of containers from STL lib such as Array, Vector.
If your are using C++ is preferably to use vectors
1-As fast as C Array (of course not taking into account resize, even with that, if always better that implementing yourself).
2-Memory management (automatic, tested, stable), insertion is in amortized constant time O(1).
3-Security (bound checks, debug checks, possibility of using methods that do it for your ex: at(...))
4-Iterators (possibility of using the STL), performance when using algorithms.
5-Performance (cache friendly, continuous)
6-The default container to use.
C++11 improve various problems with vectors, initializations if one that was really improved
std::vector<std::vector<std::string>> names {{"1", "2"}, {"11", "22", "333"}, ... };
if you need static size arrays in C++11 was added std::array.

Weird results reading index 0 from the stack

Normally the Lua stack begins at index 1. However, I noticed a strange phenomenon when reading the stack address 0 provided by calling a cfunction.
--lua tables defined via Lua C API
h{ }
u{ }
f{ u{ s{} } }
--table calls
h(2)
u(3)
f.u.s(4)
All the above seen tables (h, u, and the nested s) have a __call metamethod pointing to the same cfunction. From that cfunction I'm reading/dumping the passed stack:
while(start_index <= lua_gettop(state)) {
switch(lua_type(state, start_index)) {
case LUA_TNUMBER:
std::cout << "LUA_TNUMBER:"<<lua_tonumber(state, start_index);
break;
//... dump for all the other types
When start_index starts at 1, the output is as expected: LUA_TABLE LUA_TNUMBER:3; It contains the table that contains the metamethod (or so I think) and the argument, 3.
However when start_index starts at 0, I'd imagine the result is not a valid Lua type, but it is. The results are inconsistent: When calling from Lua, index 0 is always a LUA_TNUMBER with the value 5.
When however calling from C++ with pcall (lua_getfield, lua_pushnumber, lua_pcall), index 0 yields the same LUA_TNUMBER(5) for calling f.u.s, but LUA_TABLE for h and u.
What is at index 0, why is it a valid Lua type, and why is its value so weirdly inconsistent?
0 is not a valid stack index, so you can't rely on finding anything there. It's kinda like taking your lua_State pointer and dereferencing (lua_State - 1) and asking what the value there is. It's garbage.
From the Lua manual:
Any function in the API that receives stack indices works only with valid indices or acceptable indices.
A valid index is an index that refers to a real position within the stack, that is, its position lies between 1 and the stack top (1 = abs(index) = top).
An acceptable index can be any valid index, including the pseudo-indices, but it also can be any positive index after the stack top within the space allocated for the stack, that is, indices up to the stack size.
(Note that 0 is never an acceptable index)
Looking at the source (see index2addr), it looks like if Lua is built with LUA_USE_APICHECK, your call would thrown an error.

PyTuple_Pack which takes in variable number of arguments

I am on Python 2.6.6 & calling python from c++ .
How can i write PyTuple_Pack which takes in variable number of arguments .
I have variable arguments coming which I use a for loop and populate my array . My array has max of 1000 elements but i populate only top no_of_argument elements using
python_obj[i] = PyString_FromString(arg[i])
I DO NOT want to write code like
if(no_of arg ==1 )
return PyTuple_Pack(1, python_obj[0]);
if(no_of arg ==2 )
return PyTuple_Pack(2, python_obj[0],python_obj[1]);
I want something like
return PyTuple_Pack(no_of_args, followed by my array where pyobjects exist ;
On net i can see http://docs.python.org/release/2.4.4/whatsnew/node14.html
A new function, PyTuple_Pack(N, obj1, obj2, ..., objN), constructs tuples from a variable length argument list of Python objects. (Contributed by Raymond Hettinger.)
However example to write good code using above is not found by my search for which i need your help pls..
Use PyTuple_New to make a tuple with the right size, then iterate over your array and use PyTuple_SetItem to fill the tuple with values.