>> description
=> [" name=\"margin-top\" content=\"1\" />\r\n", " name=\"oreintation\" content=\"horizontal\" />\r\n", " name=\"paper-height\" content=\"6\" />\r\n", " name=\"paper-width\" content=\"5\" />\r\n", " name=\"margin-left\" content=\"3\" />\r\n"]
description is of type array
I need key as margin-top value 1.
I need key as oreintation value oreintation and so on, how to get this done?
Use
result = {}
description.each{|val|
result[val.split("\"")[1]] = val.split("\"")[3]
}
result # contains expected hash
Related
I am generating a json inside a stored procedure as like
Declare res CLOB(5M);
Set res = (values (json_array(select json_object…
Json Looks like
[{pk: 1, name1: xyz, name: 2}, {pk: 2, name1: cvc, name2: vcc}]
At the end I Need the Information what is the length of the json, means How many entries do it have, beginning from Root.
I need something like this,
Declare counter SMALLINT;
Set Counter = xyz —should be 2
So How can I find out from res, that there are two rows?
--# SET TERMINATOR #
-- Create a generic function to deal with JSON arrays
CREATE OR REPLACE FUNCTION UNNEST_JSON (P_DOC CLOB(1M), P_PATH VARCHAR(128))
RETURNS TABLE
(
INDEX INT
, ITEM CLOB (1M)
)
DETERMINISTIC
NO EXTERNAL ACTION
BEGIN
DECLARE L_IDX INT DEFAULT 0;
L1:
WHILE TRUE DO
IF NOT JSON_EXISTS (P_DOC, P_PATH || '[' || L_IDX || ']') THEN LEAVE L1; END IF;
PIPE (L_IDX, JSON_QUERY (P_DOC, P_PATH || '[' || L_IDX || ']'));
SET L_IDX = L_IDX + 1;
END WHILE L1;
RETURN;
END
#
SELECT COUNT (1) AS ELEM_NUM
FROM TABLE (UNNEST_JSON (
-- You give your unnamed array some name you will refer in the 2-nd arg
JSON_OBJECT
(
KEY 'items'
VALUE
JSON_ARRAY
(
JSON_OBJECT (KEY 'pk' VALUE 1, KEY 'name1' VALUE 'xyz', KEY 'name' VALUE 2) FORMAT JSON
, JSON_OBJECT (KEY 'pk' VALUE 2, KEY 'name1' VALUE 'cvc', KEY 'name2' VALUE 'vcc') FORMAT JSON
)
FORMAT JSON
)
, '$.items'
))
#
ELEM_NUM
2
Anyone tell me why this doesn't work?
GET_TABLE {1=ID}
key = string.format("%q", GET_TABLE[1])
RETURN_TABLE[key] = "ss"
print(RETURN_TABLE[ID])
print(GET_TABLE[1])
First print result: nil. Second print result: ID
I want the first print result to be: ss
GET_TABLE {1=ID}
key = "ID"
RETURN_TABLE[key] = "ss"
print(RETURN_TABLE[ID])
print(GET_TABLE[1])
The above works fine so I assume its due to the string.format not working right?
The %q format token returns the input as an escaped and quoted Lua string. This means that given the input ID it will return "ID" (the double quotes being part of the string!) which is a different string. (Or, represented as Lua strings, the input is 'ID' and the return value is '"ID"'.)
You have therefore set the ID key while trying to retrieve the "ID" key (which presumably does not exist).
> x = 'ID'
> =x
ID
> =string.format('%q', x)
"ID"
> =#x
2
> =#string.format('%q', x)
4
Your code does not compile (you need [] around the index), and you should use the raw string of ID, not the "quoted" string:
GET_TABLE = {[1]=ID}
key = string.format("%s", GET_TABLE[1])
Note that I had to initialize ID and RETURN_TABLE objects to the following:
ID = 'ID'
RETURN_TABLE = {}
Stylistic note: you should only use all-caps names for constants, otherwise too many makes code hard to read
I have a array of string and I want to show in my view all the elements separate by ", ".
When I do a each of this array, How to know if the current element is the last one?
Something like this?
- categoryName.each do |name|
- if (name.last) -- the last elemnt of array
= name
- else
= name + ", "
any idea!
Try to use Array.join
= categoryName.join(', ')
You can simply call categoryName.join(', ') which connects all names in your array with the specified string. It does nothing if only one value is in the array and just returns the single name.
You can use Enumerable#each_with_index for that :
- categoryName.each_with_index do |name, i|
- if (i == categoryName.length - 1) -- the last elemnt of array
= name
- else
= name + ", "
If you are using Rails, you can try this.
categoryName.to_sentence
It will automatically do what you are trying to.
How do you return an array from a sqlite database in lua?
I have this sameple code here:
function getMoveName()
tempMoveName = {}
for row in db:nrows("SELECT * FROM movetable") do
tempMoveName = row.movename .. " " .. row.totalcubicfeet .. " " .. row.totalitem .. "\n"
end
return tempMoveName
end
which will return the content of the database and then print the content with this line of code:
local displaymovenames = mydatabase.getMoveName()
print ( displaymovenames )
however it only returns the last data and not all of the contents of it.
What you are doing is for every row, you just store the row data in the variable tempMoveName and hence overwriting the previous value.
You need to add the rowData to the table tempMoveName.
function getMoveName()
tempMoveName = {}
for row in db:nrows("SELECT * FROM movetable") do
local rowData = row.movename .. " " .. row.totalcubicfeet.." "..row.totalitem.."\n"
tempMoveName[#tempMoveName+1] = rowData
end
return tempMoveName
end
EDIT
To access the table elements you have to do the following
for i=1,#tempMoveName do
print(tempMoveName[i])
end
note that #tempMoveName gives the length of the table(i.e no. of elements in the table)
P.S if you are going to do a lot of coding in Lua, I'd suggest you get a grip on the basics of Tables, cos table is the main datatype of Lua. Arrays, lists, dictionaries, classes and almost everything are implemented via tables. Here is a tutorial for a start!
This has nothing to do with SQL or databases; this is basic Lua stuff here.
This:
tempMoveName = {}
creates a table and stores the table into the (global) variable named tempMoveName.
This:
tempMoveName = row.movename .. " " .. row.totalcubicfeet .. " " .. row.totalitem .. "\n"
Creates a big string from concatenating other strings (don't forget about string.format) and stores that in the (global) variable named tempMoveName.
Note what I said: "stores it in the variable". Not "in the table stored in the variable".
It's no different from doing this:
tempMoveName = 1
tempMoveName = "foo"
This doesn't combine a string with an integer in some way. tempMoveName held 1, then it was replaced with "foo".
Tables aren't special; they're values just like anything else in Lua. Variables hold values. So if you change which value is stored in a variable, you have changed which value is stored there. You haven't affected the value itself, only where it is stored.
If you have a table stored in a variable, and you want to build an array, you should access the elements within the table, not the variable itself. This is done in the usual way:
tempMoveName[#tempMoveName + 1] = row.movename .. " " .. row.totalcubicfeet .. " " .. row.totalitem .. "\n"
Alternatively, you can use table.insert:
table.insert(tempMoveName, row.movename .. " " .. row.totalcubicfeet .. " " .. row.totalitem .. "\n")
Lastly, if tempMoveName is meant to be temporary (as the name suggests), then you should declare it as a local variable.
I have a forms collection (fc) and I'm attempting to append to an email the values of the 'key' and the 'value'. I'm having no problem with the key (newKey), but I can't seem to code the 'value' properly. The 'for' loop checks to see if the key's first 3 characters of the key are 'ddl' indicating it came from a dropdownlist. if so, the loop should append the value from the dropdownlist control (the value of the key-value pair). (If not, the loop calls another method to append either a yes or no based upon the value of a checkbox control) Thanks in advance.
//Append new key-value pairs implemented since legacy keys
for (int i = 0; i < newKeys.Length; i++ )
{
//Checks for prefix of element to determine type of element
if(newKeys[i].Substring(0,3) == "ddl"){
sb.Append(newKeys[i] + ": " + fc.GetValue(newKeys[i]) + "\",<br />");
sb.Append(newKeys[i] + ": " + fc.GetValues(newKeys[i].ToString()) + "\",<br />");
}
else{
sb.Append(newKeys[i] + ",\"" + Boolean(fc[newKeys[i]]) + "\",<br />");
}
}
The 2 sb.append commands return the following:
ddlStratacacheConstellationManagerRole: System.Web.Mvc.ValueProviderResult"
ddlStratacacheConstellationManagerRole: System.String[]",
The values you are writing aren't of type String, nor they have the ToString() method overridden.
That's why the standard object.ToString() method is called and the object's type name is appended to the string.
To remedy this, you either need to override the ToString() method of all possible form collection's values, or think of some other algorithm, which would iterate over collections and output the corresponding values.