# Thread: a combination function alltrim(str())

1. ## a combination function alltrim(str())

I imagine a UDF would cover this but I can't grasp how to write it at this moment.

I want a base function in A5 that combines alltrim() and str() which I would virtually always use instread of str(). I almost always forget about the default variable lengths, and therefore the leading/trailing blanks, which causes me problems.

2. ## Re: a combination function alltrim(str())

Code:
```FUNCTION allstr AS C (num AS N )
allstr = alltrim(str(num))
END FUNCTION```
Code:
```? ALLSTR(4654445)
= "4654445"```
Doesn't work for numbers over ten digits or decimals but it'll get you started.

Maybe better....

Code:
```FUNCTION allstr AS C (num AS N, dec as N )
if mod(num,1)<>0 'has decimals
allstr = alltrim(str(num,15,dec))
else 'integer
allstr = alltrim(str(num,15,dec))
end if
END FUNCTION```

3. ## Re: a combination function alltrim(str())

Thanks Stan,

I'm trying to develop reciprocals similar to dtoc() and ctod() for dow() and make one for month of year also.

4. ## Re: a combination function alltrim(str())

Originally Posted by Mike Wilson
I'm trying to develop reciprocals similar to dtoc() and ctod() for dow()
Like cdow() ??

No you probably mean to get a return value of "4" for April?

5. ## Re: a combination function alltrim(str())

Originally Posted by Mike Wilson
I want a base function in A5 that combines alltrim() and str() which I would virtually always use instread of str().
This has been proposed long ago:

I created strim() and strimpad() for myself and I use them very frequently.
Code:
```'Date Created: 12-Apr-2005 04:21:23 PM
'Last Updated: 18-Apr-2005 10:02:30 AM
'Created By  : Steve Andrews
'Updated By  : Steve Andrews
FUNCTION strim AS C (nValue AS N )
'DESCRIPTION: This is the equivalent of alltrim(str(nValue)).
strim=alltrim(str(nValue))
END FUNCTION```
Code:
```'Date Created: 12-Apr-2005 04:19:58 PM
'Last Updated: 10-May-2005 02:34:23 PM
'Created By  : Steve Andrews
'Updated By  : Steve Andrews
FUNCTION strimpad AS C (nValue AS N, nLen = 8, cFill = "0")
'DESCRIPTION: This is the equivalent of padl(alltrim(str(nValue)),nLen,cFill) where nLen defaults to 8 and cFill defaults to "0".
END FUNCTION```

6. ## Re: a combination function alltrim(str())

Alpha has automatic conversion to character when using the concatenation character '+' with another character value. So I regularly use "" + NumValue as in
Code:
```NumValue = 12
Strg = "" + NumValue
?Strg
="12"```

7. ## Re: a combination function alltrim(str())

Stan,
Yes,

Code:
```? cmofd(date())   ' this exists as  cmonth()
= "March"

? nmofd(date())              ' this exists as month()
= 3

? nmtocm(4)
= "April"

? cmtonm("April")
= 4```
Steve,
Thanks for the examples. I have a host of them to make now.

Tim,
Nice! I did not know that. Pretty simple, thanks.

8. ## Re: a combination function alltrim(str())

Hi Mike,

Originally Posted by Mike Wilson
Thanks Stan,

I'm trying to develop reciprocals similar to dtoc() and ctod() for dow() and make one for month of year also.
Do you mean effectively these built in functions?
?MONTH_NUMBER("april")
= 4
?DAY_NUMBER("Monday")
= 2
To convert a dow number to a string
cdow=word("Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday",dow_num,",")
Similarly, to convert a month number to a string
cmonth=word("January,February,March,April,May,June,July,August,September,October,November,December",month_num,",")
I'd argue that use of the above is seldom ever really needed, as your source and target are almost always complete dates. If that is the case, you could use the built_in function to reverse those values
Date_Value as D = DATE_VALUE( Year as N, Month as N, Day as N )

9. ## Re: a combination function alltrim(str())

Originally Posted by Steve Andrews
I created strim() and strimpad() for myself and I use them very frequently.
Steve (and anyone else doing things like this), be careful using simple function names like "strim" or even "strimpad". This may get you into the same trap I've run into a couple times in the past. I developed my own function then Alpha came up with their own function with the same name in a latter version of A5. This can certainly result in confusion and, in some cases, errors.

This is why any "generic" function I create like this that could possibly be used by Alpha is named something like AIMS_xxxx. (My company name is AIMS DataCom.)

Even something as simple as, for example, adding a "9" on the end would keep it unique - e.g., strim9().

10. ## Re: a combination function alltrim(str())

Hi Cal,

Originally Posted by CALocklin
This is why any "generic" function I create like this that could possibly be used by Alpha is named something like AIMS_xxxx. (My company name is AIMS DataCom.)

Even something as simple as, for example, adding a "9" on the end would keep it unique - e.g., strim9().
This is a good warning, but adding a number at the end can create issues as well. Just take a look at some of the built-in functions ending in numbers, just a few such as ;

• a5_toolbar_editor_V6()
• md5()
• convert_a4_to_a5()
• a5_html_editor3()
• ui_get_list2()

11. ## Re: a combination function alltrim(str())

Yeah. That's why I picked "9". I don't think that one is used anywhere - yet. The simpler the "addition" to the name, the greater the chance for duplication.

12. ## Re: a combination function alltrim(str())

Cal,

Thanks for pointing that out. It's painful to think of how many field rules, filters, calculated fields, etc. I would have to change if they ever put a function named strim() into a future version.

I have many more UDF's which are more dangerous along these lines. One is Get_Name() and another is Get_Title(). Thanks again.

Steve

13. ## Re: a combination function alltrim(str())

Ira,
Thanks for your imput. When all is said-and-done, meaning when I have hopefully absorbed all the documentation and others imput (likely incomplete even still) the only function missing is the "converts the month integer value to a month character name". It might be right in front of my nose but I am missing it.

4-->"APRIL"

What I found very cool, yet not in the docs, which I found out by trial and error is this:

Code:
```? month_number("jan")
= 1```
The docs say nothing about the 3 letter abbreviation being equal to the full month name, yet the function does this. Which is in good part what I needed which started off this whole thread.

So for the "converts the month integer value to a month character name" I came up with this:

Code:
```FUNCTION mn_to_mc AS C (num as N)

dim vcM as C
for i = 1 to 12
vcM = cmonth(ctod(str(i)+"/01/2008"))
if num = i
mn_to_mc = vcM
end
end if
next

END FUNCTION```
I am not concise, and I miss alot.. but this one is working for me.

Cal,
Thank you, thank you, thank you on the function naming tip. Yes you are keen to naming and nomenclature. Great tip. I'm wondering of your thoughts on my choice above: mn_to_mc() = MonthNumeic_To_MonthCharacter? Chances A5 will use this within their propietary? Maybe so, so then John Michael Wilson might use mn_to_mc_jmw().

OK.. thanks!! Great stuff!

14. ## Re: a combination function alltrim(str())

Hi Mike,

Hellooooo.....Is this thing on?!?:D

Did you read my post above with month_number() and day_number() as well as the reverse code (that could be placed in a function)?

month_number() only looks at the 1st 3 characters and day_number() only the 1st 2 characters of the day of week.

15. ## Re: a combination function alltrim(str())

Originally Posted by Mike Wilson
It might be right in front of my nose but I am missing it.
Originally Posted by csda1
Similarly, to convert a month number to a string
[INDENT] cmonth=word("January,February,March,April,May,June,July,August,September,October,November,December",month_num,",")
Hey Ira,
See what I mean.... right in front of my nose!:o
So which is faster.... your word() method or my for-next method. I'm betting word(). Your the master of speed... the answer behind door number 3......?
Word_number_get() well explains/exemplifies the functions capabilities to use partials of the word. Month_number() does not.
month_number() only looks at the 1st 3 characters and day_number() only the 1st 2 characters of the day of week.
From where does this come from.... your trial and error experience? Or do you have a special docs version not available to us small guys?

So is there a reciprocal of Word_number_get() where the number produces the value of the number position in a list. I swear I have seen this, but I can't see it now.

So, at the end of the day... if the docs had a hyperlink or something to the reciprocal of a function.......how cool would that be? ( it would be for me.... I'm a pretty simpleton guy!).

16. ## Re: a combination function alltrim(str())

Hi Mike,
Originally Posted by Mike Wilson

So is there a reciprocal of Word_number_get() where the number produces the value of the number position in a list. I swear I have seen this, but I can't see it now.
Isn't that what Word() does?
WORD() returns one or more words, specified by the Word_Number and Word_Count and delimited by Word_Delimiter, from the supplied Input_String.

17. ## Re: a combination function alltrim(str())

Oh.... yes.:o Clearly, a level of cognition ended somewhere before 2AM. I have, however, decided to generate a list of reciprocal functions, to assist me as my Alzheimer's creeps in.

18. ## Re: a combination function alltrim(str())

Originally Posted by Mike Wilson
From where does this come from.... your trial and error experience? Or do you have a special docs version not available to us small guys?
.....
So, at the end of the day... if the docs had a hyperlink or something to the reciprocal of a function.......how cool would that be? ( it would be for me.... I'm a pretty simpleton guy!).

http://support.alphasoftware.com/alphafivehelpv8/ version 8 help
http://support.alphasoftware.com/alphafivehelpv9/ version 9 help

Under the Help's search tab, search for Month_Number(), Day_number(). They are both there. Search Date, go to Date and Time functions and you'll see a full list of functions

Originally Posted by Mike Wilson
So which is faster.... your word() method or my for-next method. I'm betting word(). Your the master of speed... the answer behind door number 3......?
Any time you have a loop, you want to try to eliminate them, including any used by any functions (built-in or otherwise) it may call. How do you know which internal functions use them? By measuring their speed with some utility (Do you know of any?:)) or timing code.

In your case, the operative part of your code doesn't require a loop and could have been
?cmonth(ctod(str(num)+"/01/2008"))
which takes .00039 seconds. (Your looped version took .00139 seconds.) Compare to my version below which takes .00036 seconds.
?word("January,February,March,April,May,June,July,August,September,October,November,December",num,",")

Originally Posted by Mike Wilson
Word_number_get() well explains/exemplifies the functions capabilities to use partials of the word. Month_number() does not.
I believe there are oddities with this function if not using CRLF() as a delimiter. I wrote my own equivalent, that runs a bit faster anyway. For most cases, use wordat() or wordatc().

Originally Posted by Mike Wilson
So is there a reciprocal of Word_number_get() where the number produces the value of the number position in a list. I swear I have seen this, but I can't see it now.
See word() function

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•