Page 2 of 2

Re: How to generate an ordinal number

Posted: Saturday 10th July 2021 3:51pm
by BruceSteers
Oops I've messed up with one problem ...

The 11st 😱

Dang and it was so perfect 🤣,
easily fixed with an iif()

So a 2 liner..

Dim ss As String = ["th","st","nd","rd","th"]
Return(Iif(v % 100 = 11, "th", ss[Min(4, v % 10)]))

😊

Re: How to generate an ordinal number

Posted: Saturday 10th July 2021 6:08pm
by grayghost4
It appears that " IIf " is the same as " If ", is that correct ?

I appears that IIf is a carryover form visual basic. But seem to be the same as If in Gambas.

Re: How to generate an ordinal number

Posted: Saturday 10th July 2021 6:34pm
by BruceSteers
No idea, I've always used iif.
it exists in a few languages.

I'm rubbish with gambas 😉

In the wiki iif is written before if so what's the chicken and what's the egg? , it's the same command whatever :)
http://gambaswiki.org/wiki/lang/iif
😉

Re: How to generate an ordinal number

Posted: Saturday 10th July 2021 9:23pm
by BruceSteers
I got home from work and was able to test and the ad-hoc string array does work :)

So my final input on this is this tested and working one liner that works on any number value..
Public Sub OrdinalString(Value As Integer) As String

  Return Str(Value) & ["th", "st", "nd", "rd", "th"][If(Value % 100 = 11, 0, Min(4, Value % 10))]

End
Edit: (not my final input lol)
oops 12 and 13 too :-\
So not a one line after all...
Public Sub nth(Value As Integer) As String

  Select Value % 100
    Case 11, 12, 13
      Return Str(Value) & "th"
  End Select
  Return Str(Value) & ["th", "st", "nd", "rd", "th"][Min(4, Value % 10)]

End
Also note Cogiers version is twice as fast as mine :(

Re: How to generate an ordinal number

Posted: Tuesday 13th July 2021 3:53pm
by grayghost4
  Return Str(Value) & ["th", "st", "nd", "rd", "th"][Min(4, Value % 10)]
I am still learning Gambas ... I have not seen this concept till this thread.

I there anywhere that it is explained, uses and limitation ?
what is it called .... string look up ?

Re: How to generate an ordinal number

Posted: Tuesday 13th July 2021 5:12pm
by cogier
I there anywhere that it is explained, uses and limitation ?
what is it called .... string look up ?
It's just an array created between the square brackets. Consider this code below, both Print lines do exactly the same thing:-
Public Sub Form_Open()

  Dim iLoop As Integer
  Dim sText As String[] = ["th", "st", "nd", "rd", "th"]

  For iLoop = 0 To 4
    Print sText[iLoop]
    Print ["th", "st", "nd", "rd", "th"][iLoop]
  Next

End

Re: How to generate an ordinal number

Posted: Tuesday 13th July 2021 5:35pm
by grayghost4
Thank You ... That is a good explanation so it is just indexing an array :D
I think I understand better now

Edit :

Another question comes to mind, is either method more efficient or faster, to assign the array to a variable or create it on the fly ?

Edit Again :D

I decided to test it my self and creating the array is about twice as fast and the other method
I guess that is because it is only creating the array once as opposed to creating it 4 times

Re: How to generate an ordinal number

Posted: Tuesday 13th July 2021 8:43pm
by BruceSteers
I have a habit of ditching as many single use variables as I can.

It's amazing the way you can use the dot . To pass all sorts of things in one line of text.

If you need to re-use the variable a lot then it makes sense to create one.

The big speed increase in cogiers ordinal method was in the use of Select.
Fewer conditions to meet I think.

Re: How to generate an ordinal number

Posted: Tuesday 13th July 2021 9:21pm
by grayghost4
Bruce :

could you expain :

It's amazing the way you can use the dot . To pass all sorts of things in one line of text.

Re: How to generate an ordinal number

Posted: Tuesday 13th July 2021 9:47pm
by BruceSteers
grayghost4 wrote: ↑Tuesday 13th July 2021 9:21pm Bruce :

could you expain :

It's amazing the way you can use the dot . To pass all sorts of things in one line of text.
Something that springs to.mind is using Image to stretch a Picture...

PictureBox1.Picture = PictureBox1.Picture.Image.Stretch(PictureBox1.Picture.Width / times, PictureBox1.Picture.Height / times).Picture

That could be done using an Image assignment variable pointer but theres no need.
I've bunched all sorts of things up into one liners 🙂