To create a trigger before insert using Informix database.
When we try to insert a record into the table it should insert random alphanumeric string into one of the field. Are there any built in functions?
The table consists of the following fields:
empid serial NOT NULL
age int
empcode varchar(10)
and I am running
insert into employee(age) values(10);
The expected output should be something as below:
id age empcode
1, 10, asf123*
Any help is appreciated.
As already commented there is no existing function to create a random string however it is possible to generate random numbers and then convert these to characters. To create the random numbers you can either create a UDR wrapper to a C function such as random() or register the excompat datablade and use the dbms_random_random() function.
Here is an example of a user-defined function that uses the dbs_random_random() function to generate a string of ASCII alphanumeric characters:
create function random_string()
returning varchar(10)
define s varchar(10);
define i, n int;
let s = "";
for i = 1 to 10
let n = mod(abs(dbms_random_random()), 62);
if (n < 10)
then
let n = n + 48;
elif (n < 36)
then
let n = n + 55;
else
let n = n + 61;
end if
let s = s || chr(n);
end for
return s;
end function;
This function can then be called from an insert trigger to populate the empcode column of your table.
I have defined both records and created a union from them, but F# still complains that the constructor "Food" is not defined. What is the problem?
type Product = {Name : string; BasePrice: int}
type Size = {Medium: int; Large: int}
type Food = | Product of Product| Size of Size
let food = Food({Name = "Bagel"; BasePrice = 20}; {Medium = 10; Large = 20})
Food is a type, not a constructor - to create a value of type Food you need to use one of the constructors Product or Size. It looks like you're trying to construct a list Food, in which case you can use:
let food = [Product {Name = "Bagel"; BasePrice = 20}; Size {Medium = 10; Large = 20}]
Okay so I'm lost on this one. I have a list of objects. Each object as a non-unique ID in it. I want to group on this ID but for the life of me I can't figure out how to do this.
This is what I have
type fooObject = {
Id : int
Info : string
}
let fooObjects: fooObject list
The data might look something like this
[ { Id = 1 ; Data = "foo" } ; { Id = 1 ; Data = "also foo" } ; { Id = 2 ; Data = "Not foo" } ]
I would like something like
let fooObjectsGroupedById : fooObject list list
So the final result would look like this
[ [{ Id = 1 ; Data = "foo" } ; { Id = 1 ; Data = "also foo" } ] ; [{ Id = 2 ; Data = "Not foo" }]]
Use List.groupBy:
let groupById fooObjects =
List.groupBy (fun foo -> foo.Id) fooObjects
|> List.map snd
how would I go about counting the number of times a string occurs in a table?
Basically I have a table that is like, say 200 entires for example (but it is larger)... each entry has a sub entry called name.
so..
itemlist[i].name == somestring.
Now I can search and find a match pretty easy using an if statment while looping though the table...
if string.find(string.lower(itemlist[i].name), string.lower(searchString)) ~= nil then
So say I'm searching for Thomas, it will return when it finds "Thomas F Malone".
The thing is some cases there are more than one result for the search value.. for example.. say there are three different names that all start with Thomas.
At the moment it will just find the 1st occurrence of the Thomas.
So the plan is to count all the occurrences of Thomas and then output all of them... but I can not work out how to get the numeric value of how many times the result is found in the table.
TL;DR - How can I count the number of occurrences that of a string in a table?
When you found the matching values, store them in a temporary table, e.g.
table.insert(temporaryTable, value)
rather than quitting the function using return. You can find below, a sample function which gathers and counts the occurence of a query string in a variable inside of a multidimensional table (or see it in action here).
--data
itemList = {
{name = "Denny Kuhlman", id = "6688"},
{name = "Russell Leisy", id = "3751"},
{name = "Hilario Stermer", id = "1886"},
{name = "Thomas Hemming", id = "9666"},
{name = "Samuel Lafuente", id = "8232"},
{name = "Lazaro Ashby", id = "5274"},
{name = "Ronnie Nicosia", id = "9664"},
{name = "Edison Seyal", id = "1344"},
{name = "Jerald Officer", id = "9497"},
{name = "Lupe Burdge", id = "266"},
{name = "Stephan Iler", id = "5968"},
{name = "Josue Stephens", id = "2128"},
{name = "Salvador Ortmann", id = "3643"},
{name = "Tony Ricker", id = "8799"},
{name = "Corey Carbone", id = "6485"},
{name = "Conrad Theberge", id = "139"},
{name = "Arnulfo Oquendo", id = "2861"},
{name = "Damien Balsley", id = "5572"},
{name = "Efren Sloop", id = "7106"},
{name = "Blair Clagon", id = "614"},
{name = "Dario Service", id = "1411"},
{name = "Paul Ashalintubbi", id = "3403"},
{name = "Felix Veal", id = "1539"},
{name = "Laurence Caskey", id = "2827"},
{name = "Will Ranallo", id = "8463"},
{name = "Thomas Brenner", id = "9599"},
{name = "Claudio Hallmark", id = "6265"},
{name = "Nolan Haslett", id = "9661"},
{name = "Lenard Pereira", id = "5652"},
{name = "Dusty Duer", id = "4034"},
}
--
function countStringOccurence(query, itemList)
query = string.lower(query)
--if query string is found, store index of the itemList in table searchResult
local searchResult = {}
for i, item in ipairs(itemList) do
local name = string.lower(item.name)
if string.find(name, query) then
table.insert(searchResult, i)
end
end
--return both the occurence count and the list of found item
return #searchResult, searchResult
end
--execute the function
count, foundItemList = countStringOccurence("thomas", itemList)
--print results
print(count) --> 2
for i, index in ipairs(foundItemList) do
print(index, itemList[index].name) --> 4 Thomas Hemming
--> 26 Thomas Brenner
end
NB: Please note that if your table/list entries might be moved around (e.g. sort), it might not be advisable to just store the array index because the references/values collected in foundItemList might become incorrect.
Just don't stop iterating over the table once you found something. Keep on going and increment a counter variable every time you find your string or put your results into a table and count its elements later.
local texts = {"a123", "b213", "a332", "d411", "a124"}
local result = {}
for i,v in ipairs(texts) do
if string.find(v, "a") then
table.insert(result, v)
end
end
print(#result)
I'm using F# alongside a JSON data-store making use of the JSON.NET library. I'm trying to utilise F# structures and types where possible and have run into the following issue. Say I wish to store the following data structure,
type A = {
id : int
name : string
posts : string list
}
Creation works fine, but to update just the stored name field I need to send a JSON record that omits the posts field. Using the empty list won't work as the persistence system will assume that I wish to replace the existing posts with an empty list and thus overwrite them. From the JSON.NET docs I've read a field can be omitted from serialisation by setting it to null,
let updatedEntry = { id : 0, name : "Fred", posts = null }
However the F# compiler will give an error stating that the type list can not be set to null. Is there anyway to accomplish this from within F#, perhaps an attribute I'm unaware of? Thanks
There are two ways you could do this easily:
Option 1
Use the System.Collections.Generic.List type, which can be null:
> type A = {id: int; name:string; posts: System.Collections.Generic.List<string> };;
type A =
{id: int;
name: string;
posts: System.Collections.Generic.List<string>;}
> let a = {id=5; name="hello"; posts=null};;
val a : A = {id = 5;
name = "hello";
posts = null;}
Option 2
The other, more idiomatic way, would be to use the Option type:
> type A = {id: int; name:string; posts: string list option };;
type A =
{id: int;
name: string;
posts: string list option;}
> let a = {id=5; name="there"; posts=None};;
val a : A = {id = 5;
name = "there";
posts = null;}
Note that you'd compare the posts member to None rather than null.
Handy reading: Option types
Edit
(After some searching & experimentation) you could use boxing to still use F# types as the values:
> type A = {id: int; name:string; posts: System.Object };;
type A =
{id: int;
name: string;
posts: Object;}
> let a = {id=5; name="foo"; posts=null};;
val a : A = {id = 5;
name = "foo";
posts = null;}
> let b = {id=6; name="bar"; posts=(box [])};;
val b : A = {id = 6;
name = "bar";
posts = [];}
But I'd stick with the Option type, personally