Sorting a 2D array with one row - arrays

I've recently started scripting in PowerShell and I'm having a difficulty in sorting a 2D array by multiple columns.
The following code is working fine. I have a 2D array (5x4) and I sort it by two columns; first, by the second column, and then by the first column.
$table1 = ("hhctrl.ocx",21,503,"Microsoft® HTML Help Control"),("mscomct2.ocx",10,629,"Microsoft Common Controls 2 ActiveX Control DLL"),("msscript.ocx",2,86,"Microsoft ® Script Control"),("sysmon.ocx",15,384,"System Monitor Control"),("tdc.ocx",1,61,"TDC ActiveX Control")
$table1_sorted = $table1 | Sort-Object #{Expression={$_[1]}; Ascending=$false}, #{Expression={$_[0]}; Ascending=$true}
echo (var_dump $table1)
echo (var_dump $table1_sorted)
var_dump is a custom function. I've created it to debug arrays. Write-Host and echo, they both flatten the arrays and don't seperate the items (no comma between items) e.g.
hhctrl.ocx 21 503 Microsoft® HTML Help Control mscomct2.ocx 10 629 Microsoft Common Controls 2 ActiveX Control DLL msscript.ocx 2 86 Microsoft ® Script Control sysmon.ocx 15 384 System Monitor Control tdc.ocx 1 61 TDC ActiveX Control
var_dump outputs:
[
[
"hhctrl.ocx",
21,
503,
"Microsoft® HTML Help Control"
],
[
"mscomct2.ocx",
10,
629,
"Microsoft Common Controls 2 ActiveX Control DLL"
],
[
"msscript.ocx",
2,
86,
"Microsoft ® Script Control"
],
[
"sysmon.ocx",
15,
384,
"System Monitor Control"
],
[
"tdc.ocx",
1,
61,
"TDC ActiveX Control"
]
]
[
[
"hhctrl.ocx",
21,
503,
"Microsoft® HTML Help Control"
],
[
"sysmon.ocx",
15,
384,
"System Monitor Control"
],
[
"mscomct2.ocx",
10,
629,
"Microsoft Common Controls 2 ActiveX Control DLL"
],
[
"msscript.ocx",
2,
86,
"Microsoft ® Script Control"
],
[
"tdc.ocx",
1,
61,
"TDC ActiveX Control"
]
]
Now, if I use another array, "table" with only one row, sorting flattens the array.
$table2 = ,("hhctrl.ocx",21,503,"Microsoft® HTML Help Control")
$table2_sorted = $table2 | Sort-Object #{Expression={$_[1]}; Ascending=$false}, #{Expression={$_[0]}; Ascending=$true}
echo (var_dump $table2)
echo (var_dump $table2_sorted)
Output:
[
[
"hhctrl.ocx",
21,
503,
"Microsoft® HTML Help Control"
]
]
[
"hhctrl.ocx",
21,
503,
"Microsoft® HTML Help Control"
]
This happens when there's only one row. Why is that?

Sort-Object does not return array. It return write to pipeline individual input items in sorted order.
PS> 7,3,8,5,1 | Sort-Object | ForEach-Object { Write-Host "Item: $_" }
Item: 1
Item: 3
Item: 5
Item: 7
Item: 8
As you can see, next command in pipeline ForEach-Object see each individual item, but not array as whole.
Wrapping to array in $table1 case happens, because last command in pipeline wrote more than one item in pipeline. In $table2 case last command in pipeline wrote only one item, so no wrapping necessary there. If you want to always wrap pipeline results into array regardless of amount of results (zero, one or many), then you need to use array subexpression operator:
$Results = #( First-Command | Middle-Command | Last-Command )

Related

How can I insert a JSON array in to a postgres column that is of type JSON[]?

I am trying to get some test data in to a column inside a PostgreSQL table, seems simple enough but have tried the following methods using pgAdmin 4 and nothing works. It is simply an array of JSON objects that will later be read in by a spring entity it is mapped to.
Simply using the GUI to double click a box in a row under the column
Expected - saves the JSON array
Actual - can not save
[{
"appName": "UCRM",
"scopes": [
"read",
"write"
]
},
{
"appName": "OCTA",
"scopes": [
"read",
"write",
"delete"
]
}]
error after trying to save the above JSON
using a script to insert the same data to every row that has that column
Expected - saves JSON array to every column "app_acl" for every row
Actual - It does insert in to the table but, saves a invalid JSON array and making it useless as shown below
script in question:
UPDATE application_acl SET app_acl = array[
'{
"appName": "UCRM",
"scopes": [
"read",
"write"
]
}',
'{
"appName": "OCTA",
"scopes": [
"read",
"write",
"delete"
]
}'
]::json[];
output:
{"{
\"appName\": \"UCRM\",
\"scopes\": [
\"read\",
\"write\"
]
}","{
\"appName\": \"OCTA\",
\"scopes\": [
\"read\",
\"write\",
\"delete\"
]
}"}

multiple 'instances' of the same labelled field on the same page in Azure Form Recognizer custom model?

I was wondering if there is something I'm missing on dealing with multiple instances of the same labelled field in a Azure Form Recognizer Custom Model (with labels)? Let's use the following (VERY simplified) document, for example:
Now, If I train a model to detect 'Name', 'DOB', and 'Company', I end up with results that look like:
{
"fields": {
"Name": {
"value_type": "string",
"label_data": null,
"value_data": {
"page_number": 1,
"text": "John R. Smith Ronald Johnson., Esquire",
"bounding_box": [
[
0.57,
4.435
],
[
1.8,
4.435
],
[
1.8,
6.005
],
[
0.57,
6.005
]
],
"field_elements": null
},
"name": "Name",
"value": "John R. Smith Ronald Johnson., Esquire",
"confidence": 1
},
...
As you can see, there is no delimiter between each 'instance' of the Name field in the Azure Form Recognizer results JSON. How should I train and/or deal with the Field results in a way that allows me to extract each instance of a given field from the document?
The first thing I tried, was marking the label name & the value for a field from the document and training on that. For example, Name: John R. Smith and Name: Ronald Johnson., Esquire would be what i marked in FOTT as the Name field for this training example. Then, I would split the result on Name:. This seems fine in theory, but in practice I ended up with VERY low accuracy compared to selecting JUST the field value and training on those.
Please label these as Name1 and Name2 to extract them as separate fields.

jq: delete element from array

I have this JSON file and want to delete an element from an array:
{
"address": "localhost",
"name": "local",
"vars": {
"instances": [
"one",
"two"
]
}
}
I am using this command:
jq 'del(.vars.instances[] | select(index("one")))' data.json
The output is:
{
"address": "localhost",
"name": "local",
"vars": {
"instances": [
"two"
]
}
}
So it works as expected, but only with jq v1.6. With jq v1.5 I get this error:
jq: error (at data.json:20): Invalid path expression near attempt to access element 0 of [0]
So what am I doing wrong? Is this a bug or a feature of v1.5? Is there any workaround to get the same result in v1.5?
Thanks in advance
Vince
One portable to work with on both versions would be,
.vars.instances |= map(select(index("one")|not))
or if you want to still use del(), feed the index of the string "one" to the function as below, where index("one") gets the index 0 which then gets passed to delete as del(.[0]) meaning to delete the element at zeroth index.
.vars.instances |= del(.[index("one")])
The implementation of del/1 has proven to be quite difficult and indeed it changed between jq 1.5 and jq 1.6, so if portability across different versions of jq is important, then usage of del/1 should either be restricted to the least complicated cases (e.g., no pipelines) or undertaken with great care.

How can I build this kind of UI, from AWS Comprehend Medical?

I'm using the AWS machine learning service Comprehend Medical to analyse clinical texts and extract data.
Some context info (skippable maybe):
This is the type of input:
Pt is 40yo mother, highschool teacher
HPI : Sleeping trouble on present dosage of Clonidine. Severe Rash on face and leg, slightly itchy
Meds : Vyvanse 50 mgs po at breakfast daily,
Clonidine 0.2 mgs -- 1 and 1 / 2 tabs po qhs
HEENT : Boggy inferior turbinates, No oropharyngeal lesion
Lungs : clear
Heart : Regular rhythm
Skin : Mild erythematous eruption to hairline
Follow-up as scheduled
This is the kind of output I get from AWS:
{
"Entities": [
{
"Id": 0,
"BeginOffset": 6,
"EndOffset": 10,
"Score": 0.9984116554260254,
"Text": "40yo",
"Category": "PROTECTED_HEALTH_INFORMATION",
"Type": "AGE",
"Traits": []
},
{
"Id": 1,
"BeginOffset": 19,
"EndOffset": 37,
"Score": 0.28823626041412354,
"Text": "highschool teacher",
"Category": "PROTECTED_HEALTH_INFORMATION",
"Type": "PROFESSION",
"Traits": []
},
...
Then I would like to render this kind of UI, the same way AWS does in their console to represent Comprehend medical outputs (see the image) :
AWS Console Comprehend Medical output representation
I managed to tag the text. But I have no idea in how to make the tagged arrows: represent those dependecies between html elements.
I have tried some react libraries like Taggy(only do tagging) and explored some other solutions like Spacey (will require SSR and not even the same output of tags + tagged arrows ),
Anyone could suggest an approach to do this kind of tagged arrows between those html components?
Thank you ma people.

Can I mix type commands with variables in batch?

I am making a batch game, and I am making visual maps for it. Here's an example.
========
(Town)
========
[ ]
|
[ ] - [ ] - [ ] - [ ] - [ ] - [ ]
| | |
[ ] - [ ] - [ ] - [ ]
| |
[ ] - [ ] - [ ] - [ ] - [ ]
|
[ ]
Current Location: %loc%
1) Go up
2) Go down
3) Go left
4) Go right
As you can see, I used %loc% at one point. I am planning to use type commands for this, but it always appears as is (%loc%). I have found nothing else that clarifies this. Will adding variables into a text document like this work at all (note that I am also planning to use variables in every space in the square brackets), and if not, is this map basic enough so that I can paste it directly into the editor with some echo commands (haven't tried, don't have the time just yet)? If neither of these work, is there anything else I can do to avoid making separate text documents for every location? If not, no worries.

Resources