Suppose I have an array of strings "31,793.1" "29,798.6" "30,455.7" "29,700.9"
How do I replace , with nothing to give me "31793.1" "29798.6" "30455.7" "29,700.9"
Another example is to replace - in "-5" "-3" "-4" with _ to give "_5" "_3" "_4" .
"31,793.1" "29,798.6" "30,455.7" "29,700.9" would not be an "array of strings" in J. I will suppose that you have a line like this and you want to end up with an array of numbers:
data =: '"31,793.1" "29,798.6" "30,455.7" "29,700.9" "-5"'
NB. Convert commas to "null" and '-'s to '_'s
NB. rplc works in pairs 'old';'new'
data rplc ',';'';'-';'_'
"31793.1" "29798.6" "30455.7" "29700.9" "_5"
NB. remove '"'s
data rplc '"';''
31793.1 29798.6 30455.7 29700.9 _5
Normally now you would have to split on whitespace (there are many ways to do this) but converting to numbers using ". takes care of this here:
NB. ". data
31793.1 29798.6 30455.7 29700.9 _5
+/ data
121743
Related
I got some issues trying to split a sentence in single substrings. Using the split function, I can't manage to use more than a delimiter. The code is:
sentence = "If you're visiting this page, you're likely here because you're searching for a random sentence"
split(sentence, "," , " " , ";")
I get this error:
LoadError: MethodError: no method matching split(::String, ::String, ::String, ::String).
I would like to get an array of single words.
Provide a vector of Chars as the split argument (in Julia quotation " is used for String and apostrophe ' for Char):
julia> split(sentence,[' ',',',';'])
16-element Vector{SubString{String}}:
"If"
"you're"
"visiting"
"this"
"page"
""
"you're"
"likely"
"here"
"because"
"you're"
"searching"
"for"
"a"
"random"
"sentence"
I am searching for a way to use a formatter to put a space between two characters. i thought it would be easy with a string formatter.
here is what i am trying to accomplish:
given: "AB" it will produce "A B"
Here is what i have tried so far:
"AB".format("%#s")
but this keep returning "AB" i want "A B". i thought the number sign could be used for space.
i also tried this:
"26".format("%#d") but its still prints "26"
is there anyway to do this with string.formatter.
It is kind of possible with the string formatter although not directly with a pattern.
jshell> String.format("%1$c %2$c", "AB".chars().boxed().toArray())
$10 ==> "A B"
We need to turn the string into an object array so it can be passed in as varargs and the formatter pattern can extract characters based on index (1$ and 2$) and format them as characters (c).
A much simpler regex solution is the following which scales to any number of characters:
jshell> "ABC^&*123".replaceAll(".", "$0 ").trim()
$3 ==> "A B C ^ & * 1 2 3"
All single characters are replaced with them-self ($0) followed by a space. Then the last extra space is removed with the trim() call.
I could not find way to do this using String#format. But here is a way to accomplish this using regex replacement:
String input = "AB";
String output = input.replaceAll("(?<=[A-Z])(?=[A-Z])", " ");
System.out.println(output);
The regex pattern (?<=[A-Z])(?=[A-Z]) will match every position in between two capital letters, and interpolate a space at that point. The above script prints:
A B
I am learning the lua programming from a online book.
It talks about string indices for an array
If the indices are strings, you can create a single index
concatenating both indices with a character in between to separate
them. For instance, you can index a matrix m with string indices s and
t with the code m[s..':'..t], provided that both s and t do not
contain colons (otherwise, pairs like ("a:", "b") and ("a", ":b")
would collapse into a single index "a::b"). When in doubt, you can use
a control character like `\0ยด to separate the indices.
https://www.lua.org/pil/11.2.html
I don't understand what is wrong with index "a::b". What is the difference between "a:b" and "a::b".
What's the trick behind it?
What the documentation you linked is describing, is a way to represent a multidimensional matrix in a single dimensional table. After giving an example of how you can do this with number indices:
mt = {} -- create the matrix
for i=1,N do
for j=1,M do
mt[i*M + j] = 0
end
end
they describe a way to do the same thing with strings: "If the indices are strings, you can create a single index concatenating both indices with a character in between to separate them." A code snippet that would fit the description would look like:
str_idxs = {"foo", "bar", "baz"} -- table of the string indices
mt = {} -- matrix
for 1,N do
for 1,M do
mt[str_idxs[N] .. ":" .. str_idxs[M]] = 0
end
end
print(mt["foo:bar"]) -- 0
print(mt["foo" .. ":" .. "bar"]) -- 0
print(mt["foo::bar"]) -- nil
As you can see in this example there is nothing special about the ":" character, you can choose any string to be a separator (including "::" if you really wanted). The reason why "foo::bar" is wrong in this case is because you never gave a value to "foo::bar."
How to print a string inside a string?
I have row data like this
City_name
Abcde/def/Report_names/names
Now i want to print only that string which is after report_names
Abcde/def length may vary from line to line.
Report_names/names is standard naming format.
So i want to print only text appearing after /report_names/
For simple exercises like this, it is best to learn and then use standard string functions, INSTR and SUBSTR. instr will find the position of the first letter of a substring you are searching for within a longer string. So if you search for 'Report_names/' you will find the position of R. The first letter of "names" is 13 positions to the right of that. That will be the first letter of the substring you actually want, which is the "names". With this you should be able to understand what the query below does:
-- begin TEST DATA (not part of the solution to the problem)
with
test_data ( str ) as (
select 'City_name Abcde/def/Report_names/Ann, Helen, Mary' from dual
)
-- end of TEST DATA; SQL query begins BELOW THIS LINE
-- use your actual table and column names instead of "test_data" and "str"
select str, substr( str, instr(str, 'Report_names/') + 13 ) as names
from test_data
;
STR NAMES
------------------------------------------------- --------------------
City_name Abcde/def/Report_names/Ann, Helen, Mary Ann, Helen, Mary
1 row selected.
I am having two problems while working in Lisp and I can't find any tutorials or sites that explain this. How do you split up a string into its individual characters? And how would I be able to change those characters into their corresponding ASCII values? If anyone knows any sites or tutorial videos explaining these, they would be greatly appreciated.
CL-USER 87 > (coerce "abc" 'list)
(#\a #\b #\c)
CL-USER 88 > (map 'list #'char-code "abc")
(97 98 99)
Get the Common Lisp Quick Reference.
A Lisp string is already split into its characters, in a way. It is a vector of characters, and depending upon what you need to do, you can use either whole string operations on it, or any operations applicable to vectors (like all the operations of the sequence protocol) to handle the individual characters.
split-string splits string into substrings based on the regular expression separators
Each match for separators defines a splitting point; the substrings between splitting points are made into a list, which is returned. If omit-nulls is nil (or omitted), the result contains null strings whenever there are two consecutive matches for separators, or a match is adjacent to the beginning or end of string. If omit-nulls is t, these null strings are omitted from the result. If separators is nil (or omitted), the default is the value of split-string-default-separators.
As a special case, when separators is nil (or omitted), null strings are always omitted
from the result. Thus:
(split-string " two words ") -> ("two" "words")
The result is not ("" "two" "words" ""), which would rarely be useful. If you need
such a result, use an explicit value for separators:
(split-string " two words " split-string-default-separators) -> ("" "two" "words" "")
More examples:
(split-string "Soup is good food" "o") -> ("S" "up is g" "" "d f" "" "d")
(split-string "Soup is good food" "o" t) -> ("S" "up is g" "d f" "d")
(split-string "Soup is good food" "o+") -> ("S" "up is g" "d f" "d")
You can also use elt or aref to get specific characters out of a string.
One of the best sites for an in-depth introduction to Common Lisp is the site for the Practical Common Lisp book (link to the section on numbers, chars and strings). The whole book is available online for free. Check it out.