Elixir integer list to string causes (UnicodeConversionError) invalid code point - string-interpolation

The problem
I need to create a string interpolating a list of integers in it.
"""
SomeQuery {
someQuery(articleIds: #{inspect article_ids}) {
edges {
node {
id
}
}
}
}
"""
Failing example
For example, the list [725553234] makes the example above to fail:
article_ids = [725553234]
"""
SomeQuery {
someQuery(articleIds: #{article_ids}) {
edges {
node {
id
}
}
}
}
"""
** (exit) an exception was raised:
** (UnicodeConversionError) invalid code point 725553234
(elixir) lib/list.ex:839: List.to_string/1
(commsapp_api) lib/my_project/client.ex:70: CommsappApi.News.Clients.CommunicationMs.Client.articles_feed/3
System
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:8:8] [ds:8:8:10]
[async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Elixir 1.6.3 (compiled with OTP 20)
Tried solutions
It tried the following:
Using inspect is not working: articleIds: #{inspect(article_ids)}
Using the IO.inspect with the :char_lists opt with :as_list: IO.inspect(article_ids, char_lists: :as_lists
Trying to join the integer list as string with: articleIds: [#{Enum.join(article_ids, ", ")}]
Interpolating the integers parsed to string with: Enum.map(article_ids, &Integer.to_string/1) |> Enum.join(", ")
I tried using a single line instead of a multiline string, not working
Many things I don't remember after trying different solutions... >.<
Guesses
The problem comes when using the brackets in the string, Elixir treats the interporlation as a list and raises the error because it cannot find the codepoints.
Ideas?
Thanks in advance!

In the documentation on strings, binaries and charlists, the charlists are defined as a list of point codes, i.e. integers. The code
article_ids = [725553234]
"#{article_ids}"
attempts to print the character which point code is 725553234. This point code is not defined and you get the error. Replace 725553234 by 65 and you should get an A character.
To interpolate your list of integers, you may want to do things like this:
iex(5)> a=[65, 66, 67]
'ABC'
iex(6)> "#{Enum.map(a, fn(c) -> Integer.to_string(c)<>" " end)}"
"65 66 67 "
BTW, if you look at list [65, 66, 67] is interpreted as 'ABC'.

By using Enum.join return string and separate integer by ","
Enum.join(list, ",")
or using Enum.map return as list by changing every integer value to string
Enum.map(list, &Integer.to_string/1)
list = [1, 2, 3, 4]
Enum.join(list, ",") -> "1,2,3,4"
Enum.map(list, &Integer.to_string/1) -> ["1", "2", "3", "4"]

Related

r json mongodb query $in operator syntax error due to double quotes?

I'm building a json query to pass to a mongodb database in R.
In one scenario, I have a vector of dates and I want to query the database to return all records which have a date in the relevant field that matches a date in my vector of dates.
The second scenario is the same as the first, but this time I have a vector of character strings (IDs) and need to return all the records with matching IDs.
I understood the correct way to do this in a json query is to use the $in operator, and then put my vector in an array.
However, when I pass the query to my mongodb database, the exportLogId returns NULL. I'm quite sure that the problem is something to do with how I am representing the $in operator in the final query, since I have very similarly structured queries without the $in operator and they are all working. If I look for just one of my target dates or character strings, I get the desired result.
I followed the mongodb manual here to construct my query, and the only issue I can see is that the $in operator in the output of jsonlite::toJSON() is enclosed in double quotes; whereas I think it might need to be in single quotes (or no quotes at all, but I don't know how to write the syntax for that).
I'm creating my query in two steps:
Create the query as a series of nested lists
Convert the list object to json with jsonlite::toJSON()
Here is my code:
# Load libraries:
library(jsonlite)
# Create list of example dates to query in mongodb format:
sampledates <- c("2022-08-11T00:00:00.000Z",
"2022-08-15T00:00:00.000Z",
"2022-08-16T00:00:00.000Z",
"2022-08-17T00:00:00.000Z",
"2022-08-19T00:00:00.000Z")
# Create query as a list object:
query_list_l <- list(filter =
# Add where clause:
list(where =
# Filter results by list of sample dates:
list(dateSampleTaken = list('$in' = sampledates),
# Define format of column names and values:
useDbColumns = "true",
dontTranslateValues = "true",
jsonReplaceUndefinedWithNull = "true"),
# Define columns to return:
fields = c("id",
"updatedAt",
"person.visualId",
"labName",
"sampleIdentifier",
"dateSampleTaken",
"sequence.hasSequence")))
# Convert list object to JSON:
query_json = jsonlite::toJSON(x = query_list_l,
pretty = TRUE,
auto_unbox = TRUE)
The JSON query now looks like this:
> query_json
{
"filter": {
"where": {
"dateSampleTaken": {
"$in": ["2022-08-11T00:00:00.000Z", "2022-08-15T00:00:00.000Z", "2022-08-16T00:00:00.000Z", "2022-08-17T00:00:00.000Z", "2022-08-19T00:00:00.000Z"]
},
"useDbColumns": "true",
"dontTranslateValues": "true",
"jsonReplaceUndefinedWithNull": "true"
},
"fields": ["id", "updatedAt", "person.visualId", "labName", "sampleIdentifier", "dateSampleTaken", "sequence.hasSequence"]
}
}
As you can see, $in is now enclosed in double quotes, even though I put it in single quotes when I created the query as a list object. I have tried replacing with sprintf() but that just adds a lot of backslashes to my query. I also tried:
query_fixed <- gsub(pattern = "\\"\\$\\in\\"",
replacement = "\\'$in\\'",
x = query_json)
... but this fails with an error.
I would be very grateful to know if:
The syntax problem that is preventing $in from working is actually the double quotes?
If double quotes is the problem, how do I replace them with single quotes without messing up the JSON format?
UPDATE:
The issue seems to occur when R is passing the query to the database, but I still can't work out exactly why.
If I try the query out in loopback explorer in the database, it works and using the export log ID produced, I can then fetch the results with httr::GET() in R. Example query results are shown below (sorry for the hashes - the main point is you can see the format of the returned values):
[1] "[{\"_id\":\"e59953b6-a106-4b69-9e25-1c54eef5264a\",\"updatedAt\":\"2022-09-12T20:08:39.554Z\",\"dateSampleTaken\":\"2022-08-16T00:00:00.000Z\",\"labName\":\"LNG_REFERENCE_DATA_CATEGORY_LAB_NAME_LAB_A\",\"sampleIdentifier\":\"LS0044-SCV2-PCR\",\"sequence\":{\"hasSequence\":false},\"person\":{\"visualId\":\"C-2022-0002\"}},{\"_id\":\"af5cd9cc-4813-4194-b60b-7d130bae47bc\",\"updatedAt\":\"2022-09-12T20:11:07.467Z\",\"dateSampleTaken\":\"2022-08-17T00:00:00.000Z\",\"labName\":\"LNG_REFERENCE_DATA_CATEGORY_LAB_NAME_LAB_A\",\"sampleIdentifier\":\"LS0061-SCV2-PCR\",\"sequence\":{\"hasSequence\":false},\"person\":{\"visualId\":\"C-2022-0003\"}},{\"_id\":\"b5930079-8d57-43a8-85c0-c95f7e0338d9\",\"updatedAt\":\"2022-09-12T20:13:54.378Z\",\"dateSampleTaken\":\"2022-08-16T00:00:00.000Z\",\"labName\":\"LNG_REFERENCE_DATA_CATEGORY_LAB_NAME_LAB_A\",\"sampleIdentifier\":\"LS0043-SCV2-PCR\",\"sequence\":{\"hasSequence\":false},\"person\":{\"visualId\":\"C-2022-0004\"}}]"

Need to change an element of a multidimensional string vector in C++

I have a multidimensional string vector, gameState. The contents of gameState can be seen below. I would like to "move" A up one space by making [2][1] = " " and [1][1] = "A". But I'm getting an error (error: invalid conversion from 'const char*' to '__gnu_cxx::__alloc_traitsstd::allocator<char, char>::value_type' {aka 'char'} [-fpermissive]). Not sure if there is a way to use vector.at() in this case. Here is a snippet. The full code reads in the initial game state from a text file and assigns it to the string vector gameState. I get the row and col indexes for each letter in the grid below and assign them to variables (eg. Arow and Acol). I'm very new to C++, so any help would be appreciated. If you have any questions about the rest of the code, I'd be happy to elaborate. Thanks in advance.
int Arow;
int Acol;
vector<string> gameState;
string tempU = gameState[Arow-1][Acol];
if (tempU != "#") {
// if no wall is up, move up
gameState[Arow][Acol] = " ";
gameState[Arow-1][Acol] = "A";
Arow = Arow-1;
Amovesequence.push_back("U");
}
###########
#...#P...B#
#A#.$.###*#
#...#D...R#
###########
The type of gameState[Arow-1][Acol] is a char, not a string. Modify the code accordingly, e.g.: char tempU = ... and replace " " with ' 'etc.

Concat 2 strings erlang and send with http

I'm trying to concat 2 variables Address and Payload. After that I want to send them with http to a server but I have 2 problems. When i try to concat the 2 variables with a delimiter ';' it doesn't work. Also sending the data of Payload or Address doesn't work. This is my code:
handle_rx(Gateway, #link{devaddr=DevAddr}=Link, #rxdata{port=Port, data= RxData }, RxQ)->
Data = base64:encode(RxData),
Devaddr = base64:encode(DevAddr),
TextAddr="Device address: ",
TextPayload="Payload: ",
Address = string:concat(TextAddr, Devaddr),
Payload = string:concat(TextPayload, Data),
Json=string:join([Address,Payload], "; "),
file:write_file("/tmp/foo.txt", io_lib:fwrite("~s.\n", [Json] )),
inets:start(),
ssl:start(),
httpc:request(post, {"http://192.168.0.121/apiv1/lorapacket/rx", [], "application/x-www-form-urlencoded", Address },[],[]),
ok;
handle_rx(_Gateway, _Link, RxData, _RxQ) ->
{error, {unexpected_data, RxData}}.
I have no errors that I can show you. When I write Address or Payload individually to the file it works but sending doesn't work...
Thank you for your help!
When i try to concat the 2 variables with a delimiter ';' it doesn't work.
5> string:join(["hello", <<"world">>], ";").
[104,101,108,108,111,59|<<"world">>]
6> string:join(["hello", "world"], ";").
"hello;world"
base64:encode() returns a binary, yet string:join() requires string arguments. You can do this:
7> string:join(["hello", binary_to_list(<<"world">>)], ";").
"hello;world"
Response to comment:
In erlang the string "abc" is equivalent to the list [97,98,99]. However, the binary syntax <<"abc">> is not equivalent to <<[97,98,99]>>, rather the binary syntax <<"abc">> is special short hand notation for the binary <<97, 98, 99>>.
Therefore, if you write:
Address = [97,98,99].
then the code:
Bin = <<Address>>.
after variable substitution becomes:
Bin = <<[97,98,99]>>.
and that isn't legal binary syntax.
If you need to convert a string/list contained in a variable, like Address, to a binary, you use list_to_binary(Address)--not <<Address>>.
In your code here:
Json = string:join([binary_to_list(<<Address>>),
binary_to_list(<<Pa‌​yload>>)],
";").
Address and Payload were previously assigned the return value of string:concat(), which returns a string, so there is no reason to (attempt) to convert Address to a binary with <<Address>>, then immediately convert the binary back to a string with binary_to_list(). Instead, you would just write:
Json = string:join(Address, Payload, ";")
The problem with your original code is that you called string:concat() with a string as the first argument and a binary as the second argument--yet string:concat() takes two string arguments. You can use binary_to_list() to convert a binary to the string that you need for the second argument.
Sorry I'm new to Erlang
As with any language, you have to study the basics and write numerous toy examples before you can start writing code that actually does something.
You don't have to concatenate strings. It is called iolist and is one of best things in Erlang:
1> RxData = "Hello World!", DevAddr = "Earth",
1> Data = base64:encode(RxData), Devaddr = base64:encode(DevAddr),
1> TextAddr="Device address", TextPayload="Payload",
1> Json=["{'", TextAddr, "': '", Devaddr, "', '", TextPayload, "': '", Data, "'}"].
["{'","Device address","': '",<<"RWFydGg=">>,"', '",
"Payload","': '",<<"SGVsbG8gV29ybGQh">>,"'}"]
2> file:write_file("/tmp/foo.txt", Json).
ok
3> file:read_file("/tmp/foo.txt").
{ok,<<"{'Device address': 'RWFydGg=', 'Payload': 'SGVsbG8gV29ybGQh'}">>}

awk; comparing time -- puzzling behavior of strftime

Use GNU-Awk (gawk) in UNXUTILS on a Win-7 PC. This question deals with strftime(.) and a comparison of times using that.
Have followed the discussion on how to compare strftime values because I have a similar problem. In financial market data I have a date-time string ($25) given as "03-APR-2006 09:55:25" (so time = substr($25, 13, 8)) and my objective is to count records (especially order cancellation records) that come in after 14:00:00 (2 pm).
In my code I have a line which reads
{ if ($3==3) {
{ ++CK_NR}
{ ++CO_NR[$4, substr($25, 1, 11)] }
{ if (strftime(substr($25, 13, 8)) > ("14:00:00"))\
{
{ ++CK_LATE_NR }
{ ++CO_LATE_NR[$4, substr($25, 1, 11)] }
}
}
}}
Just realized that the inequality I used -- if (strftime(substr($25, 13, 8)) > ("14:00:00")) -- has only a string in the RHS, and I didn't make this string the argument of another strftime(.). What's puzzling is that it did NOT give me an error.
Am concerned that while it has not generated any errors, and the code has run, perhaps it is giving me something other than what I intended with the code. In a Command Prompt Window, doing
gawk "{ print (strftime(\"09:55:25\") > (\"14:00:00\")) }"
does yield "0" and
gawk "{ print (strftime(\"09:55:25\") < (\"14:00:00\")) }"
does yield "1". The GNU Awk Manual (http://www.gnu.org/software/gawk/manual/gawk.html#Time-Functions) yields little information on what is required for a meaningful comparison. Just now tried the above deleting the "strftime" even from the LHS, as under
gawk "{ print ((\"09:55:25\") > (\"14:00:00\")) }"
and
gawk "{ print ((\"09:55:25\") > (\"14:00:00\")) }"
and got the same results. I want to be sure that I am getting the correct True/False result because GAWK is comparing time, and not for some other internal rule it uses in making string comparisons (causing the limited test to be only a coincidence). Can someone resolve this puzzle? Thanks. Best,
Murgie
You seem to be using string comparisons. Strings are compared by comparing the first character of each, then the second character of each, and so on. Thus, "10" is less than "9", since the ASCII value of "1" is less than that of "0", see http://www.gnu.org/software/gawk/manual/gawk.html#Variable-Typing and http://en.wikibooks.org/wiki/An_Awk_Primer/Search_Patterns_%282%29#Logic_Operators
If you want to do numeric comparison for strings on the form "xx:yy:zz" (for instance "10:22:45") then you can try to convert the string to a number first using gsub(/:/,"",str)

Cannot Solve "index was outside the bounds of the array"

I am working in C# with ASP.NET. I am familiar with this error but this time I can't solve it.
I have text in a drop-down list like this:
राम कुमार सिंह 8s2w8r
here राम कुमार सिंह is the name in HINDI while 8s2w8r is users' ID.
I need to separate these two values and need to pass them as session variables. The logic I am using is depicted in the code.
public string reverse(string s)
{
char []temp=s.ToCharArray();
Array.Reverse(temp);
return (temp.ToString());
}
string dropdowntextreversed=reverse(DropDownList1.Text);
char []delim=new char[]{' '};
string []parts=dropdowntextreversed.Split(delim,2);
string family_head_uid = reverse(parts[0]);
string family_head = reverse(parts[1]);
Session.Add("family_head", family_head);
Session.Add("family_head_uid", family_head_uid);
Response.Redirect("/WebForm1.aspx");
I always get an error as the index was outside the bounds of the array! I don't understand this because I am breaking the string into 2 parts so it should have parts[0] and parts[1]. Please suggest...
You are splitting the string into MAXIMUM 2 parts, but if there's only one you will get probably one part.
Read this documentation
Try to assert that parts.Length is == 2 or to access elemnts only there atre two elements
Try this link. As I think there is a problem in the temp.ToString() which will return System.Char[] rather than the value which are you looking for. Use string.join instead will work.
Use the following reverse method:
public string reverse(string s)
{
return String.Join(String.Empty, s.ToCharArray().Reverse());
}

Resources