Just a quick note today that if you split a CSV string in Scala, you should also (immediately) do a
trim on each resulting element in the array. The Scala REPL shows why this is necessary:
A Scala substring example: I ran into a situation today where I wanted to get a string after the Nth occurrence of another string, in this case after the 6th occurrence of a “:” character. There are probably many ways to determine the Nth occurrence of a string in another string, but as a quick example, this is what I did.
First, I started with this string:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 1.3, “How to Split Strings in Scala.”
You want to split a string into parts based on a field separator, such as a string you get from a CSV or pipe-delimited file.
Use one of the split methods that are available on String objects:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.5, “How to process a CSV file in Scala.”
You want to process the lines in a CSV file, either handling one line at a time or storing them in a two-dimensional array.
Combine Recipe 12.1, “How to Open and Read a Text File” with Recipe 1.3, “Splitting Strings”. Given a simple CSV file like this named finance.csv:
Scala String FAQ: How do I split a
String in Scala based on a field separator, such as a string I get from a comma-separated value (CSV) or pipe-delimited file.
Use one of the
split methods that are available on
A few weeks ago when I upgraded my iPhone 3G to iOS 4, I hooked up my iPhone to my old Windows computer, where it was originally sync'd. Downloading my iPhone photos onto my Windows XP computer was a pain, and worse than that, in the process, the iPhone photos were pulled off the phone in some random order, so all the photos are completely out of order when I look at them in the Windows Explorer or the Mac Finder.
Perl Apache log file FAQ: Can you demonstrate how to read an Apache access log file in Perl (How to parse an Apache access log file in Perl)?
I've provided Perl examples before that can be used to read and parse an Apache log file ("How many RSS feed readers do I have?", "A Perl program to read an Apache access log file"), but to make this code a little easier to find, I'm breaking that code out here.
Perl string processing FAQ: How can I process every character in a Perl string?
I recently had to write some Perl code to process every word in a file, and that made me wonder how to process every character in a Perl string. I didn't know how to do this, but I just cracked open my copy of the Perl Cookbook, and found a couple of possible solutions.