I would like to know how one can append items to a list in boost build/b2/bjam.
I am sure this must be easy but the search engines do not provide a result. Also Ctrl+F in the documentation does not help!
The operator to use is += as in GNU Make.
Example:
MYLIST = a b ;
MYLIST += c ;
echo $(MYLIST) ;
Shows: a b c
And to concatenate lists:
MYLIST = a b c ;
OTHERLIST = dd ee ;
MYLIST += $(OTHERLIST) ;
echo $(MYLIST) ;
import sequence ;
echo length: [ sequence.length $(MYLIST) ] ;
Shows:
a b c dd ee length: 5
More information can be found in the Variables section of the documentation.
Related
I am failing to create a list of strings in KDB C++ API. This is what I am doing:
K lst = ktn(KC,0);
jk(&lst, kp((S)"str_1"));
jk(&lst, kp((S)"str_2"));
r1(lst);
k(h, (S)"{show type x}" , lst, (K)0);
r1(lst);
k(h, (S)"{show count x}", lst, (K)0);
r1(lst);
k(h, (S)"{show x}" , lst, (K)0);
The output
10h
2
"\260\260"
Where instead of "\260\260" could be some other random string repeated twice. It seems, I do create a list of strings, but the list contains rubbish for whatever reason. Could you please help me to understand what I am doing wrong here?
Thank you for your help!
You're initialising the list with type char (KC), so this will be a char list, not a string list.
A string list is a mixed type list, as it a list of list of chars in Kdb. Therefore, you need to initialise the list with type 0 (mixed list type).
Example C code - strList.c:
#define KXVER 3
#include "k.h"
K createStrListStatic(K x){
K strList = ktn(0,2);
kK(strList)[0] = kp("hello");
kK(strList)[1] = kp("world");
return strList;
}
K createStrListDynamic(K x){
K strList = ktn(0,1);
kK(strList)[0] = kp("hello");
js(&strList, (S)kp("world"));
return strList;
}
Example Q code to load the lib & call C funcs - strList.q:
createStrListStatic:`:strList 2:(`createStrListStatic;1);
createStrListDynamic:`:strList 2:(`createStrListDynamic;1);
-1 "\ncreateStrListStatic";
-1 "\t",.Q.s1 createStrListStatic[];
-1 "\ncreateStrListDynamic";
-1 "\t",.Q.s1 createStrListDynamic[];
Compile and run as follows:
$ gcc -shared -m32 -fPIC strList.c -o strList.so && LD_LIBRARY_PATH="." q strList.q
KDB+ 3.4 2016.10.27 Copyright (C) 1993-2016 Kx Systems
l32/ 8()core 7982MB salih glyph01 127.0.1.1 NONEXPIRE
createStrListStatic
("hello";"world")
createStrListDynamic
("hello";"world")
I am trying to concatenate three string variables.
Data X ;
a = "A" ;
b = "B" ;
c = "C" ;
z = catx ( '0D0A'x, a, b, c ) ;
run;
I am trying to display the string values like this in the final dataset, so that the values appear one below the other -
A
B
C
But by using '0D0A'x option, the string appears as ABC. I have to display the z variable in an excel. If I had to output the same into a HTML file then I would have used "\n" as an option in CATX function. Is there a way where I can introduce new line characters.
I adapted your example slightly:
libname test excel "%sysfunc(pathname(work))\text.xls";
Data test.X ;
a = "A" ;
b = "B" ;
c = "C" ;
z = catx ( '0D0A'x, a, b, c ) ;
run;
libname test clear;
x "explorer ""%sysfunc(pathname(work))\text.xls""";
If you use this approach, you get a value in cell D2 which contains the line breaks as expected. However, in order for them to display correctly, you have to enable the 'wrap text' option for the cell formatting.
I am fairly new to R so please go easy on me if this is a stupid question.
I have a dataframe called foo:
< head(foo)
Old.Clone.Name New.Clone.Name File
1 A Aa A_mask_MF_final_IS2_SAEE7-1_02.nrrd
2 B Bb B_mask_MF_final_IS2ViaIS2h_SADQ15-1_02.nrrd
3 C Cc C_mask_MF_final_IS2ViaIS2h_SAEC16-1_02.nrrd
4 D Dd D_mask_MF_final_IS2ViaIS2h_SAEJ6-1_02.nrrd
5 E Ee F_mask_MF_final_IS2_SAED9-1_02.nrrd
6 F Ff F_mask_MF_final_IS2ViaIS2h_SAGP3-1_02.nrrd
I want to extract codes from the File column that match the regular expression (S[A-Z]{3}[0-9]{1,2}-[0-9]_02), to give me:
SAEE7-1_02
SADQ15-1_02
SAEC16-1_02
SAEJ6-1_02
SAED9-1_02
SAGP3-1_02
I then want to use these codes to search another directory for other files that contain the same code.
I fail, however, at the first hurdle and cannot extract the codes from that column of the data frame.
I have tried:
library('stringr')
str_extract(foo[3],regex("(S[A-Z]{3}[0-9]{1,2}-[0-9]_02)", ignore_case = TRUE))
but this just returns [1] NA.
Am I simply missing something obvious? I look forward to cracking this with a bit of help from the community.
Hello if you are reading the data as a table file then foo[3] is a list and str_extract does not accept lists, only strings, then you should use lapply to extract the match of every element.
lapply(foo[3], function(x) str_extract(x, "[sS][a-zA-Z]{3}[0-9]{1,2}-[0-9]_02"))
Result:
[1] "SAEE7-1_02" "SADQ15-1_02" "SAEC16-1_02" "SAEJ6-1_02" "SAED9-1_02"
[6] "SAGP3-1_02"
str_extract(foo[3],"(?i)S[A-Z]{3}[0-9]{1,2}-[0-9]_02")
seems to work. Somehow, my R gave me
"Error in check_pattern(pattern, string) : could not find function "regex""
when using your original expression.
The following code will repeat what you asked (just copy and paste to your R console):
library(stringr)
foo = scan(what='')
Old.Clone.Name New.Clone.Name File
A Aa A_mask_MF_final_IS2_SAEE7-1_02.nrrd
B Bb B_mask_MF_final_IS2ViaIS2h_SADQ15-1_02.nrrd
C Cc C_mask_MF_final_IS2ViaIS2h_SAEC16-1_02.nrrd
D Dd D_mask_MF_final_IS2ViaIS2h_SAEJ6-1_02.nrrd
E Ee F_mask_MF_final_IS2_SAED9-1_02.nrrd
F Ff F_mask_MF_final_IS2ViaIS2h_SAGP3-1_02.nrrd
foo = matrix(foo,ncol=3,byrow=T)
colnames(foo)=foo[1,]
foo = foo[-1,]
foo
str_extract(foo[,3],regex("(S[A-Z]{3}[0-9]{1,2}-[0-9]_02)", ignore_case = T))
The reason you get NULL is hidden: R stores entries by column, hence foo[3] is the 3rd row and 1st column of foo matrix/data frame. To quote the third column, you may need to use foo[,3]. or foo<-data.frame(foo); foo[[3]].
I have a 2D vector of string as
vector<vector<string>> database
it has following data
database[0] -> A C D (database[0][0] -> A , database[0][1] -> C and so on)
database[1] -> B C E F
database[2] -> A B C E F
database[3] -> B E
database[4] -> A C F
I am counting the occurrence of each string (in this example each character A,B etc.) and saving it in a map map_c as
map<string,int> map_c ;
for(i=0 ; i<database.size() ; i++)
{
for(j=0 ; j<database.at(i).size() ; j++)
{
if(map_c.find(database.at(i).at(j)) != map_c.end())
{
map_c[database[i][j]]++;
}
else
{
map_c[database[i][j]] = 1;
}
}
}
And printing the count of each string using the code
for(map<string,int>::iterator it = map_c.begin() ; it != map_c.end() ; it++ )
{
cout << it->first << " -> " << it->second << endl;
}
Output ->
-> 1
A -> 3
B -> 3
C -> 4
D -> 1
E -> 3
F -> 3
Why NULL key has been created with a count 1 ?
The code you post should not generate an empty (or spaces) string. It is possible such a string comes with your initial data.
Either way, your code to fill the map can be made much much shorter:
map<string,int> map_c ;
for(const auto& line: database)
for(const auto& s: line)
++map_c[s];
Replace your if...else with just ++map_c[database[i][j]]; . The default value for int in a map is 0.
The unexpected entry in your output is because your database actually had that entry in it. If you are not sure why this entry is in your database, review the code that sets up your database (and/or post that code).
Sorry , finaly i got rid the problem. Problem was in the input file. At the end of the file , there was a space character at last line and i was thinking that my file has been over before it . That space was creating the problem. I am sorry for inconvenience and thank you so much for your replies and time .
I have some aligned data (something bioinformatic related) as so:
reference_string = 'yearning'
string2 = 'learning'
string3 = 'aligning'
I need to extract only columns showing differences in relation to the reference data.
The output should show only positional information of the columns containing differences in relation to the reference string and the corresponding reference item.
1 2 3 4
y e a r
l
a l i g
My current code does most things okay except that it also reports columns with no difference.
string1 = 'yearning'
string2 = 'learning'
string3 = 'aligning'
string_list = [string1, string2]
reference = reference_string
diffs_top, diffs = [], []
all_diffs = set()
for s in string_list:
diffs = []
for i, c in enumerate(s):
if s[i] != reference[i]:
diffs.append(i)
all_diffs.add(i)
diffs_top.append(diffs)
for d in all_diffs:
print str(int(d+1)),
print
for c in reference:
print str(c),
print
for i, s in enumerate(string_list):
for j, c in enumerate(s):
if j in diffs_top[i]:
print str(c),
else:
print str(' '),
print
This code would give:
1 2 3 4
y e a r n i n g
l
a l i g
Any help appreciated.
EDIT: I have picked some section of real data to make the problem as clearer as possible and my attempt at solving it thus far:
reference_string = 'MAHEWGPQRLAGGQPQAS'
string1 = 'MAQQWSLQRLAGRHPQDS'
string2 = 'MAQRWGAHRLTGGQLQDT'
string3 = 'MAQRWGPHALSGVQAQDA'
string_list = [string1, string2, string3]
reference = reference_string
diffs_top, diffs = [], []
all_diffs = set()
for s in string_list:
diffs = []
for i, c in enumerate(s):
if s[i] != reference[i]:
diffs.append(i)
all_diffs.add(i)
diffs_top.append(diffs)
#print diffs_top
#print all_diffs
for d in all_diffs:
print str(int(d+1)), # retains natural positions of the reference residues
print
for d in all_diffs:
for i, c in enumerate(reference):
if i == d:
print c,
print
The print out will be an output showing the position at which there is any difference to other non-reference strings and the corresponding reference letter.
3 4 6 7 8 9 11 13 14 15 17 18
H E G P Q R A G Q P A S
Then the next step is to write a code that will process non reference strings by printing out the difference with the reference (at that position). If there is no difference it will leave blank (' ').
Doing it manually the output will be:
3 4 6 7 8 9 11 13 14 15 17 18
H E G P Q R A G Q P A S
Q Q S L R H D
Q R A H T L D T
Q R H A S V A D A
My entire code as an attempt to get to the solution above as been messy to say the least:
reference_string = 'MAHEWGPQRLAGGQPQAS'
string1 = 'MAQQWSLQRLAGRHPQDS'
string2 = 'MAQRWGAHRLTGGQLQDT'
string3 = 'MAQRWGPHALSGVQAQDA'
string_list = [string1, string2, string3]
reference = reference_string
diffs_top, diffs = [], []
all_diffs = set()
for s in string_list:
diffs = []
for i, c in enumerate(s):
if s[i] != reference[i]:
diffs.append(i)
all_diffs.add(i)
diffs_top.append(diffs)
#print diffs_top
#print all_diffs
for d in all_diffs:
print str(int(d+1)),
print
for d in all_diffs:
for i, c in enumerate(reference):
if i == d:
print c,
print
# this is my attempt to look into non-reference strings
# to check for the difference with the reference, and print an output.
for d in all_diffs:
for i, s in enumerate(string_list):
for j, c in enumerate(s):
if j == d:
print c,
else:
print str(' '),
print
Your code is working perfectly fine (as per your logic).
What is happening , is that while printing the output, when you come across the reference string, Python looks for the corresponding entry in the diffs_top list and because while storing in diff_top, you have no entry stored for the reference string, Python just prints blank spaces for your reference string.
1 2 3 4
y e a r n i n g #prints the reference string, because you've coded in that way
#prints blank as string_list[0] and reference string are the same
l
a l i g
The question here is how exactly do you define your difference for reference string.
Besides, I also found some fundamental flaws in your code implementation. If you try to run your code by setting string_list[1] as your reference string, you would get your output as :
1 2 3 4
l e a r n i n g
y
a l i g
Is this what you need? Please spend some time in properly defining difference for all cases and then try to implement you code.
EDIT:
As per you updated requirements, replace the last block in your code with this:
for i, s in enumerate(string_list):
for d in all_diffs:
if d in diffs_top[i]:
print s[d],
else:
print ' ',
print
Cheers!
I think there is a general problem in your logic. If you need to extract only columns showing difference in relation to the reference data and string1 is the reference the output should be:
1 2 3 4
l
a l i g
So, 'yearning' shouldn't show any character because it has no difference to string1.
If you delete or put the following lines in comments, you will exactly get what I expect is the right answer:
#for c in reference:
# print str(c),
#print
Consider to review your logic if this solution is not what you actually want.
Update
Here is a shorter solution which solves your task:
from itertools import compress, izip_longest
def delta(reference, string):
return [ '' if a == b else b for a, b in izip_longest(reference, string)]
ref_string = 'MAHEWGPQRLAGGQPQAS'
strings = ['MAQQWSLQRLAGRHPQDS',
'MAQRWGAHRLTGGQLQDT',
'MAQRWGPHALSGVQAQDA']
delta_strings = [delta(ref_string, string) for string in strings]
selectors = [1 if any(tup) else 0 for tup in izip_longest(*delta_strings)]
indices = [str(i+1) for i in range(len(selectors))]
output_data = [indices, ref_string] + delta_strings
for line in output_data:
print ''.join(x.rjust(3) for x in compress(line, selectors))
Explanation:
I defined a function delta(reference, string) which returns the delta between the string and the referenced string. For example: delta("ABFF", "AECF") returns the list ['', E, C, ''].
The variable delta_strings holds all the deltas between each string in the list strings and the reference string ref_string.
The variable selector is a list containing only 1 and 0 values, where 0 specifies the collumns which shouldn't be printed and vice versa.