a = "stackoverflow" does not work in QBasic - type-mismatch

The qbasic code returns a type mismatch error.
a="StackOverflow"
print left$(a,5)
print right$(a,8)
What is the cause of this error and how can I rectify it?

The error is caused by the way you have named the variable.
"StackOverflow" is a string and cannot be assigned to variables of any other type.
In Qbasic, string variables must end with a $ symbol. So try a$ instead of a.
So try this code instead.
a$="StackOverflow"
print left$(a$,5)
print right$(a$,8)

You could define the variable as string first:
DIM a AS STRING
a = "StackOverflow"
PRINT LEFT$(a, 5)
PRINT RIGHT$(a, 8)

Related

Unable to set special characters in a variable value in a class in flutter dart

I am unable to set a string with special characters. It is working fine if I only set a string.
I have written this line:
static var var1 = "s$kncsfiu(AKdhjkhj^sjffhj&jh%";
But it is showing this error:
Any clues how can I set this variable value in Dart flutter?
$ is used in strings for when you want to insert the value of a variable directly into your string. If you want to write $ in your string, you need to escape it like:
static var var1 = "s\$kncsfiu(AKdhjkhj^sjffhj&jh%";
Or do it with marking your string as a "raw" string which disable features like the $ variable replacement:
static var var1 = r"s$kncsfiu(AKdhjkhj^sjffhj&jh%";
For more information, please read the following section in the Dart Language Tour: https://dart.dev/guides/language/language-tour#strings

Calling mysql procedure in python with parameter and getting errors

I use pymysql to connect to db. Here is the way I call the procedure.
cur.execute("call delete_shop_tables(?,?,?,?,?)", args)
The procedure expects 5 string variables.
args has five string variables in an array (args = ["one","two","three","four", "five"])
I get the following errors when the above line is executed.
TypeError: not all arguments converted during string formatting
What could I be missing?
try call delete_shop_tables(args)
This is the right way to call procedures.
It's a db we recently inherited. Once I collated the columns in WHERE clause to match with the collation what's in the column definition, it all worked.
I believe in pymysql the argument placeholder is %s, not ?. Try this
cur.execute("call delete_shop_tables(%s,%s,%s,%s,%s)", args)

Why does Gambas give me an error after I Dim a variable after a function call?

I'm playing around with gambas.
This code gives me the error "unexpected dim in FMain.class:6"
Public Sub Form_Open()
Print "this won't work"
Dim nickname As String = "gambas"
Print "Your new name is " & nickname
End
This code doesn't, and runs fine:
Public Sub Form_Open()
Dim nickname As String = "gambas"
Print "Your new name is " & nickname
End
Does gambas have requirements where variables are declared like pascal? I can't find any mention of it in the documentation. Thanks.
Gambas requires all DIM statements to be placed before any executable code inside a function or Subroutine (emphasis mine):
http://gambaswiki.org/wiki/lang/dim
All DIM declarations must be in the FUNCTION or SUB before the first executable command.
So change your code to this:
Public Sub Form_Open()
Dim nickname As String = "gambas"
Print "this will work"
Print "Your new name is " & nickname
End
Gambas' requirement for forward declaration of all local variables is very old-school. Sometimes it does make it easier to make self-documenting code and it incentivizes making functions short, but if a function has many intermediate short-lived local variables that cannot be immediately initialized (e.g. inside nested loops inside a function) then it hinders readability. YMMV.
This is not required anymore since Gambas 3.12.
But I suggest to continue declaring variables at the top function. It makes the code far more readable two years later.

How to access shadowed DXL variables/functions?

I encountered an error in a script I was debugging because somebody had created a variable with a name matching a built-in function, rendering the function inaccessible. I got strange errors when I tried to use the function, like:
incorrect arguments for (-)
incorrect arguments for (by)
incorrect arguments for ([)
incorrect arguments for (=)
Example code:
int length
// ...
// ...
string substr
string str = "big long string with lots of text"
substr = str[0:length(str)-2]
Is there a way to access the original length() function in this situation? I was actually just trying to add debug output to the existing script, not trying to modify the script, when I encountered this error.
For now I have just renamed the variable.
Well, in the case that you had no chance to modify the code, e.g. because it is encrypted you could do sth like
int length_original (string s) { return length s }
<<here is the code of your function>>
int length (string s) {return length_original s }

how to to find a string in groovy list

question from a groovy newbie:
sql is initiated as follows
final Binding binding = new Binding();
binding.setProperty("sql", sql);
final groovy.sql.Sql sql = Sql.newInstance(dbConfig.getUrl(), dbConfig.getUserName(), dbConfig.getPasswd(),"oracle.jdbc.OracleDriver");
I am running a query in groovy like this
def listOfRows = sql.rows (select column1 from table1);
listOfRows when printed shows contents like [[column1_name:value1], [column1_name:value2], [column1_name:value3]]
I want to check if value2 (a String) exists in the returned list of values from the above query.
I have tried doing listOfRows.contains('value2') and listOfRows.find('value2'),
it complains that the method does not exist for lists..
what's the best way of doing this ?
EDITED: I have corrected the list of printed values. What's being returned is List<GroovyResultSet>
and I have also added the definition of sql.
I would suggest you to take a look at groovy documentation, and particularly to collections documentation (both tutorial and JDK/GDK).
in that case, the most specifically adapted solution would be to use Collection#find() ... with something like
listOfRows.find { it.contains(':value2') }
Which can be translated into human-readable
find the first element in this collection which string contains ":value2".
You probably want
listOfRows.column1.contains( 'value2' )
You are probably invoking this method which takes a GString (note that GString != String) as an argument. According to this question, a string in single quotes is a standard java string, and a string in double quotes is a templatable string.
'hello' //java.lang.String
"hello" //groovy.lang.GString
Try this:
listOfRows.contains("value2")
what i ended up doing is following :
iterate the listOfRows, get all the values for column1 from each GroovyResultSet into a listOfValues ,then check for my values in that list.
def listOfValues=[];
listOfRows.collect(listOfValues){it.getAt('column1')};
if(listOfValues.size()==3){
println('success');
}

Resources