Split into string array only the key by comma and not values c# - strsplit

This is my String and i have problems when splitting into string array with comma seperated values for keys
{ Yr = 2019, Mth = DECEMBER , SeqN = 0, UComment = tet,tet1, OComment = test,test1, FWkMth = WK, FSafety = Y, FCustConsign = Y, FNCNRPull = 0, FNCNRPush = 0, CreatedTime = 2020-01-03 06:16:53 }
when i try to use string.Split(',') i get "Ucomment = tet","tet1" as seperate array.
But i need to have split string[] when seperated by comma
UComment = tet,tet1
OComment = test,test1
I have tried using the regex ,(?=([^\"]\"[^\"]\")[^\"]$)" but it didnot work.

You may try matching on the regex pattern \S+\s*=\s*.*?(?=\s*,\s*\S+\s*=|\s*\}$):
string input = "{ Yr = 2019, Mth = DECEMBER , SeqN = 0, UComment = tet,tet1, OComment = test,test1, FWkMth = WK, FSafety = Y, FCustConsign = Y, FNCNRPull = 0, FNCNRPush = 0, CreatedTime = 2020-01-03 06:16:53 }";
Regex regex = new Regex(#"\S+\s*=\s*.*?(?=\s*,\s*\S+\s*=|\s*\}$)");
var results = regex.Matches(input);
foreach (Match match in results)
{
Console.WriteLine(match.Groups[0].Value);
}
This prints:
Yr = 2019
Mth = DECEMBER
SeqN = 0
UComment = tet,tet1
OComment = test,test1
FWkMth = WK
FSafety = Y
FCustConsign = Y
FNCNRPull = 0
FNCNRPush = 0
CreatedTime = 2020-01-03 06:16:53
Here is an explanation of the regex pattern used:
\S+ match a key
\s* followed by optional whitespace and
= literal '='
\s* more optional whitespace
.*? match anything until seeing
(?=\s*,\s*\S+\s*=|\s*\}$) that what follows is either the start of the next key/value OR
is the end of the input

Related

plotly joins points in wrong order

I have the following df (part)
"Date"
"2022-09-01" "2022-09-02" "2022-09-05" "2022-09-06" "2022-09-07" "2022-09-08" ....
"LogClose"
8.285728 8.274933 8.274933 8.270830 8.289004 8.295593 ....
"wielkoscDD"
0.00000000 -0.01073648 -0.01073648 -0.01478755 0.00000000 0.00000000 ....
I use:
p <- plot_ly(
df,
x = ~Date,
y = ~LogClose,
name = 'zamknięcie',
type = 'scatter',
mode = 'lines',
text = ~paste("zamknięcie :", Zamkniecie),
width = obrazek_szer,
height = obrazek_wyso)
but the second draw is correct
p <- plot_ly(
df,
x = ~Date,
y = ~wielkoscDD,
name = 'drawdown',
type = 'scatter',
mode = 'lines',
text = ~paste("drawdown : ", wielkoscDD),
width = obrazek_szer,
height = obrazek_wyso)
Additionaly on my Mac it works ok - on Windows there is chaos with the 1st chart.
rgds
Grzegorz

Python3 Error ValueError: invalid literal for int() with base 10: '1.8'

im trying to make a calculation script with python but im running into this problem:
ValueError: invalid literal for int() with base 10: '1.8'
Im trying to ask a user for an input that is in numbers like 2000 but when the input contains a ',' or '.' it will output that error.
This is the code:
Buiten = int(input("Buiten: "))
Binnen = int(input("Binnen: "))
DikteF = int(input("Dikte: "))
Sortle = int(input("Soortelijk gewicht: "))
inputs = Buiten,Binnen,DikteF,Sortle
Pi = 3.14
R1 = Buiten / 2
R2 = Binnen / 2
UD1 = Pi*R1/1000*R1/1000
UD2 = Pi*R2/1000*R2/1000
Tpv = UD1 - UD2
Ltr = Tpv * DikteF
Srt = Ltr * Sortle
print("")
print("Uitwendige Diameter = ",(round(UD1, 3)),"m²")
print("Inwendige Diameter = ",(round(UD2, 3)),"m²")
print("Product Oppervlak = ",(round(Tpv, 3)), "m²")
print("")
print("Inhoud in L = ",(round(Ltr, 3)),"Liters")
print("Totaal gewicht = ",(round(Srt, 3), "Kilos" ))
print("")
os.system("pause")
while True:
try:
Bereken()
except:
print("Fout: Gebruik alleen cijfers.")
time.sleep(4)
Bereken()
Python returns an error since it is expecting an integer value as string to convert to integer. However, when passing decimals, the attempt to convert to integer fails since they are not integers.
if you are going to insert either integers or decimals, use float
Buiten = float(input("Buiten: "))
Binnen = float(input("Binnen: "))
DikteF = float(input("Dikte: "))
Sortle = float(input("Soortelijk gewicht: "))

add value afterwards to key in table lua

Does someone know how to add an value to an key which already has an value ?
for example:
x = {}
x[1] = {string = "hallo"}
x[1] = {number = 10}
print(x[1].string) --nil
print(x[1].number) --10
It should be possible to print both things out. The same way how it is here possible:
x[1] = { string = "hallo" ; number = 10}
I just need to add some informations afterwards to the table and especially to the same key.
Thanks!
x = {} -- create an empty table
x[1] = {string = "hallo"} -- assign a table with 1 element to x[1]
x[1] = {number = 10} -- assign another table to x[1]
The second assignment overwrites the first assignment.
x[1]["number"] = 10 or short x[1].number = 10 will add a field number with value 10 to the table x[1]
Notice that your x[1] = { string = "hallo" ; number = 10} is acutally equivalent to
x[1] = {}
x[1]["string"] = "hallo"
x[1]["number"] = 10

Are there time zone abbreviations for UTC-12 and UTC+12?

I'm working on a problem where I want to do calculations on NSDates where a single NSDate gives different dd/mm/yyyy values in different time zones.
To do that I'm currently using New York City (EST) and Aukland, NZ, since they are frequently on different dates.
I'd like to be able to use the time zones on either side of the international date line, UTC+12, and UTC-12. There appears to be a standard abbreviation for UTC+12, ANAT, for Anadyr, Russia. However, the iOS implementation of TimeZone/NSTimeZone doesn't seem to recognize it. There also does not seem to be an abbreviation for UTC-12 (which would be in Alaska).
Does anybody know if there are such abbreviations for UTC+12 and UTC-12 that iOS (or Mac OS, for that matter) recognizes?
It looks like the answer is no.
I wrote some code to fetch all the system time zones, sort them by offset, and print them:
typealias timeZoneTuple = (abbreviation: String, name: String, offset: Int)
let timeZones = TimeZone.abbreviationDictionary
let mappedTimeZones: [timeZoneTuple] = timeZones
.map {key, value in
var offset = 0
if let timeZone = TimeZone(abbreviation: key) {
offset = timeZone.secondsFromGMT() / 3600
}
return (abbreviation: key, name: value, offset:offset)}
.sorted {$0.offset < $1.offset}
mappedTimeZones.forEach {
let abbreviation = $0.abbreviation.padding(toLength: 4, withPad: " ", startingAt: 0)
let name = $0.name.padding(toLength: 20, withPad: " ", startingAt: 0)
print("abbreviation = \(abbreviation), offset = \(name), val = \($0.offset)")}
The output of the above code is:
abbreviation = HST , offset = Pacific/Honolulu , val = -10
abbreviation = AKDT, offset = America/Juneau , val = -9
abbreviation = AKST, offset = America/Juneau , val = -9
abbreviation = PST , offset = America/Los_Angeles , val = -8
abbreviation = PDT , offset = America/Los_Angeles , val = -8
abbreviation = MDT , offset = America/Denver , val = -7
abbreviation = MST , offset = America/Denver , val = -7
abbreviation = CDT , offset = America/Chicago , val = -6
abbreviation = CST , offset = America/Chicago , val = -6
abbreviation = EDT , offset = America/New_York , val = -5
abbreviation = PET , offset = America/Lima , val = -5
abbreviation = EST , offset = America/New_York , val = -5
abbreviation = COT , offset = America/Bogota , val = -5
abbreviation = ADT , offset = America/Halifax , val = -4
abbreviation = AST , offset = America/Halifax , val = -4
abbreviation = CLT , offset = America/Santiago , val = -3
abbreviation = CLST, offset = America/Santiago , val = -3
abbreviation = ART , offset = America/Argentina/Bu, val = -3
abbreviation = BRST, offset = America/Sao_Paulo , val = -2
abbreviation = BRT , offset = America/Sao_Paulo , val = -2
abbreviation = GMT , offset = GMT , val = 0
abbreviation = WET , offset = Europe/Lisbon , val = 0
abbreviation = BST , offset = Europe/London , val = 0
abbreviation = WEST, offset = Europe/Lisbon , val = 0
abbreviation = UTC , offset = UTC , val = 0
abbreviation = CEST, offset = Europe/Paris , val = 1
abbreviation = WAT , offset = Africa/Lagos , val = 1
abbreviation = CET , offset = Europe/Paris , val = 1
abbreviation = CAT , offset = Africa/Harare , val = 2
abbreviation = MSD , offset = Europe/Moscow , val = 3
abbreviation = EAT , offset = Africa/Addis_Ababa , val = 3
abbreviation = IRST, offset = Asia/Tehran , val = 3
abbreviation = MSK , offset = Europe/Moscow , val = 3
abbreviation = EET , offset = Europe/Istanbul , val = 3
abbreviation = EEST, offset = Europe/Istanbul , val = 3
abbreviation = GST , offset = Asia/Dubai , val = 4
abbreviation = IST , offset = Asia/Calcutta , val = 5
abbreviation = PKT , offset = Asia/Karachi , val = 5
abbreviation = BDT , offset = Asia/Dhaka , val = 6
abbreviation = WIT , offset = Asia/Jakarta , val = 7
abbreviation = ICT , offset = Asia/Bangkok , val = 7
abbreviation = SGT , offset = Asia/Singapore , val = 8
abbreviation = HKT , offset = Asia/Hong_Kong , val = 8
abbreviation = PHT , offset = Asia/Manila , val = 8
abbreviation = KST , offset = Asia/Seoul , val = 9
abbreviation = JST , offset = Asia/Tokyo , val = 9
abbreviation = NZDT, offset = Pacific/Auckland , val = 13
abbreviation = NZST, offset = Pacific/Auckland , val = 13
So it looks like UTC-12, UTC-11, UTC-1, UTC+10, UTC+11, and UTC+12 are all missing from the "named" timezones that are available in Cocoa.
EDIT:
Based on a comment from #MattJohnson, it seems that the identifiers is a better way to get the list of available time zones. Modifying my code to use identifiers instead:
struct timeZoneStruct: CustomStringConvertible {
let identifier: String
var offset: Int
var description: String {
let displayOffset = String(format: "%3d", offset)
let displayIdentifier = (identifier + ",").padding(toLength: 30, withPad: " ", startingAt: 0)
return "identifier = \(displayIdentifier) offset = \(displayOffset)"
}
}
let timeZoneIDs = TimeZone.knownTimeZoneIdentifiers
let mappedTimeZones: [timeZoneStruct] = timeZoneIDs
.map {identifier in
var offset = 0
if let timeZone = TimeZone(identifier: identifier) {
offset = timeZone.secondsFromGMT() / 3600
}
return timeZoneStruct(identifier: identifier, offset: offset)}
.sorted {$0.offset < $1.offset}
mappedTimeZones.forEach {
print($0.description)
}
That yields a list of time zones ranging from UTC-11 (Pacific/Pago_pago) to UTC+14 (Pacific/Apia)
(There are quite a few duplicates for most time zones, so the list is too long to include here.)
So it seems there are defined time zones for offsets from UTC-11 to UTC+14. There is not a time zone for UTC-12 however, even though Baker Island, at Lat/Long: 0°12'N / 176°29'W, is in UTC-12. Curious.
This is the simplest way to get all timezones with their respective abbreviation.
P.S Not all timezones have their proper 3-Letter Abbreviations.
let timezoneList = NSTimeZone.knownTimeZoneNames
for i in 0...timezoneList.count - 1 {
let locale = NSTimeZone.init(name: timezoneList[i])
print("Region: \((locale?.name)!) Abbr: \((locale?.abbreviation)!)")
}
Also total 51, 3-lettered Abbreviations are present:
print(TimeZone.abbreviationDictionary.count)
You can also explore https://developer.apple.com/documentation/foundation/timezone for more.

F# - Flatten List/Range

I'm new to F# and am wondering how I would go about flattening a list.
Essentially in the database I store a record with a min_age and max_age range (this is a fictitious example for the sake of brevity - i am not agist!). My fields look something like the following:
id,
cost,
savings,
min_age,
max_age
I essentially have an F# class that acts as a one-to-one mapping with this table - i.e. all properties are mapped exactly to the database fields.
What I would like to do is flatten this range. So, instead of a list containing items like this:
saving_id = 1, cost = 100, savings = 20, min_age = 20, max_age = 26
saving_id = 2, cost = 110, savings = 10, min_age = 27, max_age = 31
I would like a list containing items like this:
saving_id = 1, cost = 100, savings = 20, age = 20
saving_id = 1, cost = 100, savings = 20, age = 21
etc.
saving_id = 2, cost = 110, savings = 10, age = 27
saving_id = 2, cost = 110, savings = 10, age = 28
etc.
Is there any in-built mechanism to flatten a list in this manner and/or does anyone know how to achieve this?
Thanks in advance,
JP
You might want to use Seq.collect. It concatenates sequences together, so in your case, you can map a function over your input that splits a single age range record to a sequence of age records and use Seq.collect to glue them together.
For example:
type myRecord =
{ saving_id: int;
cost: int;
savings: int;
min_age: int;
max_age: int }
type resultRecord =
{ saving_id: int;
cost: int;
savings: int;
age: int }
let records =
[ { saving_id = 1; cost = 100; savings = 20; min_age = 20; max_age = 26 }
{ saving_id = 2; cost = 110; savings = 10; min_age = 27; max_age = 31 } ]
let splitRecord (r:myRecord) =
seq { for ageCounter in r.min_age .. r.max_age ->
{ saving_id = r.saving_id;
cost = r.cost;
savings = r.savings;
age = ageCounter }
}
let ageRanges = records |> Seq.collect splitRecord
Edit: you can also use a sequence generator with yield!
let thisAlsoWorks =
seq { for r in records do yield! splitRecord r }
Agreeing with cfern's answer, but was wondering if this might benefit from seeing another "built-in" function used. Here's an alternative version of the splitRecord function that shows the library call for unfolding a sequence. No gain here other than having an example for Seq.unfold.
let splitRecord (r:myRecord) =
Seq.unfold (fun curr_age ->
if curr_age <= r.max_age then
Some({ saving_id = r.saving_id;
cost = r.cost;
savings = r.savings;
age = curr_age } ,
curr_age + 1)
else None)
r.min_age

Resources