every now and then I stumble over an error-message like the one in this case:
if "," in text.erase():
print ("comma erased")
error(109,1): Too few arguments for "erase()" call. Expected at least
2.
Whatever I try to put into those (), nothing seems to work. How can I find out what arguments I need in such a case?
At least some basic programming knowledge provided, the editor's Search Help offers some useful info in such a case:
void erase ( int position, int chars )
Erases chars characters from the string starting from position.
Related
I've created a script file reader, nothing more than a glorified text reader that changes loop cases in my program, but I need it to be able to ignore comments on a line, execute that command, and go to the next line and process the new command after it finds the comment denoted with a semicolon. For the life of me, I can't figure out how to do this.
Currently, the commands are read in like this:
DO THIS FUNCTION
DO THAT FUNCTION
I'd like to comment it with a semicolon like this:
DO THIS FUNCTION ;this is a comment to be ignored
Below is my text file read code, should be able to drag and drop it in to test. The command indicator just echoes the command being read. I've removed the rest of my program, sorry, can't send that part.
Can someone shed some light?
Is a semicolon used anywhere else in your file? Or is it just used to indicate a comment?
If it is only used to indicate a comment then as you read each line in, call the Split String primitive and split at the ";". Just use the top output regardless of whether or not the line contains a semicolon:
You can use the "Match Regular Expression Function" to split up the string, as #Moray already suggested.
Sadly I can't give you an example vi right now.
The main idea is:
find the "Match Regular Expression Function"
give it a ; as char to search for
there are three outputs of the function (before match, match, after match)
use the 'before match' instead of the whole line and give it to the rest of your program
This only works if your commands don't contain any ; except for the comments.
Note: I not quite sure what happens if you give the function a string that doesn't contain ; but you can figure that out by yourself by using the detailed help to this function :)
I look for a way to search in a string, starting from the last character.
I do have a solution with a for next loop and parse the string one by one. But there must be a smarter way to do this. I have tried to do this with
pos (" "=i$)
but this statement starts from the begin
The loop will do, but it's slow. There is a inbuild command you can use
I$="123 ABC DEF"
X = POS (" "=I$,-1)
This gives you the position of the last space in I$
result of this is 7
Another option is using MASK() which is pretty much similar to the unix "grep"
In an ancient PowerBasic file, I found this in the code:
%AppendRec= 1% '^a Write/Append Btrieve record to named file
%PrtBar= 2% '^b Print a Bar Code
My question deals with the numbers after the = sign. I assume the trailing % has a meaning, but I can't figure out what that meaning is.
I know that in QB, % denotes an Integer type but that normally leads the variable as shown at the beginning of the code lines. The trailing % has me confused.
It is used to specify the type of the constant, so you don't have e.g. "1" evaluate to a float and then have to be converted to an int.
This page shows you the defaults that PB uses if you don't explicitly specify the type of constants.
Like Charles said it defines the type as int. Prefixing it with % like the variable on the left is defining a numeric equate/constant. The page link is old now though and their new help is at PowerBasicHelp
Look for Numeric Equates and Data Types.
I would like to implement a small subset of siri/cortana like features in command line.
For e.g.
$ What is the sum of 100 and 1000
> Response: 1100
$ What is the product of 10 and 12
> Response: 120
The questions are predefined regular expressions. It needs to call the matching function in ruby.
Pattern: What is the sum of (\d)+ and (\d)+
Ruby method to call: sum(a,b)
Any pointers/suggestion is appreciated.
That sounds exactly like cucumber, maybe take a look and see if you can just use their classes to hack something together :) ?
You could do something like the following:
question = gets.chomp
/\A.*(sum |product |quotient |difference )\D+([0-9]+)\D+([0-9]+).*\z/.match question
send($1, $2.to_i, $3.to_i)
Quick explanation for anyone that may be new to matching in Ruby:
This gets a line of input from the command line and scans it for a function name (i.e. sum, product, etc) followed by a space and potentially some non-digit characters. Then, it looks for a first number (similarly followed by a space and 0 or more non-digit characters) and a second number followed by nothing or anything. The parentheses determine what gets assigned to the variables preceded by a $, i.e. the substring that matches the contents of the first set of parentheses gets assigned to $1.
Next, it calls the method whose name is the value of $1 with the arguments (casted to integers) found in $2 and $3.
Obviously, this isn't generalized at all--you're putting the method names in the regex, and it's taking a fixed number of arguments--but it'll hopefully be useful for getting you on the right track.
Has anyone in this forum attempted to solve the ACM programming problem http://acm.mipt.ru/judge/problems.pl?browse=yes&problem=024? It is one of the simpler problems in ACM MIPT and the goal is to evaluate an expression consisting of +, -, * and parentheses. Despite the apparent simplicity, I haven't been able to get my solution accepted, apparently because one of the test case expressions has an operator not stated in the problem. I even added support for division ('/') but that too didn't help. Any idea on what other operator needs to be supported? FYI, my program removes all whitespaces from the input before processing so that spaces shouldn't be a problem. Anything not stated in the problem but needs to be taken care of?
You're being bitten by ruby's handling of strings and characters.
curr_ch = #input[i]
gives you an integer, for the input you get, the ASCII code of the character at index i of the input.
curr_ch == '('
for example compares that integer to the string "(", of course that fails. Also the regex matches fail because you pass them an integer where a string is expected.
Replacing all occurrences of some_var = #input[some_index] with some_var = #input[some_index...some_index+1] gives me a programme that seems to work (it works on a few test inputs I gave it). Probably someone who actually knows the quirks of ruby can give you a better fix.