Format elements

Problem

Given an Array of at least 2 elements, return a new Array of formatted elements without using array indices, pipelines or loop statements.

Solution

1
2
3
$array = 1..7
-split ('{0:C}' -f [PSObject]$array)
-split ('{0:C}' -f ($array -as 'PSObject'))

The secret is to cast the Array as PSObject, this causes PowerShell to enumerate all elements internally looking for extended members, the Format Operator performs its magic on each element and returns a single String with the new -formatted- String Array expanded within it. Finally the Split Operator breaks the String and we get a new String Array of formatted Strings.


Winner: No one

Share on: