Difference between revisions of "Template:Smart lookup"

From QBWiki
Jump to navigation Jump to search
m
Line 1: Line 1:
<includeonly>{{#vardefine:page|{{{1}}}}}{{#vardefine:index|{{{2|1}}}}}{{Add|index|1}}{{#if:{{{{#var:page}}|{{#var:index}}}}<!--
+
<includeonly>{{List lookup helper|{{{1}}}}}{{#vardefine:index|{{{2|1}}}}}<!--
if the page at index exists, return immediately and be done with it
+
-->{{#ifexpr:{{#var:index}}<={{#var:length}}<!--
-->|{{{{#var:page}}|{{#var:index}}}}
+
if index is less than length of list, return immediately
|{{#vardefine:minindex|{{#var:index}}}}{{#vardefine:maxindex|1}}<!--
+
-->|{{#var:entry_{{#var:index}}}}
otherwise it is out of bounds and we need to wrap around - since we need the total size, do a binary search for last list entry for fun
+
|{{#vardefine:adjusted_index|{{#expr:({{{2|1}}} mod {{#var:index}})}}}}{{#ifexpr:{{#var:adjusted_index}}=0|{{#vardefine:adjusted_index|{{#var:index}}}}}}{{#var:entry_{{#var:adjusted_index}}}}
-->{{#dowhile:
 
|{{#ifexpr:{{#var:maxindex}}+1<{{#var:minindex}}|continue}}
 
|{{#if:{{{{#var:page}}|{{#var:index}}}}
 
<!--undershoot-->|{{#ifexpr:{{#var:index}}>{{#var:maxindex}}|{{#vardefine:maxindex|{{#var:index}}}}}}
 
<!-- overshoot-->|{{#ifexpr:{{#var:index}}<{{#var:minindex}}|{{#vardefine:minindex|{{#var:index}}}}}}
 
}}
 
{{#vardefine:index|{{#expr:floor(({{#var:maxindex}}+{{#var:minindex}})/2)}}}}
 
}}{{#vardefine:adjusted_index|{{#expr:({{{2|1}}} mod {{#var:index}})}}}}{{#ifexpr:{{#var:adjusted_index}}=0|{{#vardefine:adjusted_index|{{#var:index}}}}}}{{{{#var:page}}|{{#var:adjusted_index}}}}
 
 
}}</includeonly><noinclude>{{Template doc|
 
}}</includeonly><noinclude>{{Template doc|
 
Given a list and an index ''i'', return the (''i'' mod ''N'')-th entry in the list.
 
Given a list and an index ''i'', return the (''i'' mod ''N'')-th entry in the list.
Line 17: Line 9:
 
This behavior was extracted from [[Template:Quick links smart lingo]].
 
This behavior was extracted from [[Template:Quick links smart lingo]].
 
<pre>
 
<pre>
{{Smart lookup
+
{{Smart lookup|QBWiki:Quick links/lingo|1}}
|Quick links lingo <!-- page containing list -->
 
|1                 <!-- index -->
 
}}
 
 
</pre>
 
</pre>
  
{{Smart lookup|Quick links lingo|1}}
+
{{Smart lookup|QBWiki:Quick links/lingo|1}}
 
}}</noinclude>
 
}}</noinclude>

Revision as of 18:58, 14 December 2025

Template documentation

Given a list and an index i, return the (i mod N)-th entry in the list.

This behavior was extracted from Template:Quick links smart lingo.

{{Smart lookup|QBWiki:Quick links/lingo|1}}

Wondering why people keep saying "m"?