Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
hi i have a value which is
Blockquote
1 1:0.0644343 1 1:0.0309334 1 1:0.0261616
Blockquote
i want to separate value by space but after certain character to get result like this..is there any possible solution . i know we can do in regex
Blockquote
"1 1:0.0644343"
"1 1:0.0309334"
"1 1:0.0261616"
Blockquote
I think regex is the perfect tool here:
var str = "Blockquote 1 1:0.0644343 1 1:0.0309334 1 1:0.0261616 Blockquote"
let regex = try! NSRegularExpression(pattern: "(\\d \\d:[\\.0-9]+)", options: [])
let matches = regex.matchesInString(str, options: [], range: NSMakeRange(0, str.characters.count))
for m in matches.reverse() {
let range = m.rangeAtIndex(1)
let startIndex = str.startIndex.advancedBy(range.location)
let endindex = startIndex.advancedBy(range.length)
let value = str[startIndex..<endindex]
str.replaceRange(startIndex..<endindex, with: "\"\(str[startIndex..<endindex])\"")
}
print(str)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Can an element be appended to a list in Scala, for example, I have a list i.e.,
var li = List (1, 2, 3, 4, 5)
can I append 6 to the list, if so how?
In case of tuple how can I append, for example,
var tu = (1, "Hi", 20000)
can I append the "Hello" string to it?
Both List and Tuple are immutable constructs so to append or prepend an element, you create a new List/Tuple.
val oldLst = List(1,2,3,4,5)
val newLst = oldLst :+ 6
Appending to a List is a linear operation, O(n). It's much more efficient when prepending.
val fromZero = 0 :: newLst
Scala 3 offers enhanced Tuple abilities not available in earlier Scala releases.
val threeTup = (1, "Hi", 20000)
val fourTup = threeTup ++ "Hello" *: EmptyTuple
Note: Experienced Scala practitioners never use var.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have text column with following examplary data:
5,5,0.1;6,6,0.15;7,7,0.2;8,8,0.25;9,9,0.3;10,10,0.35;11,11,0.4;12,12,0.45;13,13,0.5;14,14,0.55;15,15,0.6;16,16,0.65;17,17,0.7;18,18,0.75;19,19,0.8;20,20,0.85;
I need to add some fixed value to each of numeric values (the one before semicolon)
so for example from:
5,5,0.1;6,6,0.15; I want add 0.15 so result would be:
5,5,0.25;6,6,0.3;
I guess I should try something with regexp_replace but I have no idea how to start here
The correct solution would be fix your broken data model and not store multiple, delimited values in a single column.
I wouldn't do this with a regex, but unnesting the elements of the string, adding the value to the third element, then aggregate everything back into the broken design:
update badly_designed_table
set denormalized_column =
(select string_agg(concat_ws(',', a, b, round(c + 0.15,2)), ';' order by idx)
from (
select split_part(val, ',', 1) as a,
split_part(val, ',', 2) as b,
split_part(val, ',', 3)::numeric as c,
idx
from unnest(string_to_array(bad_column, ';')) with ordinality as x(val,idx)
-- skip the "empty" element generated by the trailing ;
where nullif(val, '') is not null
) t)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So I have a list of product item descriptions. I have loaded this into R. Most of these descriptions are utter nonsense and we are trying to extract a decent item code from them.
Instead of going through it line by line, can I use a regular expression in R to create a new vector that will only have integer values from the list?
I have most of the code now
JJ <- read.csv2(file.choose(),header= TRUE)
JJ$X <- gsub(pattern = "[0-9]+", replacement = "",
x = JJ$LGY_DHB_ITEM_DESCRIPTION, ignore.case = TRUE)
But I am unsure what to put in the replacement argument.
you can try replacing non (^) numerical ([:digit:]) characters with empty string :
gsub("[^[:digit:]]*", "", 'PRIVATE CONTRACT INV 710456354')
[1] "710456354"
but this wont work if you have more than one numeric in your string:
gsub("[^[:digit:]]*", "", 'PRIVATE 123 CONTRACT INV 710456354')
[1] "123710456354"
You could try to find the longest numercial in each string:
JJ <- data.frame(LGY_DHB_ITEM_DESCRIPTION=c('PRIVATE CONTRACT INV 710456354', 'PRIVATE 123 CONTRACT INV 710456354'))
m <- gregexpr("[0-9]*", JJ$LGY_DHB_ITEM_DESCRIPTION)
all_m <- regmatches(JJ$LGY_DHB_ITEM_DESCRIPTION, m)
JJ$X <- mapply(FUN =function(stri,idx) stri[idx],all_m, sapply(lapply(all_m,nchar),which.max))
JJ
LGY_DHB_ITEM_DESCRIPTION X
1 PRIVATE CONTRACT INV 710456354 710456354
2 PRIVATE 123 CONTRACT INV 710456354 710456354
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
arr1 = 1,2,3,4,5;
arr2 = 1,2,3;
want to compare and output as arr3=4,5;
Please help
thanks in advance
arry::utils error out, looks like some problem with the package, so that option is ruled out.
sub diff_array {
my ($a1, $a2) = #_;
my %h;
#h{#$a2} = ();
return grep !exists $h{$_}, #$a1;
}
my #arr1 = (1,2,3,4,5);
my #arr2 = (1,2,3);
my #arr3 = diff_array(\#arr1, \#arr2);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to write an SML function that takes as parameters a list and an item. If the item is present, the function should return a tuple containing the list without the first such occurence of that item and the item just removed. If there are no occurences of the item in the list, the function should return NONE or something similar to indicate this absence.
Try this:
fun same_string(str, lst) =
case lst of
[] => NONE
|x::xs => case same_string(str, xs) of
NONE => if str = x
then SOME(xs)
else NONE
|SOME xs' => SOME (x :: xs')