Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support non-object arrays #14

Closed
iRon7 opened this issue Mar 8, 2021 · 2 comments
Closed

Support non-object arrays #14

iRon7 opened this issue Mar 8, 2021 · 2 comments
Assignees

Comments

@iRon7
Copy link
Owner

iRon7 commented Mar 8, 2021

Especially in a case of a side-by-side joins (where the -on parameter is omitted), it would be handy to be able join an array of something that is not an object (meaning anything that doesn't have visual properties like strings and primitives) to an complex (PowerShell) object list, or to another array of non-objects.

@iRon7 iRon7 self-assigned this Mar 8, 2021
@iRon7
Copy link
Owner Author

iRon7 commented Mar 9, 2021

Implementation

(Version 3.4.0 and up)

If you join on or more scalar arrays, a scalar array will be return which can be easily used in a foreach loop:

$a = 'a1', 'a2', 'a3', 'a4'
$b = 'b1', 'b2', 'b3', 'b4'
$c = 'c1', 'c2', 'c3', 'c4'
$d = 'd1', 'd2', 'd3', 'd4'

Example 1:
Default join of multiple scalar arrays

$a |Join $b |Join $c |Join $d |% { "$_" } 
# or: foreach ($row in ($a |Join $b |Join $c |Join $d)) { "$Row" }
# or: $a |Join $b |Join $c |Join $d |% { '{0} {1} {2} {3}' -f $_[0], $_[1], $_[2], $_[3] }  

a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4

Example 2:
Naming items

$a |Join $b |Join $c |Join $d -Name a, b, c, d

a  b  c  d
-  -  -  -
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4

Example 3:
Joining a scalar array with an object array:

$Id = 'a1', 'a2', 'a3', 'a4'
$Department = ConvertFrom-Csv @'
"Name",        "Country"
"Engineering", "Germany",
"Marketing",   "England",
"Sales",       "France",
"Purchase",    "France"
'@

$Id |Join $Department

Value Name        Country
----- ----        -------
a1    Engineering Germany
a2    Marketing   England
a3    Sales       France
a4    Purchase    France

Note that the scalar column will be named Value by default when it joined with a PowerShell object,
to change this use the -ValueName parameter, e.g.: $Id |Join $Department -ValueName Id

@iRon7 iRon7 changed the title Support scalar arrays Support non-object arrays Mar 10, 2021
iRon7 added a commit that referenced this issue Mar 11, 2021
iRon7 added a commit that referenced this issue Mar 11, 2021
@iRon7
Copy link
Owner Author

iRon7 commented Mar 11, 2021

Implemented in version 3.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant