Split a string on sqlite - string

i don't know sqlite but I have to implement a database already done. I'm programming with Corona SDK. The problem: i have a column called "answers" in this format: House,40|Bed,20|Mirror,10 ecc.
I want to split the string and remove "," "|" like this:
VARIABLE A=House
VARIABLE A1=40
VARIABLE B=Bed
VARIABLE B1=20
VARIABLE C=Mirror
VARIABLE C1=10
I'm sorry for my english. Thanks to everybody.

Try this:
If you want to simply remove the characters, then you can use the following:
Update 3 :
local myString = "House;home;flat,40|Bed;bunk,20|Mirror,10"
local myTable = {}
local tempTable = {}
local count_1 = 0
for word in string.gmatch(myString, "([^,|]+)") do
myTable[#myTable+1]=word
count_1=count_1+1
tempTable[count_1] = {} -- Multi Dimensional Array
local count_2 = 0
for word_ in string.gmatch(myTable[#myTable], "([^,|,;]+)") do
count_2=count_2+1
local str_ = word_
tempTable[count_1][count_2] = str_
--print(count_1.."|"..count_2.."|"..str_)
end
end
print("------------------------")
local myTable = {} -- Resetting my table, just for using it again :)
for i=1,count_1 do
for j=1,#tempTable[i] do
print("tempTable["..i.."]["..j.."] = "..tempTable[i][j])
if(j==1)then myTable[i] = tempTable[i][j] end
end
end
print("------------------------")
for i=1,#myTable do
print("myTable["..i.."] = "..myTable[i])
end
--[[ So now you will have a multidimensional array tempTable with
elements as:
tempTable = {{House,home,flat},
{40},
{Bed,bunk},
{20},
{Mirror},
{10}}
So you can simply take any random/desired value from each.
I am taking any of the 3 from the string "House,home,flat" and
assigning it to var1 below:
--]]
var1 = tempTable[1][math.random(3)]
print("var1 ="..var1)
-- So, as per your need, you can check var1 as:
for i=1,#tempTable[1] do -- #tempTable[1] means the count of array 'tempTable[1]'
if(var1==tempTable[1][i])then
print("Ok")
break;
end
end
----------------------------------------------------------------
-- Here you can print myTable(if needed) --
----------------------------------------------------------------
for i=1,#myTable do
print("myTable["..i.."]="..myTable[i])
end
--[[ The output is as follows:
myTable[1]=House
myTable[2]=40
myTable[3]=Bed
myTable[4]=20
myTable[5]=Mirror
myTable[6]=10
Is it is what you are looking for..?
]]--
----------------------------------------------------------------
Keep coding............. :)

Related

Jupyter Notebook HOw can i assign a default value to datepicker and sort the time within the selected date?

I can try to set condition some data that is within the time picker in the datepicker. I got two problem
MY code was
def show_filter_date(start ,end):
print(start_dater.value)
print(end_dater.value)
time_df = (id_one[(id_one['_source.timestampstring']>pd.to_datetime(start))&(id_one['_source.timestampstring']<pd.to_datetime(end))])
#print(time_df.head(20))
# time_df = (id_one[(id_one['_source.timestampstring']>pd.to_datetime(start_dater.value))&(id_one['_source.timestampstring']<pd.to_datetime(end_dater.value))])
time_df.head(20)
layout = widgets.Layout(width='auto', height='40px')
start_dater = widgets.DatePicker(description='Pick a Start Date',disabled=False)
end_dater = widgets.DatePicker(description='Pick an End Date',disabled=False )
#display(widgets.HBox((start_dater, end_dater)))
#display(start_dater)
#display(end_dater)
#id_one.head()
#combine_date = widgets.HBox((start = start_dater, end = end_dater))
#country_selector = widgets.Dropdown(
interact(show_filter_date,start = start_dater , end = end_dater)
everytime i run the code it show Error
"Invalid comparison between dtype=datetime64[ns] and NoneType"
I have tried to assign default value like
start_dater = widgets.DatePicker(description='Pick a Start Date',disabled=False, year = 2020 ,month = 12, day = 1)
but it won't change to 2020/12/01
So, how can I get a value other than null?
I fail in interact for the datepicker in which
A) print(time_df.head(20))
B)
time_df = (id_one[(id_one['_source.timestampstring']>pd.to_datetime(start_dater.value))&(id_one['_source.timestampstring']<pd.to_datetime(end_dater.value))])
time_df.head(20)
Only (A) can be "interact" or "refresh" when I pick a day but not (B)
And for Question 2, when I put time_df.head(20) in the NEXT CELL it does work tho.......
But what i want is to show the result like in time_df
I would appreciate if any help
the id_one is something like
Index _source.hdrrId _source.hdrfId _source.hdrType \
199 1300 1234 1
_source.timestampstring
199 2020-11-06 09:36:04.800
Thanks!
Jeff
I replicated your first error with
id_one = pd.DataFrame(pd.date_range('20200101','20200202'), columns = ['_source.timestampstring'])
This is because you did not set a default value for the DatePicker. The value is None by default hence the error. Here is the fix (value argument is the default value):
start_dater = widgets.DatePicker(description='Pick a Start Date',disabled=False, value = datetime.date(2020,1,1))
end_dater = widgets.DatePicker(description='Pick an End Date',disabled=False, value = datetime.date(2020,2,1))
I cannot replicate your second error. My guess is you put print before the time_df = statement. You need to put the print after the line that calculates time_df

Using STUFF Function

I've this table TableA that have these fields: [intIdEntidad],[intIdEjercicio],[idTipoGrupoCons]. The tableA look like for idTipoGrupoCons = 16 this image
enter image description here
I'm trying to use STUFF function to show the column intIdEjercicio separated by coma, something like this;
enter image description here
This is query I'm using to obtain result the above image:
SELECT DISTINCT o.idTipoGrupoCons, o.intIdEntidad, ejercicios= STUFF((
SELECT ', ' + CONVERT(VARCHAR,a.intIdEjercicio)
FROM dbo.[tbEntidades_Privadas_InfoAdicionalGrupo] AS a
WHERE a.idTipoGrupoCons = 16
FOR XML PATH, TYPE).value(N'.[1]', N'varchar(max)'), 1, 2, '')
FROM [tbEntidades_Privadas_InfoAdicionalGrupo] AS o
JOIN tbEntidades_Privadas p On O.intIdEntidad = p.intIdEntidad
WHERE o.idTipoGrupoCons = 16
The result isn't correct, because I execute this query for idTipoGrupoCons = 16
SELECT [idTipoGrupoCons], [intIdEntidad],[intIdEjercicio]
FROM [tbEntidades_Privadas_InfoAdicionalGrupo] A
WHERE A.idTipoGrupoCons = 16
The result is this
enter image description here
It's means that for intIdEntidad = 50 intIdEjercicio is just 7 and for intIdEntidad = 45 intIdEjercicio = 2 and 4
I suppose that the problem is that I need to add a subquery to or a function into STUFF or in the outer WHERE to add condition to intIdEntidad each time to call STUFF function.
I've read about the use of CROSS APPLY and perhaps it can be used to solve the problem
Here is the answer.
The problem was that need to join tableA with the same table into the STUFF function. At the end the query look like this:
SELECT t1.idTipoGrupoCons, t1.intIdEntidad,
,ejercicios = STUFF(
(SELECT ', ' + t3.Ejercicio
FROM [tbEntidades_Privadas_InfoAdicionalGrupo] t2
JOIN tbMtoNoRegistro_Ejercicios t3 ON t2.intIdEjercicio = e.intEjercicio
WHERE t2.idTipoGrupoCons = t1.idTipoGrupoCons
AND t2.intIdEntidad = t1.intIdEntidad
ORDER BY t3.Ejercicio
FOR XML PATH ('')
)
,1,2,'')
FROM [tbEntidades_Privadas_InfoAdicionalGrupo] t1
JOIN tbEntidades_Privadas p ON t1.intIdEntidad = p.intIdEntidad
WHERE t1.idTipoGrupoCons = 17
GROUP BY t1.idTipoGrupoCons,t1.intIdEntidad, p.strDenominacionSocial

How to DON'T return string in quotes

I have list:
itemid = ['113222408782', '113223652945', '113222268092', '113223761722', '113222277037', '113223676589', '113214024190', '113227956444', '113222400375', '113222383960', '113223749386', '113213898511', '113223653433', '113214060057', '113212059543', '113223647852', '113212403974', '113222230789', '113212110156', '113213917508', '113223748917', '113212088893', '113213936773', '113212282559', '113222369037', '113223645004', '113214034011', '113223647208', '113222397481', '113212052765', '113212136602', '113212037895', '113222210185', '113223752305', '113212049744', '113212400978', '113212274566', '113218830085', '113203034623', '113222199167', '113223648988', '113223646543', '113223651519', '113222200831', '113213996789', '113214000484', '113213890605', '113222232853', '113222298617', '113223753658', '113222238111', '113194336951', '113223631876', '113222242464', '113212123303', '113222215450', '113214000567', '113223642160', '113223639750', '113214060070', '113223644511', '113194332243', '113212139900', '113222207007', '113222374260', '113223719876', '113194339799', '113223677943', '113212417158', '113212433693', '113227977319', '113223607151', '113212409228', '113215809743', '113214051350']
This list contains 75 values. I'm cutting this list for 20 items list using following method:
while len(itemid) > 0:
slice = itertools.islice(itemid, 20)
ha = []
for x in slice:
ha.append(format(x))
var1 = ','.join(ha)
var1 returns string with 20 values like this:
113222408782,113223652945,113222268092,113223761722,113222277037,113223676589,113214024190,113227956444,113222400375,113222383960,113223749386,113213898511,113223653433,113214060057,113212059543,113223647852,113212403974,113222230789,113212110156,113213917508
And then I got stuck:
I'm using it for eBay api and I want to do the following command:
api_request = {'ItemID': var1}
and it returns:
{'ItemID': '113223748917,113212088893,113213936773,113212282559,113222369037,113223645004,113214034011,113223647208,113222397481,113212052765,113212136602,113212037895,113222210185,113223752305,113212049744,113212400978,113212274566,113218830085,113203034623,113222199167'}
But I need to return the string var1 without quotes like this:
{'ItemID': 113223748917,113212088893,113213936773,113212282559,113222369037,113223645004,113214034011,113223647208,113222397481,113212052765,113212136602,113212037895,113222210185,113223752305,113212049744,113212400978,113212274566,113218830085,113203034623,113222199167}
How can I do this?
The quotes are not actually part of the string, they just signify that it is a string. Therefore you cannot remove them
You does not need to convert array to string, you have to pass the entire object like this:
var1 = itertools.islice(itemid, 20)
api_request = {'ItemID': var1}
Why are you trying to cast it to a format if you need to pass it as a array?
Regards,
Igor Quirino

How do you use lua in redis to return usable result to nodejs

one of the module i am implementing for my mobile app api is to get all outstanding notifications from , submitting username.
i used a list called username:notifications to store all outstanding id of notifications.
For example, in my test case, ['9','10',11'] is the result after calling for
lrange username:notifications 0 -1
So i wrote a lua script to get lrange and for each result,
hgetall notification:id
And for some reason, lua could not send the table, result to nodejs in usable state. Wondering if anyone
has a solution for multiple hgetall requests and returning them to nodejs
Here goes the rest of the code:
-- #KEYS: "username"
-- #ARGV: username
-- gets all fields from a hash as a dictionary
local hgetall = function (key)
local bulk = redis.call('HGETALL', key)
local result = {}
local nextkey
for i, v in ipairs(bulk) do
if i % 2 == 1 then
nextkey = v
else
result[nextkey] = v
end
end
end
local result = {}
local fields = redis.call('LRANGE' , ARGV[1], 0,-1)
for i, field in ipairs(fields) do
result[field] = hgetall('notification:'..field)
end
return result
You cannot return a "dictionary" from a Lua script, it is not a valid Redis type (see here).
What you can do is something like this:
local result = {}
local fields = redis.call('LRANGE' , ARGV[1], 0, -1)
for i=1,#fields do
local t = hgetall('notification:' .. fields[i])
result[#result+1] = fields[i]
result[#result+1] = #t/2
for j=1,#t do
result[#result+1] = t[j]
end
end
return result
The result is a simple list with this format:
[ field_1, nb_pairs_1, pairs..., field_2, nb_pairs_2, ... ]
You will need to decode it in your Node program.
EDIT: there is another solution, probably simpler in your case: encode the result in JSON and return it as a string.
Just replace the last line of your code by:
return cjson.encode(result)
and decode from JSON in your Node code.

Writing data to excel files, MATLAB

I am using MatlabR2011a on my Windows 7 machine.
I have a folder of 635 text files. Each file contains multiple rows and 2 columns. I am trying to loop through the folder and read each file and write these values to excel. So far this is what I have:
close all; clc;
dirname = uigetdir;
Files = dir(fullfile(dirname,'*.txt'))
for k = 1:635
j =1
filename = fullfile(dirname,Files(k).name);
fid = fopen(filename);
x = fgetl(fid)
while ischar(x)
x = fgetl(fid)
a2 = strtrim(x);
a3 = textscan(a2,'%s');
a4 = a3{1}{1};
a5= a3{1}{2};
pos3 = strcat('A',num2str(j));
xlswrite('sample_output',{a4,a5},'Sheet1',pos3)
j = j+1;
end
fclose(fid);
end
It keeps giving me the following error after finishing reading the first file.
Error using ==> strtrim Input should be a string or a cell array of strings.
A sample input file looks like this:
15076 4636259
15707 4636299
15714 1781552
15721 4204950
15730 2174919
16209 4636510
16413 4758572
16470 4445808
17519 311397
17667 2116489
17739 1729694
18024 3210756
18627 3714194
18695 4192858
19141 632766
19318 1923574
19438 1255216
19493 4635020
19771 4770250
How can I fix this? Would appreciate help with this!
In the while loop, cut the line
x=fgetl(fid)
...and paste it before the end statement right after j=j+1.
The error occurs because you get a line before the while statement, then you use the while statement to test validity of the string (all fine so far), but then immediately get the subsequent line before you perform your string operation. The call to fgetl at the start of the while block could return an EOF, causing subsequent string manipulation functions to fail.
Also... The code would be more robust if you set your for loop like so
for k=1:numel(Files)
Since all your data are numeric this should work. Give it a try.
dirname = uigetdir;
Files = dir(fullfile(dirname,'*.txt'))
j =0;
for k = 1:numel(Files)
filename = fullfile(dirname,Files(k).name);
x = dlmread(filename,'\t'); %# I assume tab-delimiter
j = j + size(x, 1);
xlswrite( 'sample_output', x, 'Sheet1',sprintf('A%d',j) )
end

Resources