FAME: How do I combine 2 data objects (each a list of series)? - database

I am not sure if anyone here is familiar with FAME as it is not commonly used.
But here is my code
DB="C:\..."
A_DATA="Profit.20?.FIRM_A"
B_DATA="Revenue.20?.FIRM_XX"
EXEC "OPEN <AC RE> """ + DB + """ AS OPENEDDATABASE" -- BASE STORING RAW DATA SERIES
NLIST = WILDLIST(OPENEDDATABASE,A_DATA)
NLIST2 = WILDLIST(OPENEDDATABASE,B_DATA)
I want to combine NLIST & NLIST2 into a single variable. But there doesn't seem any FAME function that allows me to do.
Put in another way, I need to have all the series named "Profit.20?.FIRM_A" and "Revenue.20?.FIRM_XX"
in 1 variable.
Thank you

unless I misunderstand something, namelists in FAME can simply be added together with the '+' operator:
fruits = {apples,oranges}
vegetables = {cabbage,carrots}
food = fruits + vegetables
type !food
apples,oranges,cabbage,carrots

Related

How to get value in loop by determine table field dynamically?

In SAP there is a table T552A. There are several fields like TPR, TTP, FTK, VAR, KNF as per day of a month such as TPR01, TPR02, etc.
In a loop I would like to access the said fields by determining the table field dynamically instead of hard coding of field name, like below:
DATA: ld_begda LIKE sy-datum,
ld_endda LIKE sy-datum.
DATA: lc_day(2) TYPE c.
DATA: lc_field(10) TYPE c.
DATA: lc_value TYPE i.
ld_begda = sy-datum.
ld_endda = ld_begda + 30.
WHILE ld_begda <= ld_endda.
lc_day = ld_begda+6(2).
CONCATENATE 't552a-tpr' lc_day INTO lc_field.
lc_value = &lc_field. " Need support at this point.
ld_begda = ld_begda + 1.
ENDWHILE.
Something like this (depending on the exact requirement):
FIELD-SYMBOLS: <lv_tpr> TYPE tprog.
DATA: ls_t552a TYPE t552a.
DATA: lv_field TYPE fieldname.
WHILE ld_begda <= ld_endda.
lv_field = |TPR| && ld_begda+6(2). "Dynamic field name
ASSIGN COMPONENT lv_field
OF STRUCTURE ls_t552a
TO <lv_tpr>.
IF sy-subrc EQ 0.
... "<lv_tpr> has now the value of the corresponding field
ENDIF.
ld_begda = ld_begda + 1.
ENDWHILE.
To store the result of a dynamic field a variable is needed that can store values of arbitrary types, in ABAP this is supported through field symbols.
A component of a structure (i.e. the row of the table) can then be assigned to a field symbol with ASSIGN COMPONENT:
ASSIGN COMPONENT lc_field OF STRUCTURE row_of_table TO FIELD-SYMBOL(<value>).
" work with <value> here
Recently new generic expressions were introduced (and now also support structures) which would allow you to write this (sooner or later):
... row_of_table-(lc_field) ...

Csv file to a Lua table and access the lines as new table or function()

Currently my code have simple tables containing the data needed for each object like this:
infantry = {class = "army", type = "human", power = 2}
cavalry = {class = "panzer", type = "motorized", power = 12}
battleship = {class = "navy", type = "motorized", power = 256}
I use the tables names as identifiers in various functions to have their values processed one by one as a function that is simply called to have access to the values.
Now I want to have this data stored in a spreadsheet (csv file) instead that looks something like this:
Name class type power
Infantry army human 2
Cavalry panzer motorized 12
Battleship navy motorized 256
The spreadsheet will not have more than 50 lines and I want to be able to increase columns in the future.
Tried a couple approaches from similar situation I found here but due to lacking skills I failed to access any values from the nested table. I think this is because I don't fully understand how the tables structure are after reading each line from the csv file to the table and therefore fail to print any values at all.
If there is a way to get the name,class,type,power from the table and use that line just as my old simple tables, I would appreciate having a educational example presented. Another approach could be to declare new tables from the csv that behaves exactly like my old simple tables, line by line from the csv file. I don't know if this is doable.
Using Lua 5.1
You can read the csv file in as a string . i will use a multi line string here to represent the csv.
gmatch with pattern [^\n]+ will return each row of the csv.
gmatch with pattern [^,]+ will return the value of each column from our given row.
if more rows or columns are added or if the columns are moved around we will still reliably convert then information as long as the first row has the header information.
The only column that can not move is the first one the Name column if that is moved it will change the key used to store the row in to the table.
Using gmatch and 2 patterns, [^,]+ and [^\n]+, you can separate the string into each row and column of the csv. Comments in the following code:
local csv = [[
Name,class,type,power
Infantry,army,human,2
Cavalry,panzer,motorized,12
Battleship,navy,motorized,256
]]
local items = {} -- Store our values here
local headers = {} --
local first = true
for line in csv:gmatch("[^\n]+") do
if first then -- this is to handle the first line and capture our headers.
local count = 1
for header in line:gmatch("[^,]+") do
headers[count] = header
count = count + 1
end
first = false -- set first to false to switch off the header block
else
local name
local i = 2 -- We start at 2 because we wont be increment for the header
for field in line:gmatch("[^,]+") do
name = name or field -- check if we know the name of our row
if items[name] then -- if the name is already in the items table then this is a field
items[name][headers[i]] = field -- assign our value at the header in the table with the given name.
i = i + 1
else -- if the name is not in the table we create a new index for it
items[name] = {}
end
end
end
end
Here is how you can load a csv using the I/O library:
-- Example of how to load the csv.
path = "some\\path\\to\\file.csv"
local f = assert(io.open(path))
local csv = f:read("*all")
f:close()
Alternative you can use io.lines(path) which would take the place of csv:gmatch("[^\n]+") in the for loop sections as well.
Here is an example of using the resulting table:
-- print table out
print("items = {")
for name, item in pairs(items) do
print(" " .. name .. " = { ")
for field, value in pairs(item) do
print(" " .. field .. " = ".. value .. ",")
end
print(" },")
end
print("}")
The output:
items = {
Infantry = {
type = human,
class = army,
power = 2,
},
Battleship = {
type = motorized,
class = navy,
power = 256,
},
Cavalry = {
type = motorized,
class = panzer,
power = 12,
},
}

Datatype of list - moscow ml

I would like to do representation of list of katalogs and list of books.
datatype catalog = KAT of string*catalog list| KIS of string*book list | EMPTY;
and I would like to count the books. I am trying to do something like this.
fun count([]) = 0
| count(book) = LIST.length(book)
| count(x1::xs) = count(x1) + count(xs);`
and I receive cannot have type error. What can I do to caunt books ?
I think you have to be more careful with the definition of functions. For example, it seems that you want to count all the books and the catalogs inside with the same function. This is not possible in ML, because although you can include several instances of the function for pattern matching, the matching has to be done over just one type.
For example, in your code, it seems that you want to use count() for counting either given a catalog or a list of books or catalogs. This can be done, but it is not the usual way in ML. You have to write a function to count all the catalogs in the catalog list of type catalog, and another function to count the books and catalogs. The length function works as expected, so the following function may work:
fun countcatalogs ([]) = 0
| countcatalogs(cat::rest) = countbooks(cat) + countcatalogs(rest)
and
countbooks (EMPTY) = 0
| countbooks (KIS(_, l)) = length(l)
| countbooks (KAT(_,cat::rest)) = countbooks(cat) + countcatalogs(rest);

bad use of variables in db.query

I'm trying to develop a blog using webpy.
def getThread(self,num):
myvar = dict(numero=num)
print myvar
que = self.datab.select('contenidos',vars=myvar,what='contentTitle,content,update',where="category LIKE %%s%" %numero)
return que
I've used some of the tips you answer in this web but I only get a
<type 'exceptions.NameError'> at /
global name 'numero' is not defined
Python C:\xampp\htdocs\webpy\functions.py in getThread, line 42
Web GET http://:8080/
...
I'm trying to make a selection of some categorized posts. There is a table with category name and id. There is a column in the content table which takes a string which will be formatted '1,2,3,5'.
Then the way I think I can select the correct entries with the LIKE statement and some %something% magic. But I have this problem.
I call the code from the .py file which builds the web, the import statement works properly
getThread is defined inside this class:
class categoria(object):
def __init__(self,datab,nombre):
self.nombre = nombre
self.datab = datab
self.n = str(self.getCat()) #making the integer to be a string
self.thread = self.getThread(self.n)
return self.thread
def getCat(self):
'''
returns the id of the categorie (integer)
'''
return self.datab.select('categorias',what='catId', where='catName = %r' %(self.nombre), limit=1)
Please check the correct syntax for db.select (http://webpy.org/cookbook/select), you should not format query with "%" because it makes code vulnerable to sql injections. Instead, put vars in dict and refer to them with $ in your query.
myvars = dict(category=1)
db.select('contenidos', what='contentTitle,content,`update`', where="category LIKE '%'+$category+'%'", vars=myvars)
Will produce this query:
SELECT contentTitle,content,`update` FROM contenidos WHERE category LIKE '%'+1+'%'
Note that I backquoted update because it is reserved word in SQL.

How to use named parameters inside LIKE operator pattern in Adobe Air

I wanted to use a named parameter placeholder inside the LIKE operator pattern so that the argument string is properly escaped.
Here's my modified code where I am using the at-param placeholder:
var stmt = new air.SQLStatement();
stmt.text = "SELECT * FROM comments WHERE title LIKE '%#search%';";
stmt.parameters["#search"] = "argument string";
stmt.execute();
Doing so yields an SQLError with the following details
message: Error #3315: SQL Error.
details: '#search' parameter name(s) found in parameters property but not in the SQL specified
As suggested by Mike Petty, I tried:
stmt.text = 'SELECT * FROM comments WHERE title LIKE "#%search%";';
Which yields to the same SQL Error details.
Documentation has this:
expr ::= (column-name | expr) LIKE pattern
pattern ::= '[ string | % | _ ]'
My suspicion is that it is skipped due to the qoutes, any ideas on how to make it work?
Found a solution for this, basically instead of doing it like this:
var stmt = new air.SQLStatement();
stmt.text = "SELECT * FROM comments WHERE title LIKE '%#search%';";
stmt.parameters["#search"] = "argument string";
stmt.execute();
You have to put a placeholder for the entire LIKE operator pattern and bind the pattern as a parameter.
var stmt = new air.SQLStatement();
stmt.text = "SELECT * FROM comments WHERE title LIKE #search;";
stmt.parameters["#search"] = "%" + userInput + "%";
stmt.execute();
Remove your :string from your string.
Like works just fine as:
SELECT * FROM comments WHERE comments.title LIKE '%string%';
It's hard to tell from your question, but is your current statement either throwing a SQLError, just doesn't compile, or just doesn't return any results?
If I had to guess I'd say you have a few issues here:
You shouldn't have to qualify the column in the where clause since you only have 1 table to select from
The parameter string is technically a reserved word by AIR / Actionscript (although the case is different) - still a bit confusing.
Even though the documentation and code allow you to use either the colon, or at-symbol, my preference is the # since it's a bit easier to see ;)
Don't forget to add an itemClass definition of the Class you expect for rows - this helps to avoid anonymous objects.
This should be fairly straight forward:
var stmt:SQLStatement = new SQLStatement();
stmt.itemClass = Comment;
stmt.text = 'SELECT * FROM comments WHERE title LIKE "#%search%";';
stmt.parameters['#search'] = userInput.text;
stmt.addEventListener(SQLEvent.RESULT, onResults);
stmt.execute();

Resources