Announcement

Collapse
No announcement yet.

MivaScript.com is now LIVE!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    #16
    Re: MivaScript.com is now LIVE!

    Hi Ray,

    Thanks for your message and your kind words. You inspired me to finally write my sorting function correctly, and sorry for the mistakes in my quick reply earlier.

    Here is a tested function to sort arrays of structures by element, that works for numbers (positive and negative sorted correctly, keeping duplicates) and words/letters and numbers. You can choose to get the result in ascending or descending order (l.direction either 'desc' or 'asc'/leaving it blank).

    The beauty of this little function is that it preserves the structure of the array, so it returns an identical structure with all elements only in the new order of the selected element.

    For obvious reasons, it is not as fast as the other sorting algorithms (it can be easily made faster by simplifying it a bit) , but in many cases quite useful. Enjoy!

    You can see the script in action here:
    http://www.sylter-seiten.de/Balance/....mvc?max=10000

    It performs two sorting operations, one by number, one by letters. With the parameter max you can try different sizes of the array, however I limited the script to 12000.

    Note: The drawback of this method is that it only works fast up to approx. 11500 members. After that, Miva really gets problems with the arrays and slows terribly down. For bigger arrays, I'd use a different sorting algorithm which is however much more complicated to write.

    Markus

    here it comes:

    Code:
    <MvCOMMENT>This first part is just to build a random structure (l.array[x]:price and l.array[x]:code) of l.max elements</MvCOMMENT>
    
    <MvASSIGN NAME = "l.al" VALUE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_.;" >
    <MvASSIGN NAME = "l.max" VALUE = "1000" >
    
    <MvASSIGN NAME = "l.a" VALUE = "1" >
    <MvWHILE EXPR = "{ l.a LE l.max }">
    	<MvIF EXPR = "{ NOT fmod(l.a,2) }" >
    	<MvASSIGN NAME = "l.array" INDEX="{l.a}" MEMBER="Price" VALUE = "{(random(10000)$'.'$random(100))*(-1) }" >
    	<MvELSE>
    	<MvASSIGN NAME = "l.array" INDEX="{l.a}" MEMBER="Price" VALUE = "{(random(10000)$'.'$random(100))*1 }" >
    	</MvIF>
    	<MvASSIGN NAME = "l.array" INDEX="{l.a}" MEMBER="CODE" VALUE = "{substring(l.al,random(36),1)$substring(l.al,random(36),1)$substring(l.al,random(36),1)$substring(l.al,random(36),1)$substring(l.al,random(36),1)$substring(l.al,random(36),1)   }" >
    <MvASSIGN NAME = "l.a" VALUE = "{ l.a+1 }" >
    </MvWHILE>
    
    <BR>*************************************
    
    <BR>Sort array by price, descending
    
    <MvASSIGN NAME = "l.new" VALUE = "{ sort_array_by_element( l.array,'price','desc')}" >
    
    <MvASSIGN NAME = "l.a" VALUE = "1" >
    <MvWHILE EXPR = "{ l.new[l.a]:price  }">
    	<BR><MvEVAL EXPR = "{ l.a }" >.- <MvEVAL EXPR = "{ l.new[l.a]:CODE}" > ...... <MvEVAL EXPR = "{ l.new[l.a]:PRICE}" >
    <MvASSIGN NAME = "l.a" VALUE = "{ l.a+1 }" >
    </MvWHILE>
    
    <BR>/////////////////////////////////////////
    <BR>Sort array by code, default order (asc)
    
    <MvASSIGN NAME = "l.new" VALUE = "{ sort_array_by_element( l.array,'code','')}" >
    <MvASSIGN NAME = "l.a" VALUE = "1" >
    <MvWHILE EXPR = "{ l.new[l.a]:code  }">
    	<BR><MvEVAL EXPR = "{ l.a }" >.- <MvEVAL EXPR = "{ l.new[l.a]:CODE}" > ...... <MvEVAL EXPR = "{ l.new[l.a]:PRICE}" >
    <MvASSIGN NAME = "l.a" VALUE = "{ l.a+1 }" >
    </MvWHILE>
    
    
    
    <MvEXIT>
    
    Here comes the actual function:
    
     <MvFUNCTION NAME = "sort_array_by_element" PARAMETERS="array VAR,element,direction" STANDARDOUTPUTLEVEL = "html,text,compresswhitespace"> 
    	<MvASSIGN NAME = "l.max" VALUE = "{ miva_array_max( l.array ) }">
    	<MvASSIGN NAME = "l.pos" VALUE = "{ 1 }">
    
    	<MvWHILE EXPR = "{ l.pos LE l.max }">
    		<MvIF EXPR = "{  miva_variable_value( 'l.array[' $ l.pos $ ']:' $ l.element ) }">
    			<MvIF EXPR = "{ miva_variable_value( 'l.array[' $ l.pos $ ']:' $ l.element ) LT 1}" >
    				<MvCOMMENT>handling negative numbers </MvCOMMENT>
    				<MvASSIGN NAME = "l.v" VALUE = "{ 10000000000000+miva_variable_value( 'l.array[' $ l.pos $ ']:' $ l.element )}" >
       				<MvASSIGN NAME = "l.str" VALUE = "{ ' '$ padl(gettoken(l.v,'.',1),15,'0')$'.'$ padr(gettoken( l.v,'.',2),8,'0')}" >
    			<MvELSEIF EXPR = "{ miva_variable_value( 'l.array[' $ l.pos $ ']:' $ l.element ) GT 0 }" >
    				<MvCOMMENT>handling positive numbers </MvCOMMENT>
       				<MvASSIGN NAME = "l.str" VALUE = "{  padl(gettoken( miva_variable_value( 'l.array[' $ l.pos $ ']:' $ l.element ),'.',1),15,'0')$'.'$ padr(gettoken( miva_variable_value( 'l.array[' $ l.pos $ ']:' $ l.element ),'.',2),8,'0')}" >
    			<MvELSE>
    				<MvCOMMENT>handling letters and  numbers (case insensitive)</MvCOMMENT>
       				<MvASSIGN NAME = "l.str" VALUE = "{  padl(substring( miva_variable_value( 'l.array[' $ l.pos $ ']:' $ l.element ),1,21),21,'0')}" >
    			</MvIF>
    		<MvCOMMENT>build a new sorted index</MvCOMMENT>
    		<MvASSIGN NAME = "{ 'l.index:'$l.str  }" INDEX="{ miva_array_max(miva_variable_value( 'l.index:'$l.str) )+1 }" VALUE = "{ l.pos }" >
    		</MvIF>
    	<MvASSIGN NAME = "l.pos" VALUE = "{ l.pos+1 }">
    	</MvWHILE>
    
    	
    	<MvASSIGN NAME = "l.index" VALUE = "{ miva_array_deserialize(trim(l.index)) }" >
    
    	<MvIF EXPR = "{ l.direction AND NOT ('a' CIN l.direction)}" >
    		<MvCOMMENT><BR><B>DESCENDING</B></MvCOMMENT>
      		<MvASSIGN NAME = "l.a" VALUE = "{ miva_array_max(l.index) }" >
    		<MvWHILE EXPR = "{ l.a GE 1  }">
    			<MvASSIGN NAME = "l.b" VALUE = "{ l.b+1 }" >
    			<MvASSIGN NAME = "l.new" INDEX="{l.b}" VALUE = "{ l.array[l.index[l.a]] }" >
    		<MvASSIGN NAME = "l.a" VALUE = "{ l.a-1 }" >
    		</MvWHILE>
    	<MvELSE>
     		<MvASSIGN NAME = "l.a" VALUE = "1" >
    		<MvWHILE EXPR = "{ l.a LE miva_array_max(l.index)  }">
    		<MvASSIGN NAME = "l.new" INDEX="{l.a}" VALUE = "{ l.array[l.index[l.a]] }" >
    		<MvASSIGN NAME = "l.a" VALUE = "{ l.a+1 }" >
    		</MvWHILE>
    	</MvIF>
    
    <MvFUNCRETURN VALUE="{ l.new }">
    </MvFUNCTION>
    Emerald Media, Trade & Technology USA/Germany
    Professional Miva Programming since 1998
    Home of the Emerald Objects Application Server (EOA)
    Multi-dimensional CRM, CMS & E-Commerce

    http://www.emeraldobjects.com
    http://www.sylter-seiten.com

    Comment


      #17
      Re: MivaScript.com is now LIVE!

      You guys - incl Roy - rule! Thank you all.
      Susan Petracco
      NetBlazon

      1.866.400.2444

      _____________________________________________

      Like us on Facebook

      Comment


        #18
        Re: MivaScript.com is now LIVE!

        Great to see this resource available. Hopefully it will encourage more Mivascripters.

        I'm not sure whether the format has changed, but if it would be useful to have documentation on writing third party function libraries.

        One of the strengths of other languages like PHP or Python is the support for extensions.

        A good example where documentation for function libraries would be useful is for additional database support.

        I'm not a C/C++ programmer myself but there have been times I've considered checking out what would be involved to get someone to write libraries for other databases. Currently I use the obsolete unsupported 5.06 ODBC library, and for the most part that works fine, but if at some future point it becomes incompatible with the latest version of Empresa (The 64 bit version of Empresa is a classic case and point) there doesn't seem to be any documentation I can refer someone to.
        Christopher Cookson
        Create IT Powered by Webpression CMS

        Comment


          #19
          Re: MivaScript.com is now LIVE!

          Marcus

          Good work on the sort. Initial testing shows it to be about 50% faster than my implementation of Scotts Miva_Array_Sort() but still far slower the your original Qsort().

          I have a few suggestions to make it faster.

          First change the parameters. sort_array_by_element( N, 'D', source_array VAR, return_array VAR)

          Where if N is a number, this is a numeric sort, (for integers you would use 5). For text this would be 0 or null. This would let you designate the size of the padL() to the left of the decimal. Also padR() is not needed to the right of the decimal. Shorter padded numbers will sort faster.

          The function should return the number of elements in the array and return the array by reference in l.return_array but remember to clear l.return_array = "" at the start of the function. Returning it a parameter, (return_array VAR) removes the operation of stuffing a large array into the return stack, then assigning it to a variable on return.

          Instead of one MvWHILE loop with several conditionals inside the loop, use conditionals on the outside the loop to determine how the data needs to be formatted, then create separate loops for each datatype. That way conditionals are only executed once and no conditionals will be executed inside the loop.

          Instead of making a copy of the array in sorted order MvREFERENCE it.

          <MvASSIGN NAME = "l.new" INDEX="{l.b}" VALUE = "{ l.array[l.index[l.a]] }" >
          <MvREFERENCE NAME = "{ 'l.new[' $ l.b $ ']' }" VARIABLE = "{ 'l.array[l.index[' $ l.a $ ']]' }">

          The assign doubles the amount of memory used, the amount of time each assign takes is directly proportional to the amount of data being copied. Reference only creates pointers to the data, consuming less memory and each reference takes the same amount of time to execute. For small data sets, the difference will be small but for more complex data sets, the savings will be significant.

          Ray
          Ray Yates
          "If I have seen further, it is by standing on the shoulders of giants."
          --- Sir Isaac Newton

          Comment


            #20
            Re: MivaScript.com is now LIVE!

            Originally posted by RayYates View Post
            Does anyone have a colorize script for MivaScript they would like to contribute?
            Define "colorize". I have a color script, which you give base foreground and background colors, and it will return an array with RGB values for creating lighter and darker shades. Making sure that the foreground is always readable.

            I can't take credit for it though. It was a PHP script that someone published, I just converted it to MivaScript.

            I love making functions like this; just wish there was a market for functions and not whole applications, ya know?

            Comment


              #21
              Re: MivaScript.com is now LIVE!

              I'd like the Miva Script to display colorized like the PHP site.
              I have Textpad configured to colorize it and the html. It's far more readable.

              I could use my syntax definition file and write one but somewhere I remember seeing MivaScript displayed colorized like this.

              Ray Yates
              "If I have seen further, it is by standing on the shoulders of giants."
              --- Sir Isaac Newton

              Comment


                #22
                Re: MivaScript.com is now LIVE!

                +1 for Textpad. When I found that someone made a MivaScript syntax file already I was shocked. Here's a direct link to the syntax file if it helps: http://www.textpad.com/add-ons/files/syntax/miva.zip

                (Function names are probably outdated)

                Comment


                  #23
                  Re: MivaScript.com is now LIVE!

                  Hi

                  Are you looking for a syntax color for your personal editor, or for the web? If it's for the web, why not using existing javascript libraries available (like google for ex: http://code.google.com/p/google-code-prettify/

                  PS: Is it me or this post has been hijacked :-)

                  Cheers,
                  Claudiu
                  Zen Radio : Relax :) : www.zenradio.fm
                  MivaScript Tutorials & Scripts : www.mivascript.org

                  Comment


                    #24
                    Re: MivaScript.com is now LIVE!

                    To clarify, I'd like the Miva Script on MivaScript.com to display colorized like the PHP.net web site and like the Textpad example above.

                    As for Hijacked? No... But it does appear to be circling the airport. < grin >

                    As for Textpad, at least one of the syntax definition files on the site is one I created a while back. It does not reflect the latest changes to the language.
                    Ray Yates
                    "If I have seen further, it is by standing on the shoulders of giants."
                    --- Sir Isaac Newton

                    Comment


                      #25
                      Re: MivaScript.com is now LIVE!

                      Originally posted by RayYates View Post
                      To clarify, I'd like the Miva Script on MivaScript.com to display colorized like the PHP.net web site and like the Textpad example above.

                      As for Hijacked? No... But it does appear to be circling the airport. < grin >

                      As for Textpad, at least one of the syntax definition files on the site is one I created a while back. It does not reflect the latest changes to the language.
                      Any chance a MM5.5 syntax definition file exists for Dreamweaver (CS3)?
                      Last edited by William Davis; 02-10-11, 09:13 AM.
                      Thank you, Bill Davis

                      Comment


                        #26
                        Re: MivaScript.com is now LIVE!

                        ....ah, not likely as Mivascript is almost always written in a text editor and then compiled. If you are referring to SMT, it would probably be easy to create one as there are not many "tags".
                        Bruce Golub
                        Phosphor Media - "Your Success is our Business"

                        Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
                        phosphormedia.com

                        Comment


                          #27
                          Re: MivaScript.com is now LIVE! - Sorting arrays/structures by element

                          Hi Ray,

                          Sorry that I only get back to you now and thanks for your feedback, very appreciated. Some comments to your suggestions inline:

                          Marcus


                          Originally posted by RayYates View Post
                          First change the parameters. sort_array_by_element( N, 'D', source_array VAR, return_array VAR)

                          Where if N is a number, this is a numeric sort, (for integers you would use 5). For text this would be 0 or null. This would let you designate the size of the padL() to the left of the decimal. Also padR() is not needed to the right of the decimal. Shorter padded numbers will sort faster.
                          I see your point, however I try to keep interfaces as simple as possible - and with this the number of (required) parameters as small as needed. Old habbit of a lazy programmer who hates to look up APIs...

                          Originally posted by RayYates View Post
                          The function should return the number of elements in the array and return the array by reference in l.return_array but remember to clear l.return_array = "" at the start of the function. Returning it a parameter, (return_array VAR) removes the operation of stuffing a large array into the return stack, then assigning it to a variable on return.
                          Yes, definitely.

                          Originally posted by RayYates View Post
                          Instead of one MvWHILE loop with several conditionals inside the loop, use conditionals on the outside the loop to determine how the data needs to be formatted, then create separate loops for each datatype. That way conditionals are only executed once and no conditionals will be executed inside the loop.
                          Here again, that is certainly a better way IF you know in advance the format of each record within the structure. In other words: If all values are integers or decimals, the entire sorting algorithm could be simplified just as it could be much faster they were all strings. Here again this is basically a question of choice for each application.

                          Originally posted by RayYates View Post
                          Instead of making a copy of the array in sorted order MvREFERENCE it.

                          <MvASSIGN NAME = "l.new" INDEX="{l.b}" VALUE = "{ l.array[l.index[l.a]] }" >
                          <MvREFERENCE NAME = "{ 'l.new[' $ l.b $ ']' }" VARIABLE = "{ 'l.array[l.index[' $ l.a $ ']]' }">

                          The assign doubles the amount of memory used, the amount of time each assign takes is directly proportional to the amount of data being copied. Reference only creates pointers to the data, consuming less memory and each reference takes the same amount of time to execute. For small data sets, the difference will be small but for more complex data sets, the savings will be significant.
                          YES! YES! YES! I use MvREFERENCE quite often now, but I admit I was a bit lazy to test it here. MvReference has the disadvantage that the referenced variable could change somewhere in the programflow (or even get lost in case of a local variable) and introduce a difficult to track down bug.
                          Emerald Media, Trade & Technology USA/Germany
                          Professional Miva Programming since 1998
                          Home of the Emerald Objects Application Server (EOA)
                          Multi-dimensional CRM, CMS & E-Commerce

                          http://www.emeraldobjects.com
                          http://www.sylter-seiten.com

                          Comment


                            #28
                            Re: MivaScript.com is now LIVE!

                            Marcus

                            In this example: sort_array_by_element( N, 'D', source_array VAR, return_array VAR)

                            If N is a number, you know your are sorting a numeric array, if its l.null or '' you know you are sorting strings.

                            As for MvREFERENCE, in general if you are sorting an array, the intension is to display it, or search it, so it's not a problem, however, if someone changed the value of the field that had been sorted on, of course it is no longer in order.

                            Also if you reset l.new you are also clearing l.array.
                            <MvASSIGN NAME = "l.new" VALUE = "" > destroys both arrays.

                            In your routine you would have to re-set the Reference l.new before you could use it.
                            <MvREFERENCE NAME = "l.new" VARIABLE = "{ 'l.null' }">
                            Last edited by RayYates; 02-11-11, 06:18 AM.
                            Ray Yates
                            "If I have seen further, it is by standing on the shoulders of giants."
                            --- Sir Isaac Newton

                            Comment


                              #29
                              Re: MivaScript.com is now LIVE!

                              I have been following this thread very closely -interesting... I sure wish I could understand some of it ;)
                              Thank you, Bill Davis

                              Comment


                                #30
                                Re: MivaScript.com is now LIVE!

                                I added an annotation to the new indexof() function.

                                http://www.mivascript.com/item/indexof.html
                                Ray Yates
                                "If I have seen further, it is by standing on the shoulders of giants."
                                --- Sir Isaac Newton

                                Comment

                                Working...
                                X