Help:ArrayExtension

From Semantic Portal Wiki

Jump to: navigation, search

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}}
  • members: red
  • size(expect 1): 1
define array (a1), one element
{{#arraydefine:a1|0}}
  • members: 0
  • size(expect 1): 1
define array (b), four elements, and print the define array as a comma separated list
{{#arraydefine:b|orange,red ,yellow, yellow|,|print=list}}
  • members:orange, red, yellow, yellow
  • size(expect 4): 4
define array (b1), three unique elements, and print it
{{#arraydefine:b1|orange,red ,yellow, yellow|,|unique, print=list}}
  • members:orange, red, yellow
  • size(expect 3): 3
define array (b2), three unique elements, sorted in descending order, print it
{{#arraydefine:b2|orange,red ,yellow, yellow|,|unique, sort=desc, print=list}}
  • members:yellow, red, orange
  • size(expect 3): 3
define array (b3), three unique elements, and print it
{{#arraydefine:b3|0,1,2|,|unique, print=list}}
  • members:0, 1, 2
  • size(expect 3): 3
define array (c), empty
{{#arraydefine:c}}
  • members:
  • size(expect 0): 0
define array (d) without specifying delimiter - use ',' as default delimiter, two element
{{#arraydefine:d|apple, pear}}
  • members: apple, pear
  • size(expect 2): 2
define array (d1) with empty delimiter, one element
{{#arraydefine:d1|apple, pear|}}
  • members: apple, pear
  • size(expect 1): 1
define array (e) with customized delimiter, two elements
{{#arraydefine:e|apple ; pear, orange|;}}
  • members: apple, pear
  • size(expect 2): 2
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*/}}
  • members: apple, pear, orange
  • size(expect 3): 3

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}}

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
red
yellow
yellow

embed wiki link
{{#arrayprint:b|<br/>|@@@@|[[@@@@]]}}

orange
red
yellow
yellow

embed Semantic MediaWiki link
{{#arrayprint:b|<br/>|@@@@|[[prop1::@@@@]]}}

orange
red
yellow
yellow

embed parser function
{{#arrayprint:b|<br/>|@@@@|length of @@@@:{{#len:@@@@}}}}

length of orange:6
length of red:3
length of yellow:6
length of yellow: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

http://www.php.net/manual/en/function.count.php

{{#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

http://www.php.net/manual/en/function.array-search.php

{{#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/}}
  • case1 (expect -1): -1
  • case2 (expect 2): 2
  • case3 (expect 1): 1

use yes/no option

  • case1 (expect no): no
  • case2 (expect yes): yes

use offset option

  • case1 (expect 1): 1
  • case2 (expect -1): -1

use regular expression needle

  • case1 (expect 2): 2
  • case2 (expect -1): -1
  • case3 (expect 4): 4
  • case4 (expect -1): -1
  • case5 (expect 4): 4
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}}
  • expect "yellow": yellow
  • expect (empty string):
  • expect "bad value":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}}
  • before:
  • after: (expect empty because the offset is not a number)
  • after: (expect empty because the length is zero)
  • after: orange, red (expect orange,red )
  • after: red, yellow (expect red, yellow)
  • after: yellow, yellow (expect yellow, yellow)
  • after: (expect empty because the offset if beyond the index of the array)
  • after: orange, red, yellow, yellow, BLACK (expect orange, red, yellow, yellow, BLACK)- all element will be selected

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

Facts about ArrayExtensionRDF feed
Prop1Orange  +, Red  +, and Yellow  +
Prop2Orange  +, Red  +, and Yellow  +
Version1.2.2  +
Personal tools
Semantic Web Community
Tetherless World constellation
maintenance