FoxPro Unique Random Number - visual-foxpro

Could some one help, Looking for Unique random generated number.
Currently have 4 pubs, If data have more than 4 pubs, need randomly select the pubs and place the value to Pub1 Pub2 Pub3 and Pub4 fields. Can't find solution by using FoxPro.
SELE A
USE TEST
REPL PUB1 WITH "" ALL
REPL PUB2 WITH "" ALL
REPL PUB3 WITH "" ALL
REPL PUB4 WITH "" ALL
REPL RANDOM1 WITH "" ALL
REPL RANDOM2 WITH "" ALL
REPL RANDOM3 WITH "" ALL
REPL RANDOM4 WITH "" ALL
REPL RANDOMLOG WITH "" ALL
SELE B
USE WHATPUB
SELE A
GO TOP
DO WHILE !EOF()
cBRANCH=BRANCH
SELE B
SET FILTER TO BRANCH=cBRANCH
COUN TO nBRANCHQTY
IF nBRANCHQTY<=4
FOR loop=1 TO nBRANCHQTY
SELE B
LOCA FOR loop=FT_URN
IF FOUND()
cPUBID=PUBID
SELE A
cFLD1="PUB"+LTRIM(STR(loop))
REPL (cFLD1) WITH cPUBID
ENDIF
NEXT loop
ELSE
SELE A
FOR loop=1 TO 4
SELE A
DO WHILE nRANDOMPUB>nBRANCHQTY
nRANDOMPUB=INT(RAND()*10)+1
ENDDO
SELE B
LOCATE FOR nRANDOMPUB=FT_URN
IF FOUND()
cPUBID=PUBID
SELE A
cFLD1="PUB"+LTRIM(STR(loop))
cFLD2="RANDOM"+LTRIM(STR(loop))
REPL (cFLD1) WITH cPUBID
REPL (cFLD2) WITH LTRIM(STR(nRANDOMPUB))
ENDIF
nRANDOMPUB=9999999
NEXT loop
ENDIF
SELE A
SKIP
ENDDO
GO TOP
BROW FIELDS BRANCH,RANDOMLOG,RANDOM1,PUB1,RANDOM2,PUB2,RANDOM3,PUB3,RANDOM4,PUB4
If something is not understandable, just let me know.

You are using int(rand() * 10) + 1. Be it VFP or any other language your intent is to select a random number between 1 and 10. There is nothing in your code that says it should be unique Pub1, Pub2, ... It is normal that repeated calls to rand may return same value when you are choosing 4 values out of max 10. Instead you could do something like this (untested off the top of my head):
Create Cursor crsRand (pubVal i)
Local ix
For ix = 1 To 10
Insert Into crsRand Values (m.ix)
Endfor
Rand(-1)
Select 0
Use TEST
Replace PUB1 With "", ;
PUB2 With "", ;
PUB3 With "", ;
PUB4 With "", ;
RANDOM1 With "", ;
RANDOM2 With "", ;
RANDOM3 With "", ;
RANDOM4 With "", ;
RANDOMLOG With "" All
Use WHATPUB In 0
Local cBRANCH, nBranch, nBRANCHQTY, Loop, cPubId, cFld1
Local Array laRandPub[1]
Select TEST
Scan
cBRANCH=BRANCH
Select WHATPUB
Count For BRANCH=m.cBRANCH To nBRANCHQTY
If m.nBRANCHQTY<=4
Select WHATPUB
For Loop=1 To m.nBRANCHQTY
Locate For FT_URN=m.loop
If Found()
cPubId=PUBID
Select TEST
cFld1="PUB"+Ltrim(Str(m.loop))
Replace (m.cFld1) With m.cPubId
Endif
Endfor
Else
Select pubVal From ;
(Select Top 4 pubVal, Rand() From crsRand Where pubVal <= m.nBRANCHQTY Order By 2) tmp ;
into Array laRandPub
For Loop=1 To 4
Select WHATPUB
Locate For FT_URN=laRandPub[m.loop]
If Found()
select Test
cPubId=PUBID
cFld1="PUB"+Ltrim(Str(m.loop))
cFLD2="RANDOM"+Ltrim(Str(m.loop))
Repl (m.cFld1) With m.cPubId, (m.cFLD2) With Ltrim(Str(laRandPub[m.loop]))
Endif
Endfor
Endif
Endscan
Locate
Brow Fields BRANCH,RANDOMLOG,RANDOM1, ;
PUB1,RANDOM2,PUB2,RANDOM3,PUB3,RANDOM4,PUB4
Note that Select A, Select B style coding is dangerous. Use aliases instead.

Related

Convert numbers to Excel Column Letters in Power Query

I would like to transform Numbers to Excel Column Letters.
There are several threads on the topic but it seems that it has not been answered yet for M language.
I managed to address it with the following code but I am sure someone will have a much more efficient code to propose:
= Table.AddColumn(Source, "Column Letter", each Text.Combine({ if (Number.IntegerDivide(
Number.IntegerDivide(
Number.IntegerDivide([Column1]-1,1),26)-1,26))<1 then "" else Character.FromNumber( 64+Number.IntegerDivide(Number.IntegerDivide(Number.IntegerDivide([Column1]-1,1),26)-1,26)),
(if Number.IntegerDivide([Column1]-1,26)-
Number.Abs(
Number.IntegerDivide(
Number.IntegerDivide([Column1]-1,26)
-1,26)*26)<1 then "" else
Character.FromNumber(64+
Number.IntegerDivide([Column1]-1,26)-Number.Abs(
Number.IntegerDivide(
Number.IntegerDivide([Column1]-1,26)-1,26)*26))),
(if [Column1]-Number.Abs(
Number.IntegerDivide([Column1]-1,26)*26)<1 then "" else Character.FromNumber(64+[Column1]-Number.Abs(
Number.IntegerDivide([Column1]-1,26)*26)))}))
Thank you!
I think this custom function will work also:
Edit*Formula for b corrected as per #VassilisAgapitos note below
(n as number)=>
let
//make sure it is an integer
a = Number.IntegerDivide(n,1),
b = Number.IntegerDivide(Number.IntegerDivide(a-1,26)-1,26),
L1 = if b = 0 then null else Character.FromNumber(b+64),
c = Number.IntegerDivide(a-b*26*26-1,26),
L2 = if c = 0 then null else Character.FromNumber(c+64),
d = a-b*26*26-c*26,
L3 = Character.FromNumber(d+64)
in
Text.Combine({L1,L2,L3})
You can use it as a Table.AddColumn argument:
#"Added Custom" = Table.AddColumn(#"Changed Type", "Column Letter", each fnNumberToExcelColumn([Column Number]), type text)
An uglier version than Ron's, just for fun
=(if [Column1] <703 then "" else Character.FromNumber(64+Number.IntegerDivide([Column1]-27,26*26)))
&( if [Column1] >26 then Replacer.ReplaceText(Character.FromNumber(64+ Number.Mod( Number.IntegerDivide([Column1]-1,26) ,26) ), "#", "Z") else "" )
&(Character.FromNumber(65+Number.Mod([Column1]-1,26)))

RPGLE End Field Name with an Ascending Variable

I have a simple question that involves numbering the end of field names with a numeric variable.
Exp: FIELD,X = FIELD01, FIELD,X = FIELD02, ETC....
Z-ADD 1 X 2.0
DOU X = 10
FIELD,X IFEQ *BLANK
MOVE FIELDREAD FIELD,X
ENDIF
ADD 1 X
ENDDO
I could do this in RPG but I'm not sure how to do this in RPGLE. When I try to I get this error: Entry contains data that is not valid; only valid data is used.
Thanks!!
The syntax for array indexes in RPGLE is ARR(X).
FIELD(X) IFEQ *BLANK
If you're not sure about the syntax for RPGLE, try writing a little bit of code in RPG, and then use CVTRPGSRC to convert it to RPGLE.
fSomeFile if e k disk
D ArMax c 10
D Key1 S like(KeyField)
D Field S dim(ArMax) like(FieldRead)
Exsr $Sample1;
*inlr = *on;
return;
Begsr $Sample1;
c z-add 1 X 3 0
setll (key1) SomeFile
dou x = 10;
if Field(x) = *blank;
reade (key1) SomeFile;
if %found(SomeFile);
Field(x) = FieldRead;
endif;
endif;
X = X +1;
enddo;
Endsr;

Netlogo How to avoid NOBODY Runtime error?

How can I avoid NOBODY Runtime error? The following is sample code. This is a code that can be used for zero division error avoidance. Therefore, I know that it can not be used to avoid NOBODY error. But I can not find any other way. The following is a Runtime error message-> "IFELSE-VALUE expected input to be a TRUE / FALSE but got NOBODY instead." I appreciate your advice.
set top ifelse-value (nobody)
[ 0 ][ top ]
set ts turtles with [speed = 0 and not right-end]
set top max-one-of turtles [who]
set topx [xcor] of top ; A NOBODY error appears in "of" of this code
set L count ts with [xcor > topx]
The input of ifelse-value needs to be a reporter the returns either true or false (full details here. So, if you use nobody as the input, Netlogo does not evaluate whether or not the input is nobody or not, it just reads nobody- in other words your input is not returning either true or false.
For input, then, you need to instead use a boolean variable (one that is either true or false), a to-report that returns true or false, an expression that Netlogo can evaluate, etc. Consider the following examples:
to go
let x true
set top ifelse-value ( x )
["x is true"]
["x is NOT true"]
print ( word "Example 1: " top )
set top ifelse-value ( this-is-true )
["Reporter returned true"]
["Reporter did not return true"]
print ( word "Example 2: " top )
set x nobody
set top ifelse-value ( x = nobody )
["x IS nobody"]
["Is NOT nobody"]
print ( word "Example 3: " top )
set x 0
set top ifelse-value ( x = nobody )
["x IS nobody"]
["x Is NOT nobody"]
print ( word "Example 4: " top )
set top ifelse-value ( nobody = nobody )
["nobody = nobody"]
["nobody != nobody"]
print ( word "Example 5: " top )
end
to-report this-is-true
report true
end

Balance variable keeps resetting after each loop

I was wondering how I'm supposed to get my "balance" variable to change depending on the value added or subtracted to it each time, without resetting for the next loop.
At the moment it will add or subtract a given value and return the correct number but on the next loop it will use the original base value instead of using the new value from the previous loop.
import time
print ("Welcome to balance tracker!")
sBal=float(input ("Starting balance: "))
float(sBal)
transactionAmounts = []
transactionTypes = []
print ("Your current balance is", '$',sBal)
while True:
input("Press enter to continue")
print ("""
------------
[A]ddition
[S]ubtraction
[H]istory
[I]nformation
[Q]uit
------------
""")
choice=input("What would you like to do? ")
choice = choice.upper()
if choice == "A":
aval=float(input ("Enter amount you would like to add here: "))
nBal=(sBal)+(aval)
transactionTypes.append('Addition')
transactionAmounts.append(float(aval))
balance = float(sBal) + aval
print ("Your current balance is $",balance)
elif choice == "S":
aval=input("Enter amount you would like to subtract here: ")
nBal=float(sBal)-float(aval)
transactionTypes.append('Subtraction')
transactionAmounts.append (float(aval))
balance = float(sBal) - aval
print ("Your current balance is $",balance)
elif choice == "H":
aCount = 1
tCount = 0
for i in transactionAmounts:
print ('Transaction',aCount,':',transactionTypes[tCount],i)
aCount = aCount + 1
tCount = tCount + 1
elif choice == "Q":
break
else:
print ("invalid choice")
The solution to your problem is that when your assigning your balance variable, your adding sBal(starting balance) every time to the amount they would like to add... This is causing the balance to always be set back to the starting balance plus or minus the amount added. Try defining sBal as balance also like so...
balance = float(input("Starting balance: ")) #line 5
float(sBal) #<<<can be deleted #line 6
Also don't forget to alter anywhere in the code that calls sBal to call balance.
print("Your current balance is ", "$", difference) #line 9
balance = float(balance) (+ or -) aval #lines 30 and 37
Also you can delete your nBal(new balance) as it is never called.

Problem in oracle query

Hai guys,
I've a query in which i need to interchange the values of two fields.
The query is as follows:
SELECT TO_DATE(A.G_LEDGER_DATE,'dd/mm/YYY')as G_LEDGER_DATE,C.ACC_MASTER_NAME,
A.G_LEDGER_REF_NO ,
NVL(CASE WHEN B.G_LEDGER_SECTION = 1 THEN
CASE WHEN
(SELECT COUNT(*)FROM SOSTRANS.ACC_GEN_LEDGER WHERE G_LEDGER_SECTION = B.G_LEDGER_SECTION AND G_LEDGER_ID = B.G_LEDGER_ID)> 1 THEN
B.G_LEDGER_VALUE ELSE A.G_LEDGER_VALUE END END,0) AS G_LEDGER_DR_VALUE,
NVL(CASE WHEN B.G_LEDGER_SECTION = -1 THEN
CASE WHEN
(SELECT COUNT(*) FROM SOSTRANS.ACC_GEN_LEDGER WHERE G_LEDGER_SECTION = B.G_LEDGER_SECTION AND G_LEDGER_ID = B.G_LEDGER_ID)> 1 THEN
B.G_LEDGER_VALUE ELSE A.G_LEDGER_VALUE END END,0) AS G_LEDGER_CR_VALUE,
B.G_LEDGER_SECTION,C.ACC_MASTER_ID,SUBSTR(A.G_LEDGER_REF_NO,0,3) AS Types,'Z' as OrderChar ,
CASE WHEN A.G_LEDGER_REMARK IS NULL THEN B.G_LEDGER_REMARK ELSE A.G_LEDGER_REMARK END AS Narration
FROM SOSTRANS.ACC_GEN_LEDGER A
LEFT OUTER JOIN SOSTRANS.ACC_GEN_LEDGER B ON A.G_LEDGER_ID = B.G_LEDGER_ID
LEFT OUTER JOIN SOSMASTER.ACC_ACCOUNT_MASTER C ON A.ACC_MASTER_ID = C.ACC_MASTER_ID WHERE A.G_LEDGER_CANCEL='N' AND
B.ACC_MASTER_ID = 'MSOS000001' AND
A.ACC_MASTER_ID <> 'MSOS000001' AND
A.G_LEDGER_SECTION <> B.G_LEDGER_SECTION AND
A.G_LEDGER_DATE >= '25/sep/2009' AND
A.G_LEDGER_DATE<='26/sep/2009'
ORDER BY OrderChar,G_LEDGER_DATE
Now i get the output as
... G_LEDGER_DR_VALUE G_LEDGER_CR_VALUE .....
... 2000 0 .....
... 3000 0 .....
... -1000 0 .....
I need to get the negetive value of the G_LEDGER_DR_VALUE side in G_LEDGER_CR_VALUE and if negetive value exists in G_LEDGER_CR_VALUE then it should be in the G_LEDGER_DR_VALUE field
Can anyone help me to solve this?
If I understood your question well, you select a value (that I will call g_ledger_value) that you want to appear in a different column depending on its sign.
This is how I would do it :
SELECT
CASE WHEN t.g_ledger_value>0 THEN t.g_ledger_value ELSE 0 END AS g_ledger_dr_value,
CASE WHEN t.g_ledger_value<0 THEN t.g_ledger_value ELSE 0 END AS g_ledger_cr_value
FROM
(SELECT g_ledger_value FROM mytable) t;
It sounds like a combination of SIGN() and CASE is what you need ...
CASE WHEN SIGN(G_LEDGER_DR_VALUE) = -1 then ...
ELSE ...
END
etc
SELECT G_LEDGER_DR_VALUE,
CASE WHEN G_LEDGER_DR_VALUE < 0
THEN G_LEDGER_CR_VALUE
ELSE G_LEDGER_DR_VALUE
END
FROM (...)
Is it that you mean? I suggest calculate values of CR___VALUE and DR_VALUE in subquery, and then in wrapping query make CASE which returns you correct value.

Resources