Announcement

Collapse
No announcement yet.

Variable Value For miva_array_search Offset?

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

    Variable Value For miva_array_search Offset?

    Is there a way to use a variable value for the miva_array_search offset

    <mvt:while expr="miva_array_search(l.settings:thisArray, l.cnt, l.settings:selectName, 'tolower(g.thisVar) IN tolower(l.settings:selectName:Name)')">

    doesn't work, nether does 'l.cnt', "l.cnt" and that's all I can think of.
    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

    #2
    Re: Variable Value For miva_array_search Offset?

    I don't think you can use "simple" local variables like l.cnt in template code. I'd try a global variable such as g.cnt, or a member of l.settings such as l.settings:cnt.

    HTH --
    Kent Multer
    Magic Metal Productions
    http://TheMagicM.com
    * Web developer/designer
    * E-commerce and Miva
    * Author, The Official Miva Web Scripting Book -- available on-line:
    http://www.amazon.com/exec/obidos/IS...icmetalproducA

    Comment


      #3
      Re: Variable Value For miva_array_search Offset?

      its a thought...though i've used them before in other smt statements...would be nice to have a definitive listing of permissible and exceptions......................................


      and the winner is:

      you can use a global, entered as a global, as in

      <mvt:while expr="miva_array_search(l.settings:thisArray, g.cnt, l.settings:selectName, 'tolower(g.thisVar) IN tolower(l.settings:selectName:Name)')">


      thanks for the suggestion kent.
      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


        #4
        Re: Variable Value For miva_array_search Offset?

        Ok, new twist...

        Here's the output of the miva_array_search

        [ 1 :RMSAMPS2T-wall-mounting-bracket : 4-Hole AMPS Car or Wall Mounting Pedestal with 4" Arm - Dual-T Compatible ]
        [ 2 :GN022-SBH-AMPS : Windshield Suction Mount with AMPS Head for Sirius Satellite Radios ]
        [ 3 :GN022-SBH-AMPS : Windshield Suction Mount with AMPS Head for Sirius Satellite Radios ]
        [ 4 :GN022-SBH-AMPS : Windshield Suction Mount with AMPS Head for Sirius Satellite Radios ]
        [ 5 :GN022-SBH-AMPS : Windshield Suction Mount with AMPS Head for Sirius Satellite Radios ]
        [ 6 :GN032-SBH-AMPS : Bike or Motorcycle Handlebar Mount with AMPS Head for Satellite Radios ]

        so, how does an offset of 2, produce the same result as 3, 4, 5 and 6? (list abbreviated...there are other duplicates).
        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


          #5
          Re: Variable Value For miva_array_search Offset?

          Ok, found my error...I was doing something stupid in the loop...as in not checking the count against array max...might need to rethink my 1 cup of coffee a day resolution.
          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


            #6
            Re: Variable Value For miva_array_search Offset?

            Just curious, how is the better or different than using

            <mvt:foreach iterator="select" array="thisArray">

            If you are only looking for a single items, it would give you a one line command, but if you are looking for multiple items I doubt it would be any faster than just looping through the array testing for matches to the expression.
            Last edited by RayYates; 07-29-15, 07:02 AM.
            Ray Yates
            "If I have seen further, it is by standing on the shoulders of giants."
            --- Sir Isaac Newton

            Comment


              #7
              Re: Variable Value For miva_array_search Offset?

              A system function will run much faster than a series of SMT statements, maybe 5-10 times faster. Admittedly, the difference probably wouldn't matter in most cases.
              Kent Multer
              Magic Metal Productions
              http://TheMagicM.com
              * Web developer/designer
              * E-commerce and Miva
              * Author, The Official Miva Web Scripting Book -- available on-line:
              http://www.amazon.com/exec/obidos/IS...icmetalproducA

              Comment


                #8
                Re: Variable Value For miva_array_search Offset?

                Hi Kent,

                Normally I would agree, and if you were doing this within MivaScript it "might" be true, but I kind of doubt it. I have to do this kind of search frequently after importing CSV files, so I was curious and did a tested within a page template that indicates the results are virtually identical (milliseconds difference).

                Product Records: 585
                Benchmark array_search: Elapsed Time = 0.003255 sec.
                Benchmark foreach: Elapsed Time = 0.00475 sec.

                It seems to be most useful if searching for a single record where there are no duplicates, like a product code.

                Code:
                <mvt:assign name="g.Maxitems" value="1000" />
                <mvt:item name="ry_toolbelt" param="Product_List|g.length|g.Maxitems|g.SortBy" />
                Items = <mvt:eval expr="miva_array_max(l.settings:products)" /><hr>
                
                
                <mvt:assign name="g.search" value="'ea'" />
                
                
                <mvt:item name="ry_toolbelt" param="benchmark|array_search|START" />
                <mvt:assign name="g.index" value="miva_array_search(l.settings:products, 1, l.settings:product, 'tolower(g.search) IN tolower(l.settings:product:name)')" />
                <mvt:while expr="g.index">
                	&mvt:global:index; = &mvt:product:name;<br>
                
                
                	<mvt:assign name="g.index" value="g.index + 1" />
                	<mvt:assign name="g.index" value="miva_array_search(l.settings:products, g.index, l.settings:product, 'tolower(g.search) IN tolower(l.settings:product:name)')" />
                </mvt:while>
                <mvt:item name="ry_toolbelt" param="benchmark|array_search|END" />
                <mvt:item name="ry_toolbelt" param="benchmark|array_search|SHOW" />
                <hr>
                
                
                <mvt:assign name="g.index" value="1" />
                <mvt:item name="ry_toolbelt" param="benchmark|foreach|start" />
                <mvt:foreach iterator="product" array="products">
                	<mvt:if expr="tolower(g.search) IN tolower(l.settings:product:code))">
                		&mvt:global:index; = &mvt:product:code;<br>
                	</mvt:if>
                	<mvt:assign name="g.index" value="g.index + 1" />
                </mvt:foreach>
                <mvt:item name="ry_toolbelt" param="benchmark|foreach|end" />
                <mvt:item name="ry_toolbelt" param="benchmark|foreach|show" />
                Ray Yates
                "If I have seen further, it is by standing on the shoulders of giants."
                --- Sir Isaac Newton

                Comment


                  #9
                  Re: Variable Value For miva_array_search Offset?

                  Interesting. Thanks for taking the time to test it.

                  The difference might be more pronounced with 5000 records, or 50,000. My sense is that Miva adds things like this to the VM when they run into a specific situation that requires better performance.
                  Kent Multer
                  Magic Metal Productions
                  http://TheMagicM.com
                  * Web developer/designer
                  * E-commerce and Miva
                  * Author, The Official Miva Web Scripting Book -- available on-line:
                  http://www.amazon.com/exec/obidos/IS...icmetalproducA

                  Comment


                    #10
                    Re: Variable Value For miva_array_search Offset?

                    I don't know how efficient compiled Templates are vs compiled MivaScript But my experience has been that the difference is small, and totally dependant on what modules/items you are accessing.

                    Now that they have added the ability to assign variables and evaluate expressions (including all MS functions), template performance has increased. I use the built in commands whenever possible and supplement the script with Toolbelt only as needed.
                    Ray Yates
                    "If I have seen further, it is by standing on the shoulders of giants."
                    --- Sir Isaac Newton

                    Comment


                      #11
                      Re: Variable Value For miva_array_search Offset?

                      Ray,

                      To answer your question. No F'n idea. I just assumed it was faster than running the loop twice. The basic idea was to find a SPECIFIC item in the search array to place FIRST, then the rest.

                      Had this been something we developed from the start I actually would have just used CSS/jQuery to PLACE the identified record first, instead of getting it as the first record. (But the CSS in use would support how I know how to do that.)

                      That's why it would be great to have more information on Mivascript and SMT Variances. (Not complaining, you still should be canonized for your efforts to date. Just we could use a LOT more information.)

                      I too have been doing more SMT than mivascript coding of late, mostly to keep from having to build interfaces for variable client input...but its frustratingly slow when you bump your head constantly into 'weird' stuff like "oh, that function doesnt support a local variable" and others.
                      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


                        #12
                        Re: Variable Value For miva_array_search Offset?

                        Hi Bruce,


                        SMT only supports global and l.settings variables (or in the case of modules l.all_settings). There are also limitations when assigning array variable values using an index pointer.


                        Assuming you have products loaded into l.settings:products...


                        This works:
                        <mvt:assign name="g.ptr" value="3" />
                        <mvt:eval expr="l.settings:products[g.ptr]:code" /><br>


                        These fail:
                        &mvt:products[g.ptr]:code;
                        Printing a variable that contains an expression causes a fatal error.


                        <mvt:assign name="l.settings:copy[g.ptr]:code" value="l.settings:products[g.ptr]:code" />
                        Assigning a variable name that contains an expression causes an error. I consider this a major over site.


                        But happily there are a workarounds:
                        I wrote Toolbelts to recursively parse and evaluate the nested expressions in the case of index pointers.
                        <mvt:item name="ry_toolbelt" param="assign|l.all_settings:copy[g.ptr]:code|l.all_settings:products[g.ptr]:code" />
                        <mvt:eval expr="l.settings:copy[g.ptr]:code" />


                        OR Strangely enough, this actually works
                        <mvt:assign name="g.varname" value=" 'l.settings:copy[g.ptr]:code' " />
                        <mvt:assign name="{ g.varname }" value="l.all_settings:products[g.ptr]:code" />
                        <mvt:eval expr="l.settings:copy[g.ptr]:code" /><br>


                        However you can not do these.
                        <mvt:assign name="{ l.settings:copy[g.ptr]:code }" value="l.all_settings:products[g.ptr]:code" />
                        <mvt:assign name="{ 'l.settings:copy[g.ptr]:code' }" value="l.all_settings:products[g.ptr]:code" />
                        Ray Yates
                        "If I have seen further, it is by standing on the shoulders of giants."
                        --- Sir Isaac Newton

                        Comment


                          #13
                          Re: Variable Value For miva_array_search Offset?

                          Thanks Ray, its great to have this info in one place when (ok, most of the time) I can't even remember what day it is.
                          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


                            #14
                            Re: Variable Value For miva_array_search Offset?

                            IIRC, l.all_settings is only available in the source code of component modules, not in template code. The module variable l.all_settings is actually the same as the Store Morph variable l.settings. The module variable l.settings is a shortcut to one member of l.all_settings; it has no equivalent in Store Morph.
                            Kent Multer
                            Magic Metal Productions
                            http://TheMagicM.com
                            * Web developer/designer
                            * E-commerce and Miva
                            * Author, The Official Miva Web Scripting Book -- available on-line:
                            http://www.amazon.com/exec/obidos/IS...icmetalproducA

                            Comment

                            Working...
                            X