How to separate a string in Swift 3 by a specific character - arrays

I am using Swift 3 and have a long string containing 0's and 1's.
Ex. "1111001111111111001111111111000111000000000111000111111111100000000111100111100111111111100000000011100011111111110011110011110000000000000000000"
I am trying to split up this string into two arrays, to determine how many 1's and 0's are repeating after another.
When I call the method let oneArray = binString.components(separatedBy: "0") and let zeroArray = binString.components(separatedBy: "1") The returned arrays we get for our example string is:
["1111", "", "1111111111", "", "1111111111", "", "", "111", "", "", "", "", "", "", "", "", "111", "", "", "1111111111", "", "", "", "", "", "", "", "1111", "", "1111", "", "1111111111", "", "", "", "", "", "", "", "", "111", "", "", "1111111111", "", "1111", "", "1111", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
["", "", "", "", "00", "", "", "", "", "", "", "", "", "", "00", "", "", "", "", "", "", "", "", "", "000", "", "", "000000000", "", "", "000", "", "", "", "", "", "", "", "", "", "00000000", "", "", "", "00", "", "", "", "00", "", "", "", "", "", "", "", "", "", "000000000", "", "", "000", "", "", "", "", "", "", "", "", "", "00", "", "", "", "00", "", "", "", "0000000000000000000"]
And we don't understand why we can't just return arrays with just the 0's and 1's and not the empty strings as array elements. It doesn't seem that anyone else has asked this kind of question before, and we are confused because this seems to be the general method used to delimit strings from a character.
I'm wondering how to separate these strings the way we want. Seems like a weird situation here

Every occurrence of the delimiter string will split the original string into a new string, so consecutive occurrences will result in empty strings.
You are close to what you want. Just add a filter { !$0.isEmpty } statement to the end of each line to remove the unwanted empty strings:
let binString = "1111001111111111001111111111000111000000000111000111111111100000000111100111100111111111100000000011100011111111110011110011110000000000000000000"
let oneArray = binString.components(separatedBy: "0").filter { !$0.isEmpty }
let zeroArray = binString.components(separatedBy: "1").filter { !$0.isEmpty }
print(oneArray)
print(zeroArray)
Output:
["1111", "1111111111", "1111111111", "111", "111", "1111111111", "1111", "1111", "1111111111", "111", "1111111111", "1111", "1111"]
["00", "00", "000", "000000000", "000", "00000000", "00", "00", "000000000", "000", "00", "00", "0000000000000000000"]

The components function uses the character as delimiter so it will not include it in the resulting array and will treat consecutive delimiters as an empty string element.
What you could do is this:
let binary = "1111001111111111001111111111000111000000000111000111111111100000000111100111100111111111100000000011100011111111110011110011110000000000000000000"
let series = binary.replacingOccurrences(of: "10", with: "1,0")
.replacingOccurrences(of: "01", with: "0,1")
.components(separatedBy:",")
By inserting a comma between every break in 1/0 series, you get the delimiters exactly where you need them.
The series array will contain consecutive 1s and consecutive 0s in alternance. You can sort it if you want series of all 1s and all 0s grouped together.

Related

Pivot data down instead of flattening in snowflake

I have Json --
[
{
"label": "EL04_AA02_P2064_12588",
"uri": "http://www.awer.com/onasdgfgies/addhh.rdf#i613b844f9b503172424717ebbfa5f73098",
"updated_at": "2022-07-27 09:16:43",
"interactionData": [
{
"type": "Choice",
"nb choice": 4,
"responseIdentifier": "RESPONSE_A",
"BR_identifier": "choice_2",
"BR_label": "chronological order",
"choice_identifier_1": "choice_1",
"choice_label_1": "problem and solution",
"choice_identifier_2": "choice_2",
"choice_label_2": "chronological order",
"choice_identifier_3": "choice_3",
"choice_label_3": "cause and effect ",
"choice_identifier_4": "choice_4",
"choice_label_4": "comparison of ideas"
},
{
"type": "Choice",
"nb choice": 6,
"responseIdentifier": "RESPONSE_B",
"BR_identifier": "choice_8|choice_13",
"BR_label": "“In 1958, asdghfjgjjk asdghfjgjjk became NASA. Shortly thereafter, asdghfjgjjk became part of the space team.” (paragraph 7)|“In September 1962, President John F. Kennedy charged the country to send a man to the Moon.” (paragraph 8)",
"choice_identifier_1": "choice_5",
"choice_label_1": "“asdghfjgjjk was an asdghfjgjjk asdghfjgjjk who worked for asdghfjgjjk from 1953 until 1986.” (paragraph 1)",
"choice_identifier_2": "choice_6",
"choice_label_2": "“asdghfjgjjk was the name of the asdghfjgjjk agency that later became asdghfjgjjk.” (paragraph 4)",
"choice_identifier_3": "choice_7",
"choice_label_3": "“asdghfjgjjk.” (paragraph 5)",
"choice_identifier_4": "choice_8",
"choice_label_4": "“In 1958, asdghfjgjjk asdghfjgjjk became asdghfjgjjk. Shortly thereafter, asdghfjgjjk became part of the space team.” (paragraph 7)",
"choice_identifier_5": "choice_13",
"choice_label_5": "“asdghfjgjjk.” (paragraph 8)",
"choice_identifier_6": "choice_14",
"choice_label_6": "“to orbit.” (paragraph 8)"
}
],
"Task Model": "4B.1: Analyzing the relationship between a series of concepts",
"Common Core State Standard ID": "RI.4.5 4.1 Refer to d.",
"Evidence Statement": "RI 4.1.1: .",
"ELA Item Key": "",
"Number of Points": "2",
"Scoring Rules": "EBSR - 1A,2+E",
"Item Type": "EBSR",
"Task Type": "RST",
"Text Complexity": "Medium",
"CC4 Processing Demands": "Medium",
"CC3 Response Mode": "High",
"CC2 Command of Textual Evidence": "Medium",
"EOY Informational Category": "Reading Informational Text",
"Claim/Sub-Claim": "Reading - Informational Text",
"Item Set Sequence": "3",
"Item Passage Sequence": "",
"Associated Passage": "Who Was asdghfjgjjk",
"Passage Group": "asdghfjgjjk",
"Passage Set ID": "P2064",
"Grade": "Grade 4",
"Status Comments": "",
"Original UIN": "EL04_AA02_P2064_12588",
"Interaction Type IL": "",
"Operational": "",
"Field Test": "",
"Workflow Status": "09. Ready for Committee Review",
"Phases": "Year 2",
"Item Key": "B|D,E",
"5-Digit UIN": "12588",
"Item_Writer_ID": "CAE04",
"Multimedia": "No",
"Release Year": "",
"Scoring Rubric or Rationale": "B|D,E",
"Dependent Item": "",
"Scoring Mode": "",
"Alt Files": "",
"Exclude Accessibility Support Features": "",
"Accessibility Support Features": "",
"Item Enemy": "",
"Art": "",
"Delivery Mode": "Paper|Computer",
"Cognitive Complexity": "Medium",
"Bank": "Illinois",
"Subject": "ELA",
"Asset Type": "Item",
"State": "",
"assets": [
{
"Label": "EL04_AA02_RST_P2064-0_Scenario",
"Asset_Developer_ID": "",
"Original UIN": "",
"Content Type": "",
"Grade": "",
"Graphics/Multimedia Revisions Requested": "",
"Graphic_Alt_Tags": "",
"Academic Course": [],
"Passage/Stimulus Set ID": "",
"Paired Passage/Stimulus": "",
"Passage/Stimulus Set Title": "",
"Passage/Stimulus Set Sequence": "",
"Asset Workflow Status": "",
"Passage/Stimulus A Title": "",
"Passage/Stimulus A ID": "",
"Item Passage/Stimulus Sequence": [],
"Passage/Stimulus B Title": "",
"Phases": "",
"Passage/Stimulus B ID": "",
"Passage/Stimulus C Title": "",
"Passage/Stimulus C ID": "",
"Passage Type": "",
"Author Name": "",
"EOY Informational Category": "",
"Genre": [],
"Length Designation": "",
"Text Complexity Analysis": "",
"Word Count": "",
"Lexile": "",
"Flesch-Kincaid": "",
"RMM": "",
"Glossed Terms": "",
"Gender Representation in Text": "",
"Multi-Cultural Passage": "",
"Author Gender": "",
"Text Selection": "",
"Number of Art Pieces": "",
"Text Complexity": "",
"Task Type": "",
"Copyright": "",
"Delivery Mode": [],
"Copyright Type": "",
"Art": "",
"CCC Number": "",
"Source": "",
"Copyright Source Text": "",
"Rights": [],
"Text Permissions Notes": "",
"Art Permissions": "",
"State": "",
"PkgHref": "",
"PkgIdentifier": "",
"Language": "English",
"Alternative Text": "EL04_AA02_RST_P2064-0_Scenario",
"uri": "http://www.awer.com/onasdgfgies/addhh.rdf#i613b844f9b503172424717ebbfa5f73098",
"identifier": "i61af6b75e5c4c14293c665e5dc1199871a",
"updated_at": "2022-03-30 07:02:29"
},
{
"Label": "EL04_AA02_RST_P2064-1_WhoWasKatherineasdghfjgjjk",
"Asset_Developer_ID": "",
"Original UIN": "",
"Content Type": "",
"Grade": "",
"Graphics/Multimedia Revisions Requested": "",
"Graphic_Alt_Tags": "",
"Academic Course": [],
"Passage/Stimulus Set ID": "",
"Paired Passage/Stimulus": "",
"Passage/Stimulus Set Title": "",
"Passage/Stimulus Set Sequence": "",
"Asset Workflow Status": "",
"Passage/Stimulus A Title": "",
"Passage/Stimulus A ID": "",
"Item Passage/Stimulus Sequence": [],
"Passage/Stimulus B Title": "",
"Phases": "",
"Passage/Stimulus B ID": "",
"Passage/Stimulus C Title": "",
"Passage/Stimulus C ID": "",
"Passage Type": "",
"Author Name": "",
"EOY Informational Category": "",
"Genre": [],
"Length Designation": "",
"Text Complexity Analysis": "",
"Word Count": "",
"Lexile": "",
"Flesch-Kincaid": "",
"RMM": "",
"Glossed Terms": "",
"Gender Representation in Text": "",
"Multi-Cultural Passage": "",
"Author Gender": "",
"Text Selection": "",
"Number of Art Pieces": "",
"Text Complexity": "",
"Task Type": "",
"Copyright": "",
"Delivery Mode": [],
"Copyright Type": "",
"Art": "",
"CCC Number": "",
"Source": "",
"Copyright Source Text": "",
"Rights": [],
"Text Permissions Notes": "",
"Art Permissions": "",
"State": "",
"PkgHref": "",
"PkgIdentifier": "",
"Language": "English",
"Alternative Text": "EL04_AA02_RST_P2064-1_WhoWasKatherineasdghfjgjjk",
"uri": "http://www.awer.com/onasdgfgies/addhh.rdf#i613b844f9b503172424717ebbfa5f73098",
"identifier": "i613b844f9b503172424717ebbfa5f73098",
"updated_at": "2022-05-23 01:36:53"
}
]
}
]
Want to transform like below--
using below link to convert same to csv. selecting Pivot data down instead of flattening option.
https://www.convertcsv.com/json-to-csv.htm same i want to write transformation. Could you please help me.
I am not sure why you want to use PIVOT when flatten works really nicely
Ah that was the thing to do in the online tool...
select f1.value:uri as uri
,f1.value:updated_at as updated_at
,f2.value:type as "interactionData//0//type"
,f2.value:"nb choice" as "interactionData//0//nu choice"
,f2.value:responseIdentifier as "interactionData//0//responseIdentifier"
from data
,table(flatten(input=>data.json)) as f1
,table(flatten(input=>f1.value:interactionData)) as f2
gives:
URI
UPDATED_AT
interactionData//0//type
interactionData//0//nu choice
interactionData//0//responseIdentifier
"http://www.awer.com/onasdgfgies/addhh.rdf#i613b844f9b503172424717ebbfa5f73098"
"2022-07-27 09:16:43"
"Choice"
4
"RESPONSE_A"
"http://www.awer.com/onasdgfgies/addhh.rdf#i613b844f9b503172424717ebbfa5f73098"
"2022-07-27 09:16:43"
"Choice"
6
"RESPONSE_B"
albeit, I would cast those values to types like:
select f1.value:uri::text as uri
,f1.value:updated_at::timestamp_ntz as updated_at
,f2.value:type::text as "interactionData//0//type"
,f2.value:"nb choice"::number as "interactionData//0//nu choice"
,f2.value:responseIdentifier::text as "interactionData//0//responseIdentifier"
from data
,table(flatten(input=>data.json)) as f1
,table(flatten(input=>f1.value:interactionData)) as f2

JSON TSQL add element in middle of array

I have the following:
declare #X nvarchar(max)
set #X='[
{
"a": "",
"b": "",
"c": "",
"e": "",
"f": "",
"g": ""
}
]'
declare #y nvarchar(max)
set #y='{"bac": ""}'
And this is what I need:
'"pro": [
{
"a": "",
"b": "",
"c": "",
"address": {
"bac": ""
},
"e": "",
"f": "",
"g": ""
}
]'

Swift - define variable name from value [duplicate]

This question already has answers here:
Swift Dynamic Variable Name depend on value
(2 answers)
Create a variable in swift with dynamic name
(4 answers)
Swift: Converting a string into a variable name
(1 answer)
Closed 3 years ago.
Is there a way to define name of variable from value(array)?
I need it for automatically creating arrays, but I don't want type name manually, rather have it chosen from an array.
dataEmail = ["1", "POS101582781", "2018.10.26 10:02", "Buy", "1", "VMID", "29.435", "", "", "", "30.42", "0.99", "30.42\n2", "POS104121282", "2018.12.10 10:00", "Buy", "1", "PSN", "1940", "", "", "", "2440", "5", "24.4\n3", "POS101582480", "2018.10.26 10:00", "Buy", "2", "HL", "1796.5", "", "", "", "1730", "-1.33", "34.6\n4", "POS101582499", "2018.10.26 10:00", "Buy", "8", "INVP", "473.6", "", "", "", "493.4", "1.58", "39.47\n5", "POS102186690", "2018.11.06 10:00", "Buy", "40", "VOD", "153.63", "", "", "", "134.38", "-7.7", "53.75\n6", "POS105530053", "2019.01.04 10:00", "Buy", "5", "MKS", "253.4", "", "", "", "272.5", "0.96", "13.63\n7", "POS102224924", "2018.11.06 17:52", "Buy", "14", "EVR", "488.15", "", "", "", "563.4", "10.54", "78.88\n8", "POS102087914", "2018.11.02 17:32", "Buy", "59", "DC", "164.112", "", "", "", "133.2", "-18.24", "78.59\n9", "POS101582520", "2018.10.26 10:00", "Buy", "20", "BT", "241.4", "", "", "", "214.9", "-5.3", "42.98\n10", "POS101582491", "2018.10.26 10:00", "Buy", "101", "LLOY", "55.5975", "", "", "", "63.58", "8.06", "64.22\n11", "POS101582545", "2018.10.26 10:00", "Buy", "26", "AV", "413.662", "", "", "", "423.2", "2.48", "110.03\n12", "POS104121258", "2018.12.10 10:00", "Buy", "10", "PTEC", "389.6", "", "", "", "420.9", "3.13", "42.09\n13", "POS104962193", "2018.12.24 10:40", "Buy", "11", "RMG", "286.009", "", "", "", "283.4", "-0.29", "31.17\n14", "POS105487268", "2019.01.03 18:29", "Buy", "22", "CREI", "115.0909", "", "", "", "113.6", "-0.33", "24.99\n15", "POS108707475", "2019.02.14 10:00", "Buy", "4", "IMB", "2640", "", "", "", "2508.5", "-5.26", "100.34\n16", "POS102853078", "2018.11.16 11:45", "Buy", "3", "HMSO", "418.067", "", "", "", "377.9", "-1.21", "11.34\n17", "POS103378446", "2018.11.27 10:12", "Buy", "60", "CNCT", "37.3667", "", "", "", "38.65", "0.77", "23.19"]
let arrayName = ["VMID", "PSN", "HL", "INVP", "VOD", "MKS", "EVR", "DC", "BT", "LLOY", "AV", "PTEC", "RMG", "CREI", "IMB", "HMSO", "CNCT"]
func createStockSet (){
var indexNumber = 1
for n in 0...16 {
var arrayName[0] = StockClass(transactionName: dataEmail[indexNumber], date: dataEmail[indexNumber+1], type: dataEmail[indexNumber+2], amount: dataEmail[indexNumber+3], stockTinker: dataEmail[indexNumber+4], boughtPrice: dataEmail [indexNumber+5], nowPrice: dataEmail[indexNumber+6], floatingResult: dataEmail[indexNumber+9], margin: dataEmail[indexNumber+10])
indexNumber + 3
}}

How to modify data in a particular object using angularJS

let for an instance
{
"E-mail Address": "fultonlevy#ewaves.com",
"Related name": "",
"Home Address 2": "",
"Anniversary": "",
"First Name": "Rios",
"Business Address 2": "",
"Department": "",
"Display Name": "Baker Adkins",
"Home State": "",
"Business Country": "",
"Home Street": "",
"Birthday": "",
"Home Country": "",
"Pager": "",
"Categories": "",
"Home City": "",
"E-mail 3 Address": "bentleymccoy#cosmetex.com",
"Home Fax": "",
"Gender": "",
"Notes": "proident",
"Country Code": "",
"Job Title": "",
"Business Address": "",
"Web Page 2": "",
"Mobile Phone": "",
"Organization": "",
"Home Phone": "(962) 514-3534",
"E-mail 2 Address": "sheenaramirez#grupoli.com",
"Last Name": "Fisher",
"Nickname": "",
"Business Fax": "",
"Home Postal Code": "",
"Business Phone": "",
"Business Postal Code": "",
"Web Page": "",
"Business City": "",
"Business State": ""
}
<input ng-model="disName" type="text">
this is a specific object in a array of objects.
How can I modify a specific value for a key for suppose [key = Display Name] and with change in input value how can I modify it in object?
In angularJS you'll bind the object property to the input using ng-model.
There's no need to listen any change, because the framework takes care of that.
¿Who provides this object to the <input>? The controller, exposing the object through this.
Here's a working example using components.
https://plnkr.co/edit/lfD1LgML0nuRURKX4DCw?p=preview
As you can see, you'll need to use bracket notation because of the use of spaces on your object keys...
<input ng-model="$ctrl.obj['Display Name']">
On a real world example, I suppose your data will be provided using a service, as you can see in this example:
https://plnkr.co/edit/fp1fBC59dJLettJBvD2p?p=preview
Hope it helps!

While json_decode i want duplicate key values also in array PHP

I want to have the JSON below as an array with duplicated key values, i.e.:
"2016-09-16":{"available":"1","bind":0,"info":"","notes":"","price":"","promo":"","status":"booked"}
twice. How can I do that?
{
"2016-06-28": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-06-29": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-06-30": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-07-04": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-07-05": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-07-06": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-07-07": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-09-16": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-09-15": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-09-14": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-09-13": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-09-16": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-09-17": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
}
}
JSON with duplicate keys in the same object is not reliable across JSON parsers (some will choke, some will give you only the value of the last occurrence) and not useful in any case. Use arrays of objects for the values of those date keys, not individual objects:
{
"2016-06-29": [{
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
}],
"2016-09-16": [{
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
}, {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
}],
"2016-09-15": [{
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
}]
}
(Data shortened for clarity.)
In the above, note how 2016-06-29 and 2016-09-15 have arrays with just one entry, but 2016-09-16 has an array with two entries.

Resources