Objective C: reading a string from CSV file - ios

I am parsing csv file, and I have this row..
45,12,bruine verbinding,mechelse heide,bruin,"276,201,836,338,468",01050000208A7A000001000000010200000002000000443BFF11CF720D41F296072200BD0641189D9C0D026D0D417A50F15264C30641,"MULTILINESTRING((241241.883787597 186272.016616039,241056.256646373 186476.540499333))"
When I convert this string into array by the method
NSArray *arrObjects=[strObjects componentsSeparatedByString:#","];
Then I get 13 objects rather I want 8 objects of array. Objects at index 5 further splits up into five more objects instead of one object(because this object has further commas (,) in string) and index 7 further splits up into 2 objects.
I want only full string object of at index 5 and index 7 instead of five and two objects respectively. I know this is because of method componentsSeparatedByString:#",".

Since the CSV standard allows commas to appear inside a record you can't blindly use componentsSeparatedByString:#"," to separate the fields.
It is actually a rather fussy problem to write a CSV parser that can handle line breaks, commas, and quotation marks as field data.
I would suggest either:
Dictating that the data for a field NOT contain commas, line breaks, or quotes (percent escape each field before saving it to the CSV)
or, if you must deal with data in that format, use an existing CSV library.
A quick Google search on "objective-c csv parser" shows this on Github:
CHCSVParser
Since it claims to be a "proper CSV parser" it should handle fields containing commas.

Related

write 2d array to CSV file swift

I am trying to write some data on CSV file and exporting the it
it is working but the problem is with the 2d array, during writing it is considering , between the element of array as a separate column
Please see the picture which is showing the problem
But i need the result like below!
i have tried String(describing:rythmReport.peakList[index]) but no gain
CSV splits the column on every comma (,) occurrence.
You should enclose the column in quotes to have them as a single column.
This should work:
"\"\(String(describing:rythmReport.peakList[index]))\""
Other solution for this is using a TSV Tab (\t), Tab separated Values instead of using a comma as a delimiter

how to change the format of a field when using parse to select fields in sumologic

I am totally new to sumologic platform. I am trying to select fields from the log data. The simple code is:
| parse "transactionNumber=*|" as transactionNumber
| parse "message=*|" as message
My transaction number is a very long numbers, such as 123456789987654321. So, when I 'Export(Display Fields)' to save the result to csv file, it will be automatically transfer to scientific notation such as 123e+15.
So, how to change the format, let's say from number to character, so that I can get the real numbers in csv?
I think the simple way is save the file as txt, instead of csv.
But this is not related to sumo logic programming. So I think this is not a very "descent" way.

How to give multiple hyperlink to single string

I am of the opinion that giving multiple hyperlink to a single string is not possible in MS word document. I don't have any knowledge of C# but I think after reading How insert multiple hyperlinks to one comment in MS WORD using C#? I just want to clarify weather multiple hyperlink to a single comment is possible or not.
For example I have a "string"
I want to give different hyperlinks like this
example.com/s
example.com/t
example.com/r
example.com/i
example.com/n
example.com/g
So that I get change to select where I want to go from that string.
In Word, a hyperlink is not a string, it's a field code. Field codes are special objects. It is, therefore, not possible to pass multiple hyperlink objects as part of a string. You can't even pass one hyperlink object as part of a string...
You can pass multiple hyperlink strings as a delimited string, then "split" the string into an array, loop the array to create multiple hyperlink objects.
If you want to open a hyperlink or hyperlinks from code, there is a FollowHyperlink method, as I recall (I'm on a mobile device at the moment, so can't double-check). You can pass a string to that.

How to apply a Hive schema to unstructured text?

I have a space delimited text file representing some logs data. For simplicity, headers would be:
'date', 'time',’query’,’host’
And a record would look like:
2001-01-01 01:02:04 irfjrifjWt.f=32&ydeyf myhost
A simple Hive table with space delimited fields will read this data correctly. However I want to do further parsing of the query string.
Within this text are tags that I want to parse into Hive columns.
Here’s a de-identified example of a couple of query strings:
ofifnmfiWT.s=12&ifmrinfnWT.df=hello’&oirjfirngirngWT.gh=32&iurenfur
ggfWT.gh=12&WT.ll=12&uyfer3d
Tags have the format WT.xx, followed by an =, followed by the value of the tag, followed by an &.
The order of the tags and the presence of each tag varies from record to record. The only thing I could define ahead is a set of tags I want to parse. In the example above, let’s say I’m interested in tags [WT.gh, WT.s]. Then (making up date time and host), my Hive table would look like:
Date time host WT.s WT.gh
2011-01-01 05:03:03 myhost1 12 32
2011-01-01 05:03:03 myhost1 NULL 12
I could easily parse the query string with Python and regex, and just create a second .txt file with the original record, plus a series of new values with the parsed tags, but that seems a waste of time and it doesn’t look like it is utilizing schema on read principles.
I might be wrong in my thinking, since I’m new to this, but I was wondering if there is a way to apply a schema on this data that would inherently do the parsing for me.
If not, what solution would you recommend?

Handling commas in data with VarArrayOf

I am using Locate with multiple fields and sometimes I am getting an "Incorrect number of search fields" error and have tracked it down to the data.
tbl.Locate('LName;FName;Stuff',
VarArrayOf([LName+','+FName','+Stuff]),[loCaseInsensitive]);
Sometimes it worked and sometimes it did not and I traced it down to a Comma in the Stuff if it included a Comma in the text.
Stuff = "My dog is gray" would work.
Stuff = "I am at work, but not happy" would not. That extra Comma made it look like four data fields.
Do I have to Parse every string of incoming data for Commas and if any found replace them with what for the Locate to work? The included Comma version will have been stored without Parsing or modifying as I have no idea what they may have entered. "I am at work, but not happy" is a valid input by the user.
Thanks.
Do not concatenate all the search values into a single string. Instead, place them as distinct items in the array, just like the example in the documentation. That is, don't include the commas in the string:
tbl.Locate('LName;FName;Stuff',
VarArrayOf([LName, FName, Stuff]), [loCaseInsensitive]);

Resources