csv

A collection of Gnuplot examples

I needed to use Gnuplot a little bit over the last few days, mostly to create 2D line charts, and these are my brief notes on how to get started with Gnuplot. If you haven’t used it before, it’s a pretty amazing tool.

Jumping right in ...

Back to top

Installing gnuplot

Use MacPorts or Homebrew to install Gnuplot on Mac OS X systems:

port install gnuplot
brew install gnuplot
Back to top

Sample data files

My examples use the following 2-column and 4-column data files:

Back to top

How to split strings in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 1.3, “How to Split Strings in Scala.”

Problem

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.

Solution

Use one of the split methods that are available on String objects:

Two more Textmate commands (capitalize, CSV to list)

As a “note to self,” I wrote two more Textmate commands yesterday, one to capitalize each word in a selection of words, and another to convert a CSV list of words to a simple list. Here’s the source code for the Capitalize command:

#!/bin/sh

perl -ne 'print ucfirst $_'

The $_ portion of that Perl command isn’t required, but I include it as a reminder to myself about how Textmate commands and snippets work.

Here’s the source code for my Textmate command that uses the Unix tr command to convert a CSV list of words (such as a paragraph of comma-separated words) into a simple list of words:

#!/bin/sh

tr , "\n"

As you can see, those commands are fairly simple. If you know Unix/Linux and then know a little about how to write Textmate commands, you can usually get it to do what you want. I like that you can use any Mac/Unix programming language or tool to solve the problem at hand.

How to process a CSV file in Scala

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.”

Problem

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.

Solution

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:

A Scala “split string” example

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.

Solution

Use one of the split methods that are available on String objects:

A PHP "select from where in" query example

If you ever need the source code for a PHP "SELECT FROM foo WHERE id in (1,2,3)" example, I hope the following PHP function will be a decent example to work from. I turned a given list of database table id values into a CSV list, then use that string in a "SELECT FROM ... WHERE ... IN ... (list)" SQL query.

I'm not going to explain it today, but I will share the PHP source code for your reading enjoyment (and problem solving).

A PHP array to CSV string example

PHP array to CSV FAQ: How do your convert a PHP array to a CSV string?

Converting a PHP array to a PHP CSV string is so easy, it actually felt like I trick question when I just found the answer in the PHP docs.

In my case, I just had a PHP array named $tags, and I wanted to convert it to a PHP CSV string. To do this, all I had to do was use the PHP implode function, like this:

SQLite CSV import examples

SQLite CSV FAQ: How do I import a CSV file into a SQLite database table?

If you’re not using an autoincrement (primary key) field in your database table, importing CSV file data into a SQLite database is straightforward, though you may have to do some work to clean up your data first. Let’s take a look at how this works.

A SQLite CSV import example

As a first example, assume that I have a SQLite database table defined like this:

Perl CSV file column extraction

Perl CSV column data extraction FAQ: Can you share an example of how to extract one or more columns from a Perl CSV file or other similarly-formatted flat text file?

Perl is a terrific language for text processing, but several readers have written wondering about how to extract columns of data from text files with Perl. For instance, when you have a text database that looks like this: