How to generate an ordinal number

Post your Gambas programming questions here.
User avatar
BruceSteers
Posts: 652
Joined: Thursday 23rd July 2020 5:20pm

Re: How to generate an ordinal number

Post by BruceSteers » Saturday 10th July 2021 3:51pm

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)]))

๐Ÿ˜Š
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
grayghost4
Posts: 119
Joined: Wednesday 05th December 2018 5:00am
Location: Marengo, illinois usa

Re: How to generate an ordinal number

Post by grayghost4 » Saturday 10th July 2021 6:08pm

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.

User avatar
BruceSteers
Posts: 652
Joined: Thursday 23rd July 2020 5:20pm

Re: How to generate an ordinal number

Post by BruceSteers » Saturday 10th July 2021 6:34pm

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
๐Ÿ˜‰
Last edited by BruceSteers on Saturday 10th July 2021 9:27pm, edited 1 time in total.
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
BruceSteers
Posts: 652
Joined: Thursday 23rd July 2020 5:20pm

Re: How to generate an ordinal number

Post by BruceSteers » Saturday 10th July 2021 9:23pm

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 :(
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
grayghost4
Posts: 119
Joined: Wednesday 05th December 2018 5:00am
Location: Marengo, illinois usa

Re: How to generate an ordinal number

Post by grayghost4 » Tuesday 13th July 2021 3:53pm

  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 ?

User avatar
cogier
Site Admin
Posts: 756
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: How to generate an ordinal number

Post by cogier » Tuesday 13th July 2021 5:12pm

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

User avatar
grayghost4
Posts: 119
Joined: Wednesday 05th December 2018 5:00am
Location: Marengo, illinois usa

Re: How to generate an ordinal number

Post by grayghost4 » Tuesday 13th July 2021 5:35pm

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

User avatar
BruceSteers
Posts: 652
Joined: Thursday 23rd July 2020 5:20pm

Re: How to generate an ordinal number

Post by BruceSteers » Tuesday 13th July 2021 8:43pm

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.
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
grayghost4
Posts: 119
Joined: Wednesday 05th December 2018 5:00am
Location: Marengo, illinois usa

Re: How to generate an ordinal number

Post by grayghost4 » 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.

User avatar
BruceSteers
Posts: 652
Joined: Thursday 23rd July 2020 5:20pm

Re: How to generate an ordinal number

Post by BruceSteers » Tuesday 13th July 2021 9:47pm

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 ๐Ÿ™‚
Wishing well
Bruce

If at first you don't succeed , try it differently.

Post Reply