DBI binding parameters and square brackets - perl

I'm having problems with the following code:
$sql = <<"END_SQL";
SELECT DISTINCT Matching.[CI M], Matching2_1.[LAC M], Matching2_1.[CI M], Matching.[Band M], Matching2_1.[Band M], Matching.Site, Matching2_1.Site, Matching.[BSC/RNC], Matching.[CellName M], Matching.[BSC/RNC M], Matching2_1.[CellName M]
FROM Matching, [N 900 - 900], Matching AS Matching2_1
WHERE Matching.[Band M]= ? AND Matching2_1.[Band M]= ? ;
END_SQL
$sth = $dbh->prepare($sql);
$sth->execute(900, 900);
The columns name contains space, the database is MS access so I use square brackets to use them in a query
The problem is that Perl interprets the square brackets as a binding parameters and expects 11 parameters.
Here is the error:
DBD::ODBC::st execute failed: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 11. (SQL-07002) at NEIGHBORS MAPPING.pl line 89.

The Access Database Engine can also recognize backquotes as table/field delimiters, so if the quare brackets are giving you trouble then try
SELECT DISTINCT Matching.`CI M`, ...

Related

Julia: ERROR: LoadError: MethodError: Cannot `convert` an object of type Expr to an object of type Symbol

I am trying to multiply two array via the Einsum package (uses Meta Programming). I get the following error if I use the #einsum macro with elements of a struct but not if I copy the element beforehand. Can someone explain?
using Einsum
struct MyStruct
a::Array
end
s1 = MyStruct(rand(5, 2))
s2 = MyStruct(rand(6, 2))
# does not work (ERROR: LoadError: MethodError: Cannot `convert` an object of type Expr to an object of type Symbol)
#einsum result[i, j] := s1.a[i, k] * s2.a[j, k]
# does work
s1_a = s1.a
s2_a = s2.a
#einsum result[i, j] := s1_a[i, k] * s2_a[j, k]
The macro #einsum assumes, in the function extractindices which it uses, that the arrays are simple names (i.e. Symbols like s1_a), not expressions like s1.a or function calls or some such thing. It has simply not been written to accomodate indexing of expressions. The package Einsum has not been updated in 4 years, there might be other packages that can achieve this.

spsolve overloading and rowvec type conversion consistency

With the following declarations:
uvec basis;
rowvec c;
sp_mat B;
The expression c(basis) seems to return an
arma::subview_elem1<double, arma::Mat<unsigned int> > and the following call appears to work:
vec pi_B = spsolve(trans(B), c(basis), "superlu");
How does spsolve resolve this input?
Also vec pi_B = spsolve(trans(B), trans(c(basis)), "superlu"); throws a dimensional mismatch error but the following runs:
rowvec d;
vec pi_B2 = spsolve(trans(B), trans(d), "superlu");
According to the documentation, c(basis) is a non-contiguous submatrix, where basis specifies which elements in c to use.
In this case c is "... interpreted as one long vector, with column-by-column ordering of the elements" and that "... the aggregate set of the specified elements is treated as a column vector", which means that c(basis) produces a column vector.

How to format print statements with two vector variables?

I'd like to write several messages and tables on the same .txt file.
For example:
x=[23.9,10.9,8.9,14.2]
y=[9.83,8.04,7.47,8.32]
file=fopen('Results.txt','wt');
fprintf(file,'Results1\n');
fprintf(file,'%.2f %.2f\r\n',x,y);
fprintf(file,'Results2\n');
fclose(file);
I get this result as .txt:
Results1
23.90 10.90
8.90 14.20
9.83 8.04
7.47 8.32
Results2
But I should get this one:
Results1
23.90 9.83
10.90 8.04
8.90 7.47
14.20 8.32
Results2
Instead of fprintf(file,'%.2f %.2f\r\n',x,y);), I was trying to use:
ResultsTable2 = table(x,y);
writetable(file,ResultsTable2);
but didn't succeed. How to write the required .txt file?
Careful examination of your output shows that all the elements of x were printed before all the elements of y.
The documentation confirms that this is the expected behavior. Check out this example
A1 = [9.9, 9900];
A2 = [8.8, 7.7 ; ...
8800, 7700];
formatSpec = 'X is %4.2f meters or %8.3f mm\n';
fprintf(formatSpec,A1,A2)
X is 9.90 meters or 9900.000 mm
X is 8.80 meters or 8800.000 mm
X is 7.70 meters or 7700.000 mm
Even though the arguments to fprintf are in the order A1, A2. It first prints all the values from A1, and then it prints all the values from A2 going in single index order.
Therefore, if you want to alternate values from x and y during printing, you need to interleave them in a new variable. There are several possible ways to do so.
One example,
XY = reshape([x;y], 1, []);
Then everything should print as expected
fprintf(file, '%.2f %.2f\r\n', XY);
% or if you want to print to command window
% fprintf('%.2f %.2f\r\n', XY);
23.90 9.83
10.90 8.04
8.90 7.47
14.20 8.32
The correct answer for how to output data with fprintf is given by Cecilia: each argument will be iterated completely through in the order it appears in the argument list, so you have to combine the data into one matrix argument that will be iterated through column-wise to generate the desired output.
You also mentioned trying to use a table and the writetable function, so I though I'd add the correct way to do that in case you were curious:
ResultsTable2 = table(x(:), y(:)); % Pass data as column vectors
writetable(ResultsTable2, 'Results.txt', 'WriteVariableNames', false);

Calculator doesn't execute the operations

I am trying to build a calculator in abap. The requirements are:
Reads
two numbers (ex. 56.3 and 78.2)
a character from the following list: q, w, e, r, t
Displays the result of the operation specified by the character
Addition for character q
Subtraction for character w
Multiplication for character e
Division for character r
Exponent for character t
I have created a table with the operations that I am using in the calculator.
The problem is when I execute the program it only prints my last option (else) "the operation is not possible".
Here's the code I wrote:
REPORT Z_CALCULATOR_V2.
TABLES: ZOPERATII.
DATA result type p decimals 2.
DATA Q type c.
DATA W like Q.
DATA E like Q.
DATA R like Q.
DATA T like Q.
PARAMETERS Nr_1 type p decimals 2 OBLIGATORY.
PARAMETERS Nr_2 like Nr_1 OBLIGATORY.
PARAMETERS Operatie LIKE zoperatii-operatie OBLIGATORY.
if Operatie = Q.
result = Nr_1 + Nr_2.
elseif Operatie = W.
result = Nr_1 - Nr_2.
elseif Operatie = E.
elseif Operatie = R.
result = Nr_1 / Nr_2.
elseif Operatie = T.
result = Nr_1 ** Nr_2.
else.
WRITE 'The operation is not possible'.
endif.
write result.
if you change the data declarations to:
DATA Q type c value 'Q'.
DATA W type c value 'W'.
DATA E type c value 'E'.
DATA R type c value 'R'.
DATA T type c value 'T'.
the code should run as you expect. That said, you should read up on the documentation as suggested in the comments.

How to write symbols to file in scilab

I'm working on symbolic toolkit. Trying to solve some equations and that's a long string of symbols such as x= a1+a2^3+b0*b1... upto 80,000(80k) characters.
So I needed to put that in file.
mputstr() ans other wrting functions are not working since they are symbols.
Error is thrown as: not a string or specified format.
Does any method can help to bring down the variable to file.
code is :
Syms aa ab ac
z=ab^6*ac^6*ad^3*ba^3*bg^3*bh^3+3*aa^4*ab^6*ac^6*ad^4*ba^4*bg^2*bh^2+3*aa^5*ab^6*ac^6*ad^5*ba^5*bg*bh+aa^6*ab^6*ac^6*ad^6*ba^6
mputstr({char(z)},fd)
>>error 10000
>>char: Wrong type for input argument: Cell expected.
at line 95 of function char called by :
mputstr(z,fd)
>> !--error 999
>mputstr: Wrong type for input argument #1: A string expected.
p=string(z)
mputstr(p,fd)
>>!--error 999
>mputstr: Wrong type for input argument #1: A string expected.
mfprintf("%s",z)
>> !--error 246
>>Function not defined for given argument type(s),
check arguments or define function %c_mfprintf for overloading. ..
Let's say you have a symbolic equation x:
syms a b c
x = a + b * c
Here, x denotes a symbolic variable, so you cannot directly write it to a file. You need to convert it to a character array first. So you should be using something like
fd = mopen( this_file, "wt" );
mputstr( char(x), fd );
mclose( fd );
I think that #bremen_matt's answer is the good one, but with a modification.
If your "Syms" variables are something complex so char() and string() cannot be used, why you are not creating your own conversion function?
Please, see below my modification of #bremen_matt example:
syms a b c
x = a + b * c
fd = mopen( this_file, "wt" );
mputstr( syms_to_string(x), fd );
mclose( fd );
The syms_to_string() is returning a string of the information that you would like to print of the symbol x, and same function could be used to print other symbols (e.g. a). Of course, the syms_to_string() function could be better defined using overloading.