Change values in arrays while rolling through them.
See https://www.scotsscripts.com/mvblog/sort-tricks-for-quicksortarray.html for a more basic example in SMT.
This is an example of rolling through options in basket groups and changing prices without going through the "make a new array while we go along" deal.
We create a reference array called l.groups
based on l.all_settings:basket:groups
- any changes we make to l.groups
will be reflected in the original array. Works in mivascript and SMT.
<MvASSIGN NAME = "l.ok" VALUE = "{ miva_array_copy_ref( l.all_settings:basket:groups, 1, miva_array_elements(l.all_settings:basket:groups), l.groups, 1 ) }" />
<MvFOREACH ITERATOR = "l.group" INDEX = "l.grouppos" ARRAY = "l.groups">
<MvFOREACH ITERATOR = "l.option" INDEX = "l.optionpos" ARRAY = "l.group:options">
<MvIF EXPR = "{ gettoken(l.option:subtotal,'.',2) GE 50 }">
<MvASSIGN NAME = "l.option:subtotal" VALUE = "{ l.option:subtotal + 1 }" />
<MvASSIGN NAME = "l.option:subtotal" VALUE = "{ gettoken(l.option:subtotal,'.',1) $ '.00' }" />
<MvASSIGN NAME = "l.option:formatted_subtotal" VALUE = "{ [ g.Module_Root $ g.Store:currncy_mod:module ].CurrencyModule_AddFormatting( g.Store:currncy_mod, l.option:subtotal ) }">
<MvELSE>
<MvASSIGN NAME = "l.option:subtotal" VALUE = "{ gettoken(l.option:subtotal,'.',1) $ '.00' }" />
<MvASSIGN NAME = "l.option:formatted_subtotal" VALUE = "{ [ g.Module_Root $ g.Store:currncy_mod:module ].CurrencyModule_AddFormatting( g.Store:currncy_mod, l.option:subtotal ) }">
</MvIF>
</MvFOREACH>
<MvASSIGN NAME = "l.total" VALUE = "{ l.group:subtotal_comprehensive + l.total }" />
</MvFOREACH>
After we're done, the original l.all_settings:basket:groups
array will contain the various price changes we made.
Scot's Modules
All modern modules from Scot have a sort(l.list)
function that sorts on disp_order
- and it's easy to add disp_order
to the array structure using miva_array_copy_ref(...)
For example, load all product custom fields into l.fields
and alphabetize them. First use miva_array_copy_ref(...)
then roll through the reference array and set disp_order
to the field name and then use the sort function to alphabetize the "parent" array.
<MvASSIGN NAME = "l.ok" VALUE = "{ [ g.Module_Root $ '/modules/util/customfld.mvc' ].ProductFieldList_Load_All( l.fields ) }" />
<MvASSIGN NAME = "l.ok" VALUE = "{ miva_array_copy_ref( l.fields, 1, miva_array_elements(l.fields), l.temp, 1 ) }" />
<MvFOREACH ITERATOR = "l.item" INDEX = "l.itempos" ARRAY = "l.temp">
<MvASSIGN NAME = "l.item:disp_order" VALUE = "{ l.item:name }" />
</MvFOREACH>
<MvASSIGN NAME = "l.ok" VALUE = "{ sort(l.fields) }" />
Done. The custom fields which load by default in ID order are now sorted in alpha order by name.