Posts

Showing posts with the label string

Why is character in string not mutated?

Why is character in string not mutated? def test_problem(str) str[3].upcase! # str[3] = str[3].upcase! works str end p test_problem("hello") My question is why String.upcase! which is mutating method doesn't mutate string in a case above but you need to reassign that character in string? 2 Answers 2 String# returns a new string, as is documented. String# a = "foo" a.object_id # => 70217975553640 a[0].object_id # => 70217957574840 A string is not composed of character objects, it is a single object (at least on a superficial level, I'm not sure of the C internals). So there is no way to extract a character and still have it belong to the original string - you need to work with the string as a whole if you want to mutate it. String#= on the other hand does mutate the string String#= You could make your method like so: def test_problem(str) str[3] = str[3].upca...

R: How to concatenate vectors of strings in a list of vectors

R: How to concatenate vectors of strings in a list of vectors I have a list of vectors of strings like this: x=list(c("a","b"),"c",c("d","e","f"),c("gg","hh") ) I'd like to concatenate the vectors into single strings like this y=c("ab","c","def","gghh") I searched around but I couldn't find a similar example. Is there a smart way to do this without looping over the list elements? 2 Answers 2 With sapply : sapply y <- sapply(x, paste0, collapse = '') # [1] "ab" "c" "def" "gghh" You probably know this already, but FYI for those interested, paste0 and paste will give the same result in cases like this where there's only one argument (other than collapse ) – Ryan ...

Taking strings as a 2-D char array

Taking strings as a 2-D char array I want a code such that I enter some strings one-by-one (by pressing enter) and display it. for example; Input abc def Output abc def also I want this input to be in a array so that I can select any character from the array whenever I want. For example: s[1][1] gives 'e'. I have writen a code for this problem. #include <stdio.h> #include <stdlib.h> int main() { int i, j, n, m; scanf("%d%d", &n, &m); char a[n][m]; for (i = 0; i<n; i++) scanf("%s", a[i]); for (i = 0; i<n; i++) { printf("%s", a[i]); printf("n"); } } But for this code my input/output goes like this: Input ab cd Output abcd cd Can anyone tell where am I going wrong? The input you have shown in question is missing the input value of n and m . Show the input value you are giving to n and m . – H.S. Jun 2...

Deserializing C++ string in WCF service using Json converter

Deserializing C++ string in WCF service using Json converter I am trying to communicate with C# wcf service which takes josn as an input where json contains image data, i wrote C# client where I am converting the object (InputData) which contains image as byte to json and sending it to server. Client side: InputData RequestData; //copied image data into RequestData obj String jsonRequest = (new JavaScriptSerializer()).Serialize(RequestData); //converting obj to json where : public class InputData { private byte RawImage = null; public byte FrontImage { get return this.FrontRawImage; set if (null != value) { FrontRawImage = value.ToArray(); } } } Server side: //deserialize and get the object InputData requestData = JsonConvert.DeserializeObject<InputData>(jsonData); I am able to do this with C# client but the problem is, I have to communicate this WCF service from C++ client where I am convertin...

(Python/MySQL/JSON) Not enough arguments for format string

(Python/MySQL/JSON) Not enough arguments for format string Can anybody help me figure out what I'm doing stupidly? I'm attempting to propagate an SQL table with financial data from a json file. I get the error in the title, but I can't seem to figure out where it's coming from. import json import MySQLdb open_time = 0 openp = 0 high = 0 low = 0 closep = 0 volume = 0 close_time = 0 quoteassetvol = 0 trades = 0 ignore1 = 0 ignore2 = 0 ignore3 = 0 con = MySQLdb.connect(host='localhost',user='root',db='binance_adabtc',passwd='abcde') cur = con.cursor() symbols = ["(JSON_EXTRACT('json_obj','$[i][open_time]'), (JSON_EXTRACT('json_obj','$[i][openp]'), (JSON_EXTRACT('json_obj','$[i][high]'), (JSON_EXTRACT('json_obj','$[i][low]'), (JSON_EXTRACT('json_obj','$[i][closep]'),(JSON_EXTRACT('js...