finding data within csv files

1,619 views
Skip to first unread message

FrosteRoste

unread,
Jun 18, 2012, 9:29:16 PM6/18/12
to golan...@googlegroups.com
Is there a way to find specific values within a CSV file using Go lang?

I have a program to read the CSV data into a variable, but now I need to find a specific row of data within the variable.

My variable contains Latitude, Longitude, Date, Time, Measurement, and misc data. I need to search for a specific Lat, Long, Date, and Time, and to be able to pull out the Measurement and misc data from the accompanying row.

Is there something similar to Excel's "vlookup" or "hlookup" command? or something like a "scan", "search", or "find"? If so, how would I go about implementing this command?

Thanks,

Steve

Andrew Gerrand

unread,
Jun 19, 2012, 10:17:01 AM6/19/12
to FrosteRoste, golan...@googlegroups.com
The simplest way is to scan over the records sequentially, looking for
the value you're interested in:

http://play.golang.org/p/2A3LXF3iYJ

If you have a lot of data, you may want to sort it and then do a
binary search. The sort package provides primitives for searching and
sorting:

http://golang.org/pkg/sort/#examples

Andrew

FrosteRoste

unread,
Jun 19, 2012, 12:09:12 PM6/19/12
to golan...@googlegroups.com, FrosteRoste
Thanks Andrew. That helps. I had considered that method. I guess I was curious if there was a simple function or command that accomplished that in 1 line of code

peterGo

unread,
Jun 19, 2012, 1:16:47 PM6/19/12
to golang-nuts
Steve,

Go provides the tools for you to construct a "one line" solution.
Write a measurement package.

For random access, consider a map. For example,

package measurement

import (
"time"
)

type MeasurementId struct {
Latitude float64
Longitude float64
Time time.Time
}

type MeasurementData struct {
Measurement float64
Data string
}

type Measurement struct {
MeasurementId
MeasurementData
}

type Measurements map[MeasurementId]*MeasurementData

func (m Measurements) Find(id *MeasurementId) *MeasurementData {
return m[*id]
}

Peter
Reply all
Reply to author
Forward
0 new messages