MIVA® SNIPPETS: One-Liners
I like brief and dense code and therefore I always try to find efficient algorithms or to use functions that make my code shorter, easier to read, modular and often faster too. Sometimes I find a surprising feature of a function that can be used for another tasks than originally designed. For the moment just few examples. Some of them are evident for any experienced programmer and are nothing new. Others may be more tricky. Myself, I often find nice tricks on the Miva-Script-Users-List or in the source code of other programmers. If I publish such a one-liner, not coming from my own kitchen, I put the credit to the author, of course too. Decimal to 1/16th Fraction ConversionMy advice: instead of using such weird conversions, write to your Congress representant and ask him/her to finally force metric system instead of the imperial one, in the USA! You are the last in the world who still uses it. Even the conservative United Kingdom has abandoned it decades ago!
(should be in one line :) I recommend putting it into a function:
Is rounds to whole 1/16th's. If you need forcing rounding down or up to full 1/16th's, use int( ), resp. ceil( ) instead of the rnd( ) function. Reversing A StringDo you need to print a string backwards? Well, this is not a one-liner, but I am putting it here anyway:
Checking If A Client Accepts CookiesWith the following code you can quickly find out if the visitor's browser has accepted the Miva generated cookie:
Displaying Page Loading TimeDo you need to know how long a Miva driven page needs to load? Place the following code at the end of the page, just informt of the </HTML> tag:
You may place the result into a tag or a comment if you do not want to have it displayed on the page, but rather have it 'secretly' in the source code. Even better, you may export it into a flat file or a database. NOTE: due to a bug in Windows Miva engine versions (Mia and NT Empresa), the time variables return sometimes unexpected results. Unix Empresa works fine. 24/12 Hours Time ConversionDo you need to convert 24 hours European style of time to 12 hours one (eg. 15:07 to 3:07 PM)? Use this one-liner:
Replace the hr variable containing the hour (1-24) with time_t_hour(tm,0) and the min with time_t_min(tm,0) when you have the time stored in a variable in the time_t format. Add "$':'$ padl(sec,2,0)" resp. "$':'$ time_t_sec(tm,0)" if you want to display the seconds too. Hex/Dec ConvertionsSometimes it may be handy to have a simple function for converting hexadecimal values into decimal ones and vice versa. The following one-liners show a possible way to go. Hex to Dec ConversionThis function will convert 1-2 digit decimal numbers into their hexadecimal equivalents. Numbers above 7F will be converted as negative numbers. If you need a positive number you have to add some more code.
Dec to Hex ConversionThis function converts (with limitations) decimals into hexadecimal values. It accept values up to 255 (FF hex) and it will not show most of values below 128 (80 hex). Again, for better function, you would need to add some more coding.
Converting escaped hex values into ASCII charactersThis code was posted to the Miva-Script-Users-List in direct response to a demand for a function converting escaped hexadecimal ASCII values into their printable ASCII equivalents. It means, for example, converting the \A9 into © (not into ©). This one-liner uses the ability of the decodeattribute() function to convert attribute encoded characters into ASCII. And because attribute encoded characters are hex values prepended with the per cent sign (%), it is just necessary to convert the backslashes into per-cent signs with glosub(). The same method could be used for converting characters encoded in the HTML-entities format: A (character A with ASCII value hex 0x41). In addition to glosubbing the &#x with %, you would also need to remove the trailing semicolon.
Removing Quoted-Printable EncodingIn the same way as above, you can remove Quoted-Printable encoding (e.g. in e-mail messages read by MvPOP)
Well, you may need to make little bit more code around. For example you should first replace any previously present %-signs with another character or a unique string, and put it back afterwards. However, I think the one-liner is already a good basement. You can also use the MimeConv 2.0 - it handles not only base64, but also the quoted-printable encoding. Extracting A Number From A StringThis expression extracts the first number from any string, regardless on its position:
Hmm, you could possibly also use the FMT operator instead. Date Verification
Well, it is a little bit longer one, but it works fine. It picks up invalid dates, including leap years. It verifies any date after 1/1/1970 and on Miva Mia 3.72 it validates till the year 9999. Trying with Miva Empresa 3.72 on PC Linux it validates just up to 18/1/2038 but it can differ on other OS or Miva versions (the next Y2K bug). For dates before 1970 and after 2038 you could use 1972+(year MOD 4) instead of plain year. For correct handling of special transient years (like 1900, 1800,...) you would probably need to make more coding. Getting Number of Instances of a Character in a String
... and Number of Instances of a Substring in a String
Instead of looping through a string, using MvWHILE and substring() you can take advantage of the side-effect of the glosub() function. Glosub removes all instances of a substring (or a character) from a string and therefore the difference in the lenght before and after glosub() bring us to the result. Counting the Number of Tokens in a String
You can avoid looping through a string with MvWHILE and gettoken() to count the number of (different) tokens in a string if you use the above function - just count the separators instead. Separator may be a string or character (remove the division in that case). Checking if a String Contains a Character From a Set
Instead of multiple (char IN/CIN string) as often done, you can simply use the side-effect of gettoken(). Gettoken breaks a string in tokens each time it finds a character from the set of characters (spearators). If there is no such character (no separator), the first token is equal to the string Checking Multiple 'EQ' Conditions
Please note the very important blank appended to the variable (not to match substrings/subvalues)! However, it is not neccessary if you compare just a single character values. You can add as many values as needed. You can verify as well strings, characters as numeric values. In some cases you may need to remove leadning and trailing spaces (or 0's in case of numeric values) from the variable for better matches. Use IN instead of CIN for case matching. To avoid partial matches stricter, append a separator character to the searched value from both sides - you have to do it at the value list, of course, too. If the value may contain a space, you have to use another separator:
|
|
Miva and some other terms used on this page are registerd trademarks of the Miva Corporation |