remove last letter in string, if it is a "B", multiply it by 1000.

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures}

remove last letter in string, if it is a “B”, multiply remaining string by 1000.

let me know if there are faster ways to do so.

  1. get last letter

q)m:“8.55B”

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures}

q)v:last m

  1. remove last letter … m will usually be less than 6 chars … i’m sure there is a faster way

q)ssr[m;v;“”]

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures}

“8.55”

q)m key(-1 + count m)

“8.55”

q)reverse 1 _ reverse m 

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures}

“8.55”

q)m _ (-1+count m)

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures}

“8.55”

q)\t do[10000;ssr[m;v;“”]]

73

q)\t do[10000;m key(-1 + count m)]

18

q)\t do[10000;reverse 1 _ reverse m]

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures}

15

q)\t do[10000;m _ (-1+count m)]

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures}

15

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures}
  1. squish the parts together … I had some trouble figuring out how to string it together and also avoid the if.

Basically I want a function that takes a letter, if letter “B” returns 1000, else returns 1.

/try 1

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures} p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures}

m:“8.55B”

d:{x _(-1+count x)} / drop

a:1

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures}

if[“B”~v:last m;a:1000]

(“F”$d m) * a

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures}

/try 2

m:“8.55B”

d:{x _(-1+count x)} / drop

mult:{(1 1000) “B”~x} / simulates if

(“F”$d m) * mult[last m]

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures} p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures} p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures} p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures} p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures} p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff}span.s1 {font-variant-ligatures: no-common-ligatures}

q)-1 _ “ABCD”

“ABC”

1f^1000*“F”$“8.55B”

1f^1000f*“F”$“8.55B”

Content-ID: <9800FD228CBDC544B3563F0ED86FFAB9@GBRP123.PROD.OUTLOOK.COM>

q){$[B=$-1#x;1000;1]*“F”$-1_x}[“8.55B”]

8550f

Thanks,

Caolan

i think you have mixed requirements?

going by “Basically I want a function that takes a letter, if letter “B” returns 1000, else returns 1.”

you could run 

q)m:“8.55B” ; mm:“8.55”

q)1 1000f “B”~last m

1000f

q)1 1000f “B”~last mm

1f

//or use ‘in’

q)1 1000f “B” in m

But your try 1 and try 2 are basically trying to omit the letter at the end and multiply the actual figure by 1 or 1000 based on whether B is existent, is that what you really want? i.e.

q)(“F”$d m) * mult[last m]

8550f

q)(“F”$d mm) * mult[last mm]

8.5

(and NOT just return 1 or 1000)

if it’s the latter then perhaps

q)d:{x!x}[.Q.n,“.”],(enlist[“B”]!enlist"e3")

q)“F”$raze d mm

8.55

q)“F”$raze d m

8550f

or 

{(“F”$ x except “B”)*1 1000f "B"in x}

HTH,

Sean

sorry, misread the question :-)
q)f:{1000 1f*“F”$neg[x:“B”=last x]_x}

q)f"8.55B"

8.55

q)f"8.55"

8550f

You are correct. This is what I am attempting to do.

 

>trying to omit the letter at the end and multiply the actual figure by 1 or 1000 based on whether B is existent, is that what you really want?

q)x:1000#(“1.2B”;“111111.2B”;“3.4”;“3333333.4”)

q)g:{0 1 1000f[count b]*“F”$first b:"B"vs x}

q)g each x

but if you do have lots of strings, (like x), this should be faster:

q)v:{0 1 1000f[count each b]*“F”$first each b:("B"vs)each x}

q)v x

>but if you do have lots of strings, (like x), this should be faster:

i thought the same and that’s why I thought a map would be useful as above

vector conditional seems better again though

q)x:1000#(“1.2B”;“111111.2B”;“3.4”;“3333333.4”)

q)d:{x!x}[.Q.n,“.”],(enlist[“B”]!enlist"e3") ; 

q)se:“F”$raze@'d@

q)se2:{?[“B”=last@'x;1000f*“F”$-1_'x;“F”$x]}

q)ja1:{0 1 1000f[count b]*“F”$first b:"B"vs x} each 

q)ja2:{0 1 1000f[count each b]*“F”$first each b:("B"vs)each x}

//ts them

q)flip (func:timemem),'flip value each (`$f)!“\ts:1000 “,/:(f:string system"f”),:” x"

func| time mem

ja1 | 476  32560

ja2 | 331  120976

se  | 279  176640

se2 | 256  56736

//do they match

q)all ja1~/:(ja2;se;se2)@:x

1b

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff} span.s1 {font-variant-ligatures: no-common-ligatures}

/ anuj

q)m:“8.55B”

q)(“F”$-1_m)*(1 1000 “B”~last m)

8550f

q)\t do[10000;(“F”$-1_m)*(1 1000 “B”~last m)]

32

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff} span.s1 {font-variant-ligatures: no-common-ligatures}

q)se2:{?[“B”=last@'x;1000f*“F”$-1_'x;“F”$x]}

nice. and, with a slightly different selection:

q)ja3:{(1000f*“F”$-1_'x)^“F”$x} /use fill, as in charlie’s example

q)flip (func:timemem),'flip asc value each f!“\ts:1000 “,/:(string f:sese2ja1ja2`ja3),:” x”

func| time mem   

ja3 | 995  52592 

se2 | 1071 52592 

se  | 1339 144368

ja2 | 1530 112736

ja1 | 2308 28448 

Nice, lots of different approaches. You can avoid doing “F” in both conditionals in se2, which helps speed things up a bit:

q) v:{(1 1000 b)*“F”$neg[b:“B”=last each x]_'x}


q) flip (func:timemem),'flip value each (`$f)!“\ts:1000 “,/:(f:string system"f”),:” x"

func| time mem

ja1 | 537 32560

ja2 | 392 120976

ja3 | 279 56736

se | 328 176640

se2 | 298 56736

v | 205 33952


q) 1_differ(`$f)@:x

00000b

I think what you end up with in v is basically the same as what Charlie posted earlier on, but vectorized, and think he had the 1 and 1000 the other way around.

Cheers

Ryan.

another idea is to use 0: and like 

q)a:{@[first(enlist"F";“B”)0:x;where x like"*B";*;1000f]}

or

q)b:{(1 1000f x like"*B")*first(enlist"F";“B”)0:x}

q)\ts:1000 v x

202 33952

q)\ts:1000 a x

108 20736

q)\ts:1000 b x

106 25760

Cheers,

   Attila