Hello I am getting error "Missing '=' operator after key in hash literal." on the IF statement, where am I wrong
$Nests | ForEach-Object {
$RecordMetrics = [pscustomobject]#{
key = $_.key
count = $_.doc_count
if (key -like 'CBT99*')
{
bo = 'CBT'
SiteID = 1972
}
elseif
{
boperator = $map[$_.key.TrimEnd("1","2","3","4","5","6","7","8","9","0","-") ].boperator
SiteID = $map[$_.key.TrimEnd("1","2","3","4","5","6","7","8","9","0","-") ].SiteID
}
}}
You can't put a conditional statement in the middle of a hashtable literal like that.
You'll have to create the hashtable/dictionary first, then populate the relevant keys based on your conditional logic:
$Nests | ForEach-Object {
# create dictionary with initial properties
$properties = [ordered]#{
key = $_.key
count = $_.doc_count
Host = ''
OperatorName = ''
}
# conditionally add remaining properties
if ($_.key -like 'CBT99*') {
$properties['bo'] = 'CBT'
$properties['SiteID'] = 1972
}
else {
$properties['boperator'] = $map[$_.key.TrimEnd("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-") ].boperator
$properties['SiteID'] = $map[$_.key.TrimEnd("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-") ].SiteID
}
# convert to custom object
$RecordMetrics = [pscustomobject]$properties
}
Related
Basically I formed an array of data based on certain conditions inside a loop and arrary data is something like this:
student1 RL123 S12 student2 RL423 S32 student6 RL166 S02 student34 RL993 P12 student99 RL923 S12
Above array data needs to be converted to JSON as below:
{
"Name" : "student1"
"RollNo" : "RL123"
"SubjectID" : "S12"
},
{
"Name" : "student2"
"RollNo" : "RL423"
"SubjectID" : "S32"
},
{
"Name" : "student6"
"RollNo" : "RL166"
"SubjectID" : "S02"
},
{
"Name" : "student34"
"RollNo" : "RL993"
"SubjectID" : "RL993"
},
{
"Name" : "student99"
"RollNo" : "RL923"
"SubjectID" : "S12"
}
If indeed your array is an array of strings (let's call it $students), you can skip the below line.
However, the way you have formatted it in your question makes it a string with space-separated items, so if that is the case, we should create a proper array from it by splitting on the whitespaces:
$students = 'student1 RL123 S12 student2 RL423 S32 student6 RL166 S02 student34 RL993 P12 student99 RL923 S12' -split '\s+'
Now just iterate over the values in the array and create objects using 3 array elements on each object:
$data = for ($i = 0; $i -lt $students.Count; $i+=3) {
[PsCustomObject]#{
Name = $students[$i]
RollNo = $students[$i + 1]
SubjectID = $students[$i + 2]
}
}
# here you convert the object array to JSON
$data | ConvertTo-Json
Output:
[
{
"Name": "student1",
"RollNo": "RL123",
"SubjectID": "S12"
},
{
"Name": "student2",
"RollNo": "RL423",
"SubjectID": "S32"
},
{
"Name": "student6",
"RollNo": "RL166",
"SubjectID": "S02"
},
{
"Name": "student34",
"RollNo": "RL993",
"SubjectID": "P12"
},
{
"Name": "student99",
"RollNo": "RL923",
"SubjectID": "S12"
}
]
the opening [ and closing ] denote that we're dealing with an array in Json syntax
Is there a cleaner way of writing the conditional for case "1", "4", "7"? For this case I want to write to the string "b" only if the string value is not "4".
var b = ""
var c = ""
let s = "4"
switch s {
case "0", "6", "8":
c += "|_|"
case "1", "4", "7":
if s != "4" { b += " |" }
c += " |"
case "2":
c += "|_ "
case "3", "5", "9":
c += " _|"
default:
c += ""
}
This is a place you could use the fallthrough statement:
switch s {
case "0", "6", "8":
c += "|_|"
case "1", "7":
b += " |"
fallthrough
case "4":
c += " |"
case "2":
c += "|_ "
case "3", "5", "9":
c += " _|"
default:
break
}
Since you want "1" and "7" to execute both statements, give them their own case and then follow it with fallthough which allows it to fall though to the next case. Then "1", "4", and "7" will all execute the code in the case "4": case.
I think the whole idea of switch statement is that you don’t use ifs inside. You should probably just create a separate case for “4”. The amount of space saved by putting this conditional isn’t worth obscuring the code IMO.
I am trying to build a Powershell array containing a single hashtable.
$params = #{
"name" = "bob"
"age" = "30"
}
$params | ConvertTo-Json
current output:
{
"age": "30",
"name": "bob"
}
Desired output:
[
{
"age": "30",
"name": "bob"
}
]
Try it this way:
$params = #{
"name" = "bob"
"age" = "30"
}
ConvertTo-Json #($params)
The #() syntax makes it an array.
You have to give the ConvertTo-Json cmdlet the input as a parameter, because the pipeline will automatically "unroll" the array and you'll be right back where you started.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a string let string = "!101eggs". Now, I want to have an array like this ["!", "101", "e", "g", "g", "s"]. How can I do this?
I presume the hard part for you is "Where's the number"? As long as this is just a simple sequence of digits, a regular expression makes it easy to find:
let string = "!101eggs"
let patt = "\\d+"
let reg = try! NSRegularExpression(pattern:patt)
let r = reg.rangeOfFirstMatch(in: string,
options: [],
range: NSMakeRange(0,string.utf16.count)) // {1,3}
So now you know that the number starts at position 1 and is 3 characters long. The rest is left as an exercise for the reader.
Sorry It's too long
when input is
print("-1-2a000+4-1/000!00005gf101eg14g1s46nj3j4b1j5j23jj212j4b2j41234j01010101g0000z00005g0000".toArrayByNumber())
Result: ["-", "1", "-", "2", "a", "000", "+", "4", "-", "1", "/", "000", "!", "00005", "g", "f", "101", "e", "g", "14", "g", "1", "s", "46", "n", "j", "3", "j", "4", "b", "1", "j", "5", "j", "23", "j", "j", "212", "j", "4", "b", "2", "j", "41234", "j", "01010101", "g", "0000", "z", "00005", "g", "0000"]
extension Int {
func toZeroString() -> String {
return (0 ..< self).reduce("", { (result, zero) -> String in
return result + "0"
})
}
}
extension String {
func toArrayByNumber() -> [String] {
var array: [String] = []
var num = 0
var zeroCount = 0
var zeroEnd = false
for char in self.characters {
if let number = Int("\(char)") {
if zeroEnd == false && number == 0 {
zeroCount += 1
} else {
num = num * 10 + number
zeroEnd = true
}
} else {
if num != 0 {
array.append(zeroCount.toZeroString() + ("\(num)"))
} else if zeroCount > 0 {
array.append(zeroCount.toZeroString())
}
array.append(String(char))
num = 0
zeroCount = 0
zeroEnd = false
}
}
if num != 0 {
array.append(zeroCount.toZeroString() + ("\(num)"))
} else if zeroCount > 0 {
array.append(zeroCount.toZeroString())
}
return array
}
}
Why does this code count 6 elements, as 9 ("wrong") in swift playground.
var stringArray = ["1", "2", "3", "4", "5", "6"]
for var i = 0; i < 3; i++ {
stringArray.append("Paragraph" + "\(i)")
}
func concat (array: [String]) -> String {
let count = UInt32(stringArray.count) ** --> =9 **
let randomNumberOne = Int(arc4random_uniform(count))
let randomNumberTwo = Int(arc4random_uniform(count))
let randomNumberThree = Int(arc4random_uniform(count))
let concatString = array[randomNumberOne] + array[randomNumberTwo] + array[randomNumberThree]
return concatString
}
let finalString = concat(stringArray)
...but count this code as 6 (correct)
var stringArray = ["1", "2", "3", "4", "5", "6"] ** --> =6 **
let count = UInt32(stringArray.count)
Does it have something to do with 64 vs 32 bit? I have Xcode Version 6.0 (6A313).
You are appending new elements to the same stringArray which already has content ["1", "2", "3", "4", "5", "6"]. Then you append the "paragraph (i)" string to it 3 times. So, the new content is now, ["1", "2", "3", "4", "5", "6", "Paragraph 1", "Paragraph 2", "Paragraph 3"]. Thats how the count reached to 9.