Help:ArrayExtension
From Semantic Portal Wiki
1.2.2
for general documentation: http://www.mediawiki.org/wiki/Extension:ArrayExtension
Contents |
Examples
define an array
| description | source | result |
|---|---|---|
| define array (a), one element | {{#arraydefine:a|red}}
|
|
| define array (a1), one element | {{#arraydefine:a1|0}}
|
|
| define array (b), four elements, and print the define array as a comma separated list | {{#arraydefine:b|orange,red ,yellow, yellow|,|print=list}}
|
|
| define array (b1), three unique elements, and print it | {{#arraydefine:b1|orange,red ,yellow, yellow|,|unique, print=list}}
|
|
| define array (b2), three unique elements, sorted in descending order, print it | {{#arraydefine:b2|orange,red ,yellow, yellow|,|unique, sort=desc, print=list}}
|
|
| define array (b3), three unique elements, and print it | {{#arraydefine:b3|0,1,2|,|unique, print=list}}
|
|
| define array (c), empty | {{#arraydefine:c}}
|
|
| define array (d) without specifying delimiter - use ',' as default delimiter, two element | {{#arraydefine:d|apple, pear}}
|
|
| define array (d1) with empty delimiter, one element | {{#arraydefine:d1|apple, pear|}}
|
|
| define array (e) with customized delimiter, two elements | {{#arraydefine:e|apple ; pear, orange|;}}
|
|
| define array (f) with customized delimiter, three elements
see also:http://us2.php.net/manual/en/function.preg-split.php | {{#arraydefine:f|apple ; pear, orange|/\s*[;,]\s*/}}
|
|
deal with SMW query results
1. create an array from SMW query result
* target total: {{#ask:[[Category:Person]][[:+]] |format=count}}
* SMW query result: {{#ask:[[Category:Person]][[:+]]|sep=,|limit=5|searchlabel=}}
* arraydefine result: {{#arraydefine:persons|{{#ask:[[Category:Person]][[:+]]|sep=,|limit=5|searchlabel=}}|,|print=list }}
* arraydefine size: {{#arraysize:persons}}
- target total: 434
- SMW query result: A. Krishna Sinha, Abdelmounaam Rezgui, Aditya Kalyanpur, Akshay Java, Alain Leger
- arraydefine result: A. Krishna Sinha, Abdelmounaam Rezgui, Aditya Kalyanpur, Akshay Java, Alain Leger
- arraydefine size: 5
note: there is a limitation when SMW does not return all results
2. list unique values of a property in descending order
* target total: {{#ask:[[version::+]] |format=count}}
* SMW query result: {{#ask:[[version::+]]|?version= |link=none |mainlabel=-|sep=,|limit=1000|searchlabel=}}
* arraydefine result: {{#arraydefine:versions|{{#ask:[[version::+]]|?version= |link=none |mainlabel=-|sep=,|limit=1000|searchlabel=}}|,|unique, sort=desc, print=list }}
* arraydefine size: {{#arraysize:versions}}
- target total: 46
- SMW query result: 1.2.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.21, Category:SBP 0.2, Category:SBP 0.2, Category:SBP 0.2, Category:SBP 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.3, 0.21, 0.2, 0.3, 0.3, 0.2.1, 0.2, 0.2, 0.2, 0.2, 0.1, 0.1, 0.2, Category:SBP 0.2, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1
- arraydefine result: Category:SBP 0.2, 1.2.2, 0.3, 0.21, 0.2.1, 0.2, 0.1
- arraydefine size: 7
print array
- now more powerful (can do some foreach work)
| print with default delimiter ', ' | {{#arrayprint:b}}
|
orange, red, yellow, yellow |
| print with customized delimiter '', i.e. no delimiter | {{#arrayprint:b|}}
|
orangeredyellowyellow |
| print with customized delimiter '<br/>' | {{#arrayprint:b|<br/>}}
|
orange |
| embed wiki link | {{#arrayprint:b|<br/>|@@@@|[[@@@@]]}}
| |
| embed Semantic MediaWiki link | {{#arrayprint:b|<br/>|@@@@|[[prop1::@@@@]]}}
| |
| embed parser function | {{#arrayprint:b|<br/>|@@@@|length of @@@@:{{#len:@@@@}}}}
|
length of orange:6 |
| embed template (with parameters) | {{#arrayprint:b|<br/>|@@@@|{{f.tag{{f.print.vbar}}prop2{{f.print.vbar}}@@@@}}}}
|
other print options
- the input array b: orange, red, yellow, yellow, BLACK
- the input array f: 4, 0, 6
| size of array | {{#arraysize:b}}
|
5 |
| return the index of the first occurrence of an element in an array, '-1' for not found, non-negative number for index |
{{#arraysearch:b|white}}
{{#arraysearch:b|red}}
{{#arraysearch:f|0}}
{{#arraysearch:b|white|0|yes|no}}
{{#arraysearch:b|yellow|0|yes|no}}
{{#arraysearch:b|red|0}}
{{#arraysearch:b|red|2}}
{{#arraysearch:b|/low/}}
{{#arraysearch:b|low}}
{{#arraysearch:b|/ack/i}}
{{#arraysearch:b|/ack/}}
{{#arraysearch:b|/ACK/}}
|
use yes/no option
use offset option
use regular expression needle
|
| show an array element by index |
*expect "yellow": {{#arrayindex:b|2}}
*expect (empty string): {{#arrayindex:b|-2}}
*expect "bad value":{{#arrayindex:b|-2|default=bad value}}
|
|
create new array
- array a: red
- array b: orange, red, yellow, yellow, BLACK
- array c: DARK
| merge two arrays | {{#arraymerge:x|a|b}}
|
red, orange, red, yellow, yellow, BLACK |
| duplicate an array (keep the third argument of arraymerge empty | {{#arraymerge:x|b}}
|
orange, red, yellow, yellow, BLACK |
| append an array to an array | {{#arraymerge:c|c|b}}
|
before: DARK after: DARK, orange, red, yellow, yellow, BLACK |
| get a slice from an array | {{#arrayslice:x|b|abd|2}}
{{#arrayslice:x|b|0|0}}
{{#arrayslice:x|b|0|2}}
{{#arrayslice:x|b|1|2}}
{{#arrayslice:x|b|-3|2}}
{{#arrayslice:x|b|5}}
{{#arrayslice:x|b|-10}}
|
|
create new array - set operations
| intersect | {{#arrayintersect:x|a|b}}
|
red |
| union | {{#arrayunion:x|a|b}}
|
red, orange, yellow, BLACK |
| diff (b-a) | {{#arraydiff:x|b|a}}
|
orange, yellow, BLACK |
| diff (a-b) | {{#arraydiff:x|a|b}}
|
|
alter an array
| sort an array (desc order) | {{#arraysort:b|desc}}
|
before: orange, red, yellow, yellow, BLACK --b[2]= yellow after: yellow, yellow, red, orange, BLACK --b[2]= red |
| sort an array (random order) | {{#arraysort:b|random}}
|
before: yellow, yellow, red, orange, BLACK --b[2]= red after: yellow, BLACK, orange, red, yellow --b[2]= orange |
| sort an array (reverse) | {{#arraysort:b|reverse}}
|
before: yellow, BLACK, orange, red, yellow --b[2]= orange after: yellow, red, orange, BLACK, yellow --b[2]= orange |
| convert array to set | {{#arrayunique:b}}
|
before (b): yellow, red, orange, BLACK, yellow after (b): yellow, red, orange, BLACK before (x): a, b, , , c, c after (x): a, b, , c |
| reset an array |
{{#arraydefine:0|0,2,3,4}}
{{#arrayprint:0}}
{{#arrayreset:0}}
{{#arrayprint:0}}
|
before: 0, 2, 3, 4 after: undefined array: 0 |
| reset all defined arrays | {{#arrayreset:}}
|
before: yellow, red, orange, BLACK after: undefined array: b |
FAQ
handle multi-dimension array
In order to access a 2D array "red;#da2021, yellow;#fcff00, green;#00ff00", we can take the following steps:
1. split the array into an array:
{{#arraydefine:colors|red;#da2021, yellow;#fcff00, green;#00ff00}}
- colors[0]: red;#da2021
- size of colors: 3
2. split the first element into an array
{{#arraydefine:color0|{{#arrayindex:colors|0}}|;}}
- colors[0][0]: red
- size of colors[0]: 2
Note, it is also possible to use variable (require VariablesExtension)
{{#vardefine:i|0}}
{{#arraydefine:color_i|{{#arrayindex:colors|{{#var:i}}}}|;}}
- i=0
- colors[i][0]: red
- size of colors[i]: 2
iteratively access array members
source code
{{#arraydefine:x
|{{#ask:[[package::+]] |?package= |mainlabel=-|format=list|sep=,|link=none|searchlabel=}}
|,
|unique
}}
{{#arrayprint:x||@@@@|<nowiki/>
* package: @@@@; related pages: {{#ask: [[package::@@@@]]}}
}}
output
- Help:Smwbp foundation templates, Help:Smwbp instance templates, Help:Smwbp ontology templates, Help:Smwbp rule templates
- 4
- package: Help:Smwbp foundation templates; related pages: F.print.formfoot, F.print.section, F.print.vbar, F.show, F.smwbp, and F.tag
- package: Help:Smwbp instance templates; related pages: I.event, I.event, I.location, I.location, I.news, I.news, I.person, I.person, I.person.photo, I.person.plain, I.person.relation, I.presentation, I.presentation, I.presentation.plain, I.project, I.project, I.publication, I.publication, I.publication.bibtex, I.publication.plain, I.software, and I.software
- package: Help:Smwbp ontology templates; related pages: O.class, O.class, O.ontology, O.ontology, O.property, O.property, O.thing, and O.thing
- package: Help:Smwbp rule templates; related pages: R.inverse

