Page 1 of 1 1
Topic Options
#170068 - 2006-11-05 10:44 PM Mini-Golf
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Had to solve the following in SQL recently, thus nice Mini-Golf topic.

Task is to write a UDF that accepts either an interger value or packed integer. A packed integer is converted from an integer such that the plus/minus-sign is embedded in the last character of the converted number. the translation table is as follows:
Code:

SysVal ActVal PosNeg
A 1 1
B 2 1
C 3 1
D 4 1
E 5 1
F 6 1
G 7 1
H 8 1
I 9 1
J 1 -1
K 2 -1
L 3 -1
M 4 -1
N 5 -1
O 6 -1
P 7 -1
Q 8 -1
R 9 -1
{ 0 1
} 0 -1



Therefore integer "1" would be written as packed integer "A" and integer "-1" would be written as packed integer "J". Note that you can write "0" as both "{" and "}", either one is correct

Furthermore, integer "1234" would translate to "123D" and integer "-1234" would then be "123M".
_________________________
There are two types of vessels, submarines and targets.

Top
#170069 - 2006-11-05 11:06 PM Re: Mini-Golf
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
can't understand the task but thanks for the turnament.
Top
#170070 - 2006-11-05 11:54 PM Re: Mini-Golf
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Okay, I'll post my SQL code, that'll make the task easier to uderstand. However, I just do the conversion from packed decimal to decimal. Still, primitive solution:
Code:

CREATE function dbo.convert_COBOL_unpacked_decimals (@amtstring as varchar(255))
returns decimal(18,2)
begin
declare @amtdecimal integer
set @amtdecimal =
case
when ltrim(rtrim(@amtstring))='' then NULL
when right(@amtstring,1)='R' then '-'+left(@amtstring,len(@amtstring)-1)+'9'
when right(@amtstring,1)='Q' then '-'+left(@amtstring,len(@amtstring)-1)+'8'
when right(@amtstring,1)='P' then '-'+left(@amtstring,len(@amtstring)-1)+'7'
when right(@amtstring,1)='O' then '-'+left(@amtstring,len(@amtstring)-1)+'6'
when right(@amtstring,1)='N' then '-'+left(@amtstring,len(@amtstring)-1)+'5'
when right(@amtstring,1)='M' then '-'+left(@amtstring,len(@amtstring)-1)+'4'
when right(@amtstring,1)='L' then '-'+left(@amtstring,len(@amtstring)-1)+'3'
when right(@amtstring,1)='K' then '-'+left(@amtstring,len(@amtstring)-1)+'2'
when right(@amtstring,1)='J' then '-'+left(@amtstring,len(@amtstring)-1)+'1'
when right(@amtstring,1)='}' then '-'+left(@amtstring,len(@amtstring)-1)+'0'
when right(@amtstring,1)='{' then '+'+left(@amtstring,len(@amtstring)-1)+'0'
when right(@amtstring,1)='A' then '+'+left(@amtstring,len(@amtstring)-1)+'1'
when right(@amtstring,1)='B' then '+'+left(@amtstring,len(@amtstring)-1)+'2'
when right(@amtstring,1)='C' then '+'+left(@amtstring,len(@amtstring)-1)+'3'
when right(@amtstring,1)='D' then '+'+left(@amtstring,len(@amtstring)-1)+'4'
when right(@amtstring,1)='E' then '+'+left(@amtstring,len(@amtstring)-1)+'5'
when right(@amtstring,1)='F' then '+'+left(@amtstring,len(@amtstring)-1)+'6'
when right(@amtstring,1)='G' then '+'+left(@amtstring,len(@amtstring)-1)+'7'
when right(@amtstring,1)='H' then '+'+left(@amtstring,len(@amtstring)-1)+'8'
when right(@amtstring,1)='I' then '+'+left(@amtstring,len(@amtstring)-1)+'9'
else NULL
end
return cast(@amtdecimal as integer)
end
Code:





_________________________
There are two types of vessels, submarines and targets.

Top
#170071 - 2006-11-05 11:57 PM Re: Mini-Golf
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
oh.
now I get it.

Top
#170072 - 2006-11-06 05:25 PM Re: Mini-Golf
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Jens,

Minigolf is as well with a private phase, no?
Do you announce the 'public' phase then or what ?
_________________________



Top
#170073 - 2006-11-07 04:42 AM Re: Mini-Golf
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Nope, public only, and no price to win. my approach is simplistic but solved my immediate issue I had in SQL. I'm sure, someone can come up with a better implementation using Kixtart.
_________________________
There are two types of vessels, submarines and targets.

Top
#170074 - 2006-11-07 10:31 AM Re: Mini-Golf
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
sure, not even a big deal.
Top
#170075 - 2006-11-07 11:45 AM Re: Mini-Golf
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
something like this you ment?
Code:

function a($)
if $ = 0 + $
$a=right($,1)
$a = "" + abs(left($,~)) + chr(iif($a=0,123+2*(0>$),64+9*(0>$))+$a)
else
$a = asc(right($,1))-64
$a = $a - $a*($a>18)
$ = 0 + (left($,~)+ ($a - 9*($a>9)))
$a = $ - 2*$*($a>9 & $a<>59)
endfunction

a(1234) ?
a("123D") ?
a(-1234) ?
a("123M") ?

get $



ok, everyone, start golfing.
I didn't bother as I need to get back to work.

Top
#170076 - 2006-11-07 07:03 PM Re: Mini-Golf
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hmm...
can't test this but this one should be 2 strokes smaller:
Code:

function a($)
if $ = 0 + $
$a=right($,1)
$a = "" + abs(left($,~)) + chr(iif($a,64+9*(0>$))+$a,123+2*(0>$))
else
$a = asc(right($,1))-64
$a = $a - $a*($a>18)
$ = 0 + (left($,~)+ ($a - 9*($a>9)))
$a = $ - 2*$*($a>9 & $a<>59)
endfunction

a(1234) ?
a("123D") ?
a(-1234) ?
a("123M") ?

get $


Top
#170077 - 2006-11-08 05:23 AM Re: Mini-Golf
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Yeah, something like this :-)
_________________________
There are two types of vessels, submarines and targets.

Top
#170078 - 2006-11-08 09:08 PM Re: Mini-Golf
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
wtf.
I'm the only golfer around?
lame.

Top
#170079 - 2006-11-08 11:12 PM Re: Mini-Golf
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Quote:

wtf.
I'm the only golfer around?
lame.





You're LAME? or the idea that you're the only one coding this round is Lame?
 

Top
#170080 - 2006-11-08 11:14 PM Re: Mini-Golf
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
dunno, both?
Top
#170081 - 2006-11-09 05:25 AM Re: Mini-Golf
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
I thought it's an easy one, even provided a potential approach
_________________________
There are two types of vessels, submarines and targets.

Top
#170082 - 2006-11-09 04:40 PM Re: Mini-Golf
PaulyT Offline
Fresh Scripter
*****

Registered: 2005-08-24
Posts: 8
Loc: Madtown Wisco
Code:

Function a($)
$array = '{', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', '}', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R'
If $ < 0 $a = 10 EndIf
$ = Abs($)
? Left($, Len($) - 1) + $array[$a + Right($, 1)]
EndFunction



Edited by PaulyT (2006-11-09 04:42 PM)

Top
#170083 - 2006-11-09 05:24 PM Re: Mini-Golf
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
You can shorten that array assignment a bit:
Code:
$array = Split('{ A B C D E F G H I } J K L M N O P Q R')


Top
#170084 - 2006-11-09 05:32 PM Re: Mini-Golf
PaulyT Offline
Fresh Scripter
*****

Registered: 2005-08-24
Posts: 8
Loc: Madtown Wisco
Ah, yes, thank you for the Split!
Top
#170085 - 2006-11-09 06:21 PM Re: Mini-Golf
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
PaulyT,
Nice work, don't forget you should dim your vars.
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
Page 1 of 1 1


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 352 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.068 seconds in which 0.021 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org