Miva, Miva Script, Miva Empresa, Miva Mia amd Miva Merchant are registered trademarks of the Miva Corporation
 
Ivo Truxa - truXoft control systems: advanced programming and custom IT solutions home / about / webdesign / Miva / automation / contact

http://mivo.truxoft.com
MIVO!
miva beyond limits

 

MIVA®  SNIPPETS:  One-Liners

from Ivo Truxa, Sep/2000

  1. Decimal to 1/16th fraction conversion
  2. Reversing a string
  3. Checking if a client accepts cookies
  4. Displaying the page loading time
  5. 24/12 hours time conversion
  6. Hex/Dec convertions
  7. Converting escaped hex values into ASCII characters
  8. Removing Quoted-Printable encoding
  9. Extracting a number from a string
  10. Date verification
  11. Number of instances of a character/substring in a string
  12. Counting the number of tokens in a string
  13. Checking if a string contains a character from a set
  14. Checking multiple 'EQ' conditions
  15. User Comments

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.


top


Decimal to 1/16th Fraction Conversion

My 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!

<MvEVAL EXPR="{int(rnd(16*dec,0)/16) $ ' ' $ gettoken(
 '1/16|1/8|3/16|1/4|5/16|3/8|7/16|1/2|9/16|5/8|11/16|3/4|13/16|7/8|15/16',
 '|',rnd(16*dec,0) MOD 16)}">

(should be in one line :)

I recommend putting it into a function:

<MvFUNCTION NAME="dec2frac" PARAMETERS="dec">
 <MvFUNCRETURN VALUE="{int(rnd(16*l.dec,0)/16) $ ' ' $ gettoken(
  '1/16|1/8|3/16|1/4|5/16|3/8|7/16|1/2|9/16|5/8|11/16|3/4|13/16|7/8|15/16',
  '|',rnd(16*l.dec,0) MOD 16)}">
</MvFUNCTION>

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.

posted to the Miva-Script-Users-List on Jun 21 2002


top


Reversing A String

Do you need to print a string backwards? Well, this is not a one-liner, but I am putting it here anyway:

<MvWHILE EXPR="{len(A) NE len(B)}">
 <MvASSIGN NAME="B" VALUE="{B $ substring(A,len(A)-len(B),1)}">
</MvWHILE>

A is the original string, B is the reversed one.

posted to the Miva-Script-Users-List on Dec 17 2001


top


Checking If A Client Accepts Cookies

With the following code you can quickly find out if the visitor's browser has accepted the Miva generated cookie:

<MvIF EXPR="{s.callerid IN s.http_cookie}">OK</MvIF>

posted to the Miva-Merchant-Coders-List on Nov 27 2001


top


Displaying Page Loading Time

Do 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:

<MvEVAL EXPR="{s.dyn_time_t-s.time_t+(s.dyn_tm_usec-s.tm_usec)/1000000}">

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.

posted to the Miva-Merchant-Users-List on Jul 24 2001


top


24/12 Hours Time Conversion

Do 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:

<MvEVAL EXPR="{hr MOD 12 + 12*(NOT(hr MOD 12)) $':'$ padl(min,2,0) $ asciichar(65+15*(hr GE 12))$'M'}">

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.

posted to the Miva-Script-Users-List on Jul 24 2001
wrong results of 00:XX AM/PM instead of 12:XX AM/PM
fixed thanks to Adam Denning on 03/25/2003


top


Hex/Dec Convertions

Sometimes 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 Conversion

This 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.

<MvEVAL EXPR="{asciivalue(decodeattribute('%' $ padl(g.hex,2,'0')))}">

Dec to Hex Conversion

This 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.

<MvEVAL EXPR="{glosub(encodeattribute(asciichar(g.dec)),'%','')}">

top


Converting escaped hex values into ASCII characters

This 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 &copy;). 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: &#x41; (character A with ASCII value hex 0x41). In addition to glosubbing the &#x with %, you would also need to remove the trailing semicolon.

<MvEVAL EXPR="{decodeattribute(glosub(text,'\\','%'))}">

posted to the Miva-Script-Users-List on Nov 13 2000


top


Removing Quoted-Printable Encoding

In the same way as above, you can remove Quoted-Printable encoding (e.g. in e-mail messages read by MvPOP)

<MvEVAL EXPR="{decodeattribute(glosub(text,'=','%'))}">

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.

posted to the Miva-Script-Users-List on Dec 8 2001


top


Extracting A Number From A String

This expression extracts the first number from any string, regardless on its position:

<MvEVAL EXPR="{1*glosub(string,gettoken(string,'0123456789',1),'')}">

Hmm, you could possibly also use the FMT operator instead.

posted to the Miva-Script-Users-List on Nov 11 2000


top


Date Verification

<MvEVAL EXPR="{time_t_month(mktime_t(year,month,day,12,0,0,0),0) EQ month}">

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

dd/mm/yyyy      

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.

posted to the Miva-Script-Users-List on Sep 11 2000.


top


Getting Number of Instances of a Character in a String

<MvEVAL EXPR="{len(string)-len(glosub(string,char,''))}">

... and Number of Instances of a Substring in a String

<MvEVAL EXPR="{(len(string)-len(glosub(string,sub,'')))/len(sub)}">

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.

both posted to the Miva-Script-Users-List on Sep 4 2000


top


Counting the Number of Tokens in a String

<MvEVAL EXPR="{1+(len(string)-len(glosub(string,separator,'')))/len(separator)}">

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

posted to the Miva-Script-Users-List on March 13 2000


top


Checking if a String Contains a Character From a Set

<MvEVAL EXPR="{string EQ gettoken(string,setOfChars,1)}">

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

posted to the Miva-Script-Users-List on Aug 10 2000


top


Checking Multiple 'EQ' Conditions

<MvEVAL EXPR="{(var $ ' ') CIN 'val1 val2 val3 val4 val5 '}">

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.
This one-liner can be used instead of multiple EQ conditions or even multiple or nested IF tags as often seen in applications.

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:

<MvEVAL EXPR="{('|' $ var $ '|') CIN '|val1|val2|val3|val4|val5|'}">

last posted to the Miva-Script-Users-List on Aug 10 2000



top

   

Miva and some other terms used on this page are registerd trademarks of the Miva Corporation
copyright  truXoft  © 1997-2008