What does ON do in QBasic? - qbasic

I have been working on a project, in which I take an old program and create a new version of it .... The thing is that it is written in Quick Basic and it has a line of code that I don't understand
ON FLAG% GOTO 1730, 1900
Can anyone tell me what this is ???
By the way I'm working on VisualBasic

It is basically a shorthand syntax for this type of statement:
IF FLAG% = 1 THEN GOTO 1730 ELSE IF FLAG% = 2 THEN GOTO 1900
See this article for more information.
For more options you would probably turn to a switch statement in more modern languages.

This is a more structured example of using GOTO statement:
SELECT CASE FLAG%
CASE 1
GOTO 1730
CASE 2
GOTO 1900
END SELECT

Related

New control transfer statements (labels) for Swift 2.0

What would be the best practices for the new control transfer statements (labels) in Swift 2?
Since I hear about it I can't stop compare it with the goto command from the good and old Basic language, what never was well accepted from experient programmers at the time.
Some uses of the control flow transfer looks ok, like in example below:
outer: for i in 1...100{
for j in 1...100{
print("\(i), \(j)")
if j == 10 {
break outer
}
}
}
But what are the limitations for the control flow transfer use?
When start to be bad practice to use it?
If its use was such a bad practice why it is back?
You can prefix a loop statement, an if statement, or a switch statement with a statement label followed by a colon (:) ,break and continue statements are used to change control flow in a loop statement or a switch statement.
break statement breaks the current loop execution where as continue just breaks current iteration.
consider:
firstLoop :for j in 0...1{
secondLoop: for i in 0...10{
if (i % 2) == 0{
if (i == 4){
break firstLoop
}
print(i)
}
}
}
Output will be : 0,2
If we replace break firstloop by break secondLoop o/p Will be: 0,2,0,2
If we replace break firstloop by continue o/p Will be: 0,2,6,8,10,0,2,6,8,10
If we replace break firstloop by continue firstloop continue o/p Will be again : 0,2,0,2
If continue is followed by a statement label then it will stop the current iteration of that statement label not the loop pointed by statement label.
goto VS statement label
. goto is a kind of unconditional branching ,where you can branch anywhere in the program,The goto statement is discouraged , because it alters the sequential flow of logic .
Why statement Label is used and what is it's Limitation?
. where as continue and break can make use of statement label if and only if it is written inside the loop which has same statement label which it is using so the code will be more safer. It makes programers life easy , off course you can't directly branch out to any part of program using continue,break and statement Label it can be considered as a limitation.
I think you are asking about using labels for break and continue statements. That is a much more limited, safer use than the horrible goto statements from basic. It simply lets you tell the compiler the scope you need to break out of/continue when you have nested looping constructs.
As far as I know break and continue is the only use of labels in Swift, and for that it seems useful and appropriate.

CASE/DECODE-style command for PowerShell?

This seems pretty petty but I am writing a PowerShell script which will write an email to a user, and it will refer to a number of "days" remaining taken from a variable ($DaysLeft).
To get this gramatically correct (in terms of "day"/"days"), I could (in Oracle SQL) use the DECODE function, thus:
DaysLeft ||' day'||DECODE(DaysLeft, 1, '', 's')||' Remaining'
Obviously you could do something similar with CASE, and that would be just as satisfactory.
I am wondering if PowerShell has a similar self-contained block I can use for this purpose. I could obviously define a variable with an IF-ELSE block, but I am hoping there is a more efficient way!
I saw some examples with the SWITCH command, but this doesn't seem to be what I'm after... or is it?
Thanks!
You could use a switch like this:
$numdays = 1
$phrase = switch ($numdays){1 {'is 1 day'};default {"are $_ days"}}
"There $phrase remaining."

Quick Basic Colon Line Separator

I am working on some old qbasic code. It's a mess with all the Goto statements. Am I correct that the following line will always return?
IF FLAG = 0 THEN TARGET = X: GOSUB 55000: TEMP = XI - TEMP2: RETURN
So if I understand this correctly the colon separates statements on the same line. The if only pertains to TARGET = X. The GOSUB, TEMP =, and RETURN will always execute. Correct?
Part of my confusion is because the very next line reads
IF FLAG = 1 THEN STEP = X: GOSUB 115000
And since the label to the second statement is never used in a GOTO I can't see that it would ever get executed.
Yes, I believe your assessment is correct. The colon is a statement separator that lets you have multiple statements on the same line. Assuming your subroutine at 55000 returns, this line should return as well.
I was wrong. Running this program:
if 1=2 then print "Never printed" : print "how about this?"
print "End of program"
on qb64.net prints only End of program. I assume that its grammar details are the same as Qbasic's, although it is a reverse-engineered effort.
As an aside, this code is written in a pre-QBasic style (e.g. using GOSUB and line numbers). There is a script that often came with QBasic (remline.bas, I believe it was called) that is supposed to help translate these kinds of programs to a newer style. I have never used it myself, though.

How to search for a particular string and goto sleep if not found in perl?

I have two test cases in log file ActivateJob and GetJOBStatus as below, my perl script currently set to PASS as default and it searches for Error in the below test cases, if it finds error in the below it makes test case as FAIL. For GetJOBStatus test case if it is ACTIVE script as to sleep for couple of mins and it as to perform GetJOBStatus again and if it is success test case as to be passed or else fail. How can i perform sleep and search for active in GetJOBStatus test case.. ply help me out... thanks in advance..
==============log file===================
Date and Time is:Thu, 20-06-2013 06:04:19
Line 4 Command:ActivateJob(Job=Test_Abort_New1);
Answer:
ActivateFTSJobResult = Success
COMPLETED
Date and Time is:Thu, 20-06-2013 06:04:19
Line 5 Command:GetJOBStatus(Job=Test_Abort_New1);
Answer:
GetJOBStatusResult = NELabel,Status,ErrorReason,Progress,CurrentUnit,Total
TSS_320_1,ACTIVE,No Error,0,BACKUP.DSC,0
COMPLETED
In order to check for a string in a file, you have to
open the file using open
read the file using <>
search the file using m//
close the file
In order to sleep (for a seconds), you have to sleep(a)
Keep in mind that unless you perform seek on your filehandle, you will have to close and then re-open it in order for it to reach the beginning.
As for you specific question, it appears to be implemented as an example in the seek perldoc

Debug output in T-SQL

When debugging T-SQL, is there something like Javascript's alert(); function or console.log I can use to output values when testing and debugging SQL scripts or stored procedures?
You can use PRINT to output messages to log but I personally prefer using RAISERROR with low severity because PRINT output is not always printed to screen immediately. Especially in long runnig SP's. On the other hand something like this prints immediately:
RAISERROR ('My Message', 10, 1)
Do not be fooled by the name ERROR, an error with severity 10 does no harm.
As for CTE, I think you are having a problem with self referencing Common Table Expressions. But you should know that there is no way to insert a PRINT statement into the execution of a recursive CTE.
To debug those I usually add an extra "Iteration Index" column to the result set that is incremented with each iteration. Also some other columns to help assess the situation. When examined with the rest of the resultset, it usually gives good insight. Something like the IDX col down. Other columns help me examine join conditions to see what was wrong:
WITH x AS (
SELECT 1 AS IDX, A._ID as ID_A, NULL as ID_B
FROM MYTABLE A
WHERE A._ID = 6
UNION ALL
SELECT X.IDX + 1, A._ID, B._ID
FROM MYTABLE B INNER JOIN X ON B._ID = X._ID + 1
WHERE B._ID < 20
)
SELECT *
FROM X
Yes, it's called PRINT. See here:
http://msdn.microsoft.com/en-us/library/ms176047.aspx
I use the instruction "RAISERROR" with the hint "WITH NOWAIT" like below:
RAISERROR('My message here', 10, 1) WITH NOWAIT
This instruction displays the message in time.
PRINT statement helps. Instead of PRINT giving output to result pane I sometimes insert the debug output to a table.
Newer version of SQL (2008 and 2008R2) has debug option. Not as solid as debugging a c# project in Visual Studio but quite good. You can go step by step and also create variable watchlist.