Update series of numeric values in long string [closed] - regex

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)

Related

Regular Expressions - Snowflake [closed]

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 4 months ago.
Improve this question
enter image description hereI am trying to get text till the fourth "\n \n" from the below text. Can you please help me to write the snowflake expression for this issue.
Hello Jeffrey,\n \nWe have not heard from you yet. I hope all is well with you.\n \nChecking in to gather your Goosehead approved office location address, so we can add you to our database here at ERGOS. Once added here, we can schedule your laptop setup.\n \nGoosehead requires all agents to be onboarded by ERGOS so that we can provide IT support as well as get your laptop in our database. \n \nDo you have a laptop ready for setup?
so every thing up to the first \n \n can be fetched with regexp_substr via:
select
regexp_substr(column1, '.*\n \n') as match
from values
('Hello Jeffrey,\n \nWe have not heard from you yet. I hope all is well with you.\n \nChecking in to gather your Goosehead approved office location address, so we can add you to our database here at ERGOS. Once added here, we can schedule your laptop setup.\n \nGoosehead requires all agents to be onboarded by ERGOS so that we can provide IT support as well as get your laptop in our database. \n \nDo you have a laptop ready for setup?');
MATCH
Hello Jeffrey,
now, if we add a group around that ( ) and ask for 4 matches {4}, and swap to a smaller sample text, to make things less ugly for the output
select
regexp_substr(column1, '(.*\n \n){4}') as match
from values
('1111\n \n222222222222222\n \n3333333333333333\n \n44444444444444444\n \n55555555555555555555555');
gives:
MATCH
1111 222222222222222 3333333333333333 44444444444444444
if you are expecting the \n in the output:
then
select
column1,
regexp_substr(column1, '[^\\\\]+\\\\n \\\\n') as match
from values
('1111\\n \\n22222\\n \\n33333333\\n \\n4444444\\n \\n55555\\n \\66666\\n \\n7777');
shows how they need to be encoded in the SQL to output, and thus how to encode the match.
these matches greedy and gives:
COLUMN1
MATCH
1111\n \n22222\n \n33333333\n \n4444444\n \n55555\n \66666\n \n7777
1111\n \n
thus putting the grouping back in:
select
column1,
regexp_substr(column1, '([^\\\\]+\\\\n \\\\n){4}') as match
from values
('1111\\n \\n22222\\n \\n33333333\\n \\n4444444\\n \\n55555\\n \\66666\\n \\n7777');
COLUMN1
MATCH
1111\n \n22222\n \n33333333\n \n4444444\n \n55555\n \66666\n \n7777
1111\n \n22222\n \n33333333\n \n4444444\n \n
Picture to example for escaped new lines:

How to Create a List that holds 2 types of variable types in Scala [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Im new to scala so please go easy on me lol.
I need to create a list of where each spot holds an Int,String. So like [(1,"string1"),(2,"String2")...]
for example, Ive tried
val string1 = "something"
val string2 = "something"
List[Int,String] = List[(1,string1), (2,string), (3,string3),(4,string4),(5,string5)]
and I get the error - identifier expected but integer literal found.
How exactly would I get something like this to work?
(1,"string1") is a tuple containing an Int and a String, so type of list should also be a tuple - (Int, String):
val string1 = "something"
val string2 = "something"
// ... rest of string values
val list: List[(Int,String)] = List((1,string1), (2,string2), (3,string3),(4,string4),(5,string5))

All possible combinations of all possible length [closed]

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 3 years ago.
Improve this question
All possible combinations of all possible length
I have array like this. It can have arbitrary length rows and cols, however, cols length is fixed for every row.
{
{a, b},
{c, d},
{e, f}
}
And i need all possible combinations with all possible length.
All combinations, example for array above:
a, b, c, d, e, f
ac, ad, ae, af, bc, bd, be, bf, ce, cf, de, df
ace, acf, ade, adf, bce, bcf, bde, bdf
How do i accomplish this?
Algorithm description will be enough, however, code example (preferably C++) will help me a lot. I understand there is recursion smell with for loops, but i can't do it properly.
You can proceed by levels as your formatted output in description.
For the first level, you will have your characters
Second level you do a cartesian product between each pair of adjacent rows (easy 2 for loops)
Third level : for each result in second level, do a cartesian product with the row following the 2 adjacent rows
and so on.. until level N where N is the number of rows
An algorithm for your example is:
function rec(str, array, level)
if level = array.size()
print str
else
for i in append(array[level], "")
rec(concat(str, i), array, level + 1)
endfor
endif
You would start with
rec("", {{a, b},{c, d},{e, f}}, 0)

compare 2 arrays and get the values which are not matched :using perl map command or loops simply [closed]

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);

regex remove seconds and milliseconds [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is linked the my previous question, regex to add hypen in dates.
I would now like to be able to remove the seconds and milliseconds/change it to zero using gsub again as well
i.e. something like:
x <- c("20130603 00:00:03.102","20130703 00:01:03.103","20130804 00:03:03.104")
y <- gsub([REGEX PATTERN TO MATCH],[REPLACEMENT PATTERN TO INSERT HYPHEN and REMOVE SECONDS] ,x)
> y
[1] "2013-06-03 00:00:00" "2013-07-03 00:01:00" "2013-08-04 00:03:00"
You can use strptime to parse your objects into POSIXlt objects which, when printed, are exactly in the format you expect:
y <- strptime(x, "%Y%m%d %H:%M:%S")
# [1] "2013-06-03 00:00:03" "2013-07-03 00:01:03" "2013-08-04 00:03:03"
To remove seconds, use trunc:
y <- trunc(y, units = "mins")
# [1] "2013-06-03 00:00:00" "2013-07-03 00:01:00" "2013-08-04 00:03:00"
Having your objects as date/time objects will open a lot of doors, but if you really mean to store the output as a character vector, then just use as.character:
y <- as.character(y)
A lubridate version:
library(lubridate)
dt <- ymd_hms(x)
dt2 <- update(dt, seconds = 0)
You can try this regex, which I added a bit:
gsub("(\\d{4})(\\d{2})(\\d{2}) (\\d{2}:\\d{2}).*", "\\1-\\2-\\3 \\4:00", subject, perl=TRUE);
demo on regex101.