How to sort in group for infragistics ultragrid? - infragistics

I have a problem about Ultragrid group and sort.
My data.
"1"
"19"
"2"
"3"
I want sort data:
"1" , "2" , "3" , "19"
How to sort and group that ?
e.Layout.Bands[0].SortedColumns.Add("GuestName", false, true);
Thank you

UltraGridColumn has SortComparer property. This property allows you to assign your own comparer. So, go ahead, implement your own sort comparer and assign it to the column you need.

Related

regex to convert key values in a column to an hstore or jsonb column?

I have a database that has a table called activity with a column called detail that has this unfortunate representation of key/value pairs:
Key ID=[813],\n
Key Name=[Name of Key],\n
Some Field=[2732],\n
Another Field=[2751],\n
Description=[A text string here],\n
Location=[sometext],\n
Other ID=[2360578],\n
It's maybe clear from the formatting above, this is a one value per line and \n is a newline character so there's always one extra newline. I'm trying to avoid having an external program process this data, so I'm looking into postgresql's regex functions. The goal is to convert this to a jsonb or hstore column, I don't really care which.
Schema for the table is like:
CREATE TABLE activity
(
id integer NOT NULL,
activity_type integer NOT NULL,
ts timestamp with time zone,
detail text NOT NULL,
details_hstore hstore,
details_jsonb jsonb,
CONSTRAINT activity_pkey PRIMARY KEY (id),
);
So I'd like to run an UPDATE where I update the details_jsonb or details_hstore with the processed data from detail.
This:
select regexp_matches(activity.detail, '(.*?)=\[(.*?)\]\,[\r|\n]', 'g') as val from activity
gets me these individual rows (this is from pgadmin, I assume these are all strings):
{"Key ID",813}
{"Key Name","Name of Key"}
{"Some Field",2732}
{"Another Field",2751}
{Description,"A text string here"}
{Location,sometext}
{"Other ID",2360578}
I'm not a regex whiz but I think I need some kind of grouping. Also, that's returning as a text array of some kind, but what I really want is like this for jsonb
{"Key ID": "813", "Key Name": "Name of Key"}
or even better, if it's a number only then
{"Key ID": 813, "Key Name": "Name of Key"}
and/or the equivalent for hstore.
I feel like I'm a number of regex-in-postgres concepts away from this goal.
First is how to get ALL the pairs together in some kind of array or something, not as separate rows.
Second is, can I figure if it's a number and optionally get "" around strings and nothing around numbers for jsonb or hstore
Third, get that as some kind of string/text
Fourth is, how to then write that into another jsonb/hstore field using an update
Is this kind of regex update too much to get working in an update? i.e. update activity set details_jsonb = [[insane regex here]]? hstore is also an option (though I like that jsonb has types), so if it's easier to go to an hstore function like hstore(text[]) that's fine too.
Am I crazy and do I need to just write an external process not-in-postgresql that does this?
I would first split the single value into multiple lines. Each line can then be converted to an array from which this can be aggregated into a JSON object:
select string_to_array(regexp_replace(t.line, '(^\s+)|(\s+$)', '', 'g'), '=')
from activity a, regexp_split_to_table(a.detail, ',\s*\n') t (line)
This returns the following:
element
------------------------------------
{KeyID,[813]}
{"Key Name","[Name of Key]"}
{"Some Field",[2732]}
{"Another Field",[2751]}
{Description,"[A text string here]"}
{Location,[sometext]}
{"Other ID",[2360578]}
{}
The regex to split the detail value into lines might need some improvements though.
The regexp_replace(t.line, '(^\s+)|(\s+$)', '', 'g') is there trim the values before converting them to an array.
Now this can be aggregated into a single JSON value, or each line can be converted into a single hstore value (unfortunately there is no hstore_agg())
with activity (detail) as (
values (
'Key ID=[813],
Key Name=[Name of Key],
Some Field=[2732],
Another Field=[2751],
Description=[A text string here],
Location=[sometext],
Other ID=[2360578],
')
), elements (element) as (
select string_to_array(regexp_replace(t.line, '\s', ''), '=')
from activity a, regexp_split_to_table(a.detail, ',') t (line)
)
select json_agg(jsonb_object(element))
from elements
where cardinality(element) > 1 -- this removes the empty line
The above returns a JSON object:
[ { "KeyID" : "[813]" },
{ "Key Name" : "[Name of Key]" },
{ "Some Field" : "[2732]" },
{ "Another Field" : "[2751]" },
{ "Description" : "[A text string here]" },
{ "Location" : "[sometext]" },
{ "Other ID" : "[2360578]" }
]

Print key of list w/o knowing its position

I have a list that contains many keys:
mylist = {"a", "b", "c", "1", "2", "3", ...}
and I want to print the key for example that has value "x", without knowing it's exact position in the list. That mean I have to run the whole list and till "x" is found and print it. How could I do this? Seems easy question but it confuses me a bit... Thanks a lot
for key, value in pairs(mylist) do
if value == "x" then print(key) end
You can also create another mapping, eg.
mapping_list = {}
for key, value im pairs(mylist) do
mapping_list[value] = key
(assuming that list elements are unique) then, you'd be able to
print(mapping_list["x"])

Applescript (list choices)

I need som help with a small Applescript.
When i make my choice in the list. Safari always open test3. Maybe someone can help me?
The code
set choice to choose from list {"1", "2", "3"}
if choice is "1" then
tell application "Safari" to open location "http://test1/"
else if choice is "2" then
tell application "Safari" to open location "http://test2/"
else
tell application "Safari" to open location "http://test3/"
end if
The result from a "choose from list" dialog is a list. Even if it only has 1 item in the list it's still a list. So you really want "item 1" from the list to get the actual result.
To fix your code change your first line to this...
set choice to item 1 of (choose from list {"1", "2", "3"})
When you run your code within the Applescript Editor and you look at the Events/Replies area,
you will see that your selection is returned in braces - {"1"}. They indicate a list was returned back to you. This is especially useful if you wanted to choose multiple items from the list using the with multiple selections allowed statement.
So, your if then statements failed because you were comparing a list {"1"} with a string "1". Since all else failed, test3 was the only option.
To fix it, you can pick an item from the list returned, like this
set choice to item 1 of (choose from list {"1", "2", "3"})
or convert the selection to text so you are comparing apples with apples :)
set choice to (choose from list {"1", "2", "3"}) as text
Hope this helped.

How to use regex function in R to identify elements and create output message if elements exist

I am very new to R and not an experienced programmer so if my question sounds amateurish please forgive me. In short, I want to identify any characters other than integers in a vector and create an object with an output message "error" for any that are found.
Here is the code that I am working with:
##create dataframe
fireplaces <- data.frame(num.fireplaces <- c(0:5, "one", "two", "NA", "N/A", "zero", "none"), fireplace.exists <- c(TRUE, TRUE,TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
##use grep function to identify unwanted character strings (in this case, any element that is not an integer)
fail <- grep("[^[:digit:]]", (num.fireplaces), value=TRUE)
##use gsub function to replace unwanted strings with messaging. **Problem** messaging is repeated with each character in the string
gsub("[^[:digit:]]", "Error", (num.fireplaces), ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)
## This is the error message that I would like to show as output if non-integer elements are found in the vector "num.fireplaces"
"Error"
I realize that I am all over the place with this and may be a little out of my league. I'm hoping someone can help me with this.
I would recommend using ifelse and grepl:
ifelse(grepl("^[0-9]+",num.fireplaces),num.fireplaces,"Error")
[1] "0" "1" "2" "3" "4" "5" "Error" "Error" "Error" "Error" "Error" "Error"
Note however that your original regex didn't work the way you expected, so I replaced by a simpler example.

How to query MongoDB for matching documents where item is in document array

If my stored document looks like this:
doc = {
'Things' : [ 'one' , 'two' , 'three' ]
}
How can I query for documents which contain one in Things?
I know the $in operator queries a document item against a list, but this is kind of the reverse. Any help would be awesome.
Use MongoDB's multikeys support:
MongoDB provides an interesting "multikey" feature that can automatically index arrays of an object's values.
[...]
db.articles.find( { tags: 'april' } )
{"name" : "Warm Weather" , "author" : "Steve" ,
"tags" : ["weather","hot","record","april"] ,
"_id" : "497ce4051ca9ca6d3efca323"}
Basically, you don't have to worry about the array-ness of Things, MongoDB will take care of that for you; something like this in the MongoDB shell would work:
db.your_collection.find({ Things: 'one' })