-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathHandle-GenericList.ps1
77 lines (53 loc) · 2.14 KB
/
Handle-GenericList.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# PowerShell List Execution Sample
# Create Generic List for int
$list = New-Object 'System.Collections.Generic.List[int]'
# Create Generic List for string
$list = New-Object 'System.Collections.Generic.List[string]'
# Add items to list
$list.Add("a")
$list.Add("b")
$list.Add("c")
$list.Add("d")
$list.Add("a")
$list.Add("a")
$list.Add("a")
# Add range of items
$list.AddRange([string[]]@("e","f","g","h")) # make sure type match for list
# find matched item of System.Predicate using ScriptBlock (anonymous delegates)
$list.Find("a") # return Error
$list.Find({$args -eq "a"}) # return a
$list.Find({$args -ceq "A"}) # nothing return
$list.Find({$args -eq "Z"}) # nothing return
# find from last for an item of System.Predicate using ScriptBlock (anonymous delegates)
$list.FindLast({$args[0] -eq "c"}) # return c
# find all matched item of System.Predicate using ScriptBlock (anonymous delegates)
$list.FindAll({$args[0] -eq "a"}) # find all a
$list | where {$_ -eq "a"}
# find index for matched item from specific index to last index. item created is System.Predicate using ScriptBlock (anonymous delegates)
$list.FindIndex(0,{$args[0] -eq "a"}) # retrun index 0
$list.FindIndex(1,{$args[0] -eq "a"}) # retrun index 5
# find index of item start from last record
$list.FindLastIndex({$args[0] -eq "a"})
# insert item to specific index
$list.Insert(2,"f") # f will insert to index 2
# list up current items
$list
# Remove method for first match item
$list.Remove("a") # delete first a
# Removeall with create System.Predicate using ScriptBlock (anonymous delegates)
$list.RemoveAll({$args[0] -eq "a"}) # delete all a
# RemoveAt method to delete specific index item
$list.RemoveAt(0) # delete b
# RemoveRange method to delete specific range items
$list.RemoveRange(0,1) # delete c and d
# check item is exist or not
$list.Contains("f") #true
$list.Contains("a") #false
# output items into single String line with , foreach items
[string[]]$stringarray = $list.ToArray()
$stringSeparateComma = [string]::Join(",",$stringarray)
$stringSeparateComma # c,d,e,f,g,h
$string = [string]::Join("",$stringarray) # No separate
$string # cdefgh
# clear list
$list.Clear()