multiple-value strconv.ParseFloat() in single-value context

8,632 views
Skip to first unread message

Steven Roste

unread,
Jun 20, 2012, 2:37:56 PM6/20/12
to golan...@googlegroups.com
I'm looking for a way to store multiple string-converted-to-float values.

I'm extracting multiple specific matching data values from CSV files. This data is extracted in string format and then I convert it into float64. However, I can't seem to store multiple values that are converted simultaneously into a variable. I realize I may need to modify my for-loop or if-statements, but I'm not quite sure how. Is there a way I can make it a multiple-value context?

Any help?

./CSVreader.go:202: multiple-value strconv.ParseFloat() in single-value context
./CSVreader.go:203: multiple-value strconv.ParseFloat() in single-value context
./CSVreader.go:204: multiple-value strconv.Atoi() in single-value context

Primary problems are lines 202, 203, and 204. Not sure if my for-loop on line 194 or if-statement on line 201 are coded properly for what I need. There are other problems with my code, but I'm not worried about them at the moment.

Paul Borman

unread,
Jun 20, 2012, 2:40:58 PM6/20/12
to Steven Roste, golan...@googlegroups.com
You need to collect the ere value:

         lats[j], err = strconv.ParseFloat(row1[0],64) // store row value from column 1

You probably should check the error as well.

    -Paul

Steven Roste

unread,
Jun 21, 2012, 1:07:26 PM6/21/12
to golan...@googlegroups.com, Steven Roste
I've added the error checking code, but it doesn't tell me anything new.

./CSVreader.go:202: cannot assign float64 to lats (type [][]float64) in multiple assignment
./CSVreader.go:206: cannot assign float64 to lngs (type [][]float64) in multiple assignment
./CSVreader.go:210: cannot assign int to idtag (type [][]int) in multiple assignment

The issues is that I have multiple strings in a variable, and I'm trying to convert all of the strings each into their own float64, and store all of these float64s into another variable. Collecting the error doesn't change the fact that I can't do what I'm trying to do.

Paul Borman

unread,
Jun 21, 2012, 1:50:30 PM6/21/12
to Steven Roste, golan...@googlegroups.com
I don't know what is on your line 202, but it looks like you are trying to assign a slice of float64 slices (lats) to a float64. 

Steven Roste

unread,
Jun 21, 2012, 2:07:43 PM6/21/12
to golan...@googlegroups.com, Steven Roste
So this program is reading a bunch of fields from a CSV file. The CSV reader reads everything in as strings (including numbers). However, I need to be able to do math with these numbers, which I can't do with strings. I'm trying to convert the strings into numbers, but there are multiple strings of numbers that I'm trying to convert.

For example:
If you have a string of:   "-95.342 34.264 -32.756 45.234"
How would you separate out each number (-95.342) within the string into a float64?

Andy Balholm

unread,
Jun 21, 2012, 2:27:46 PM6/21/12
to golan...@googlegroups.com, Steven Roste
On Thursday, June 21, 2012 11:07:43 AM UTC-7, Steven Roste wrote:
If you have a string of:   "-95.342 34.264 -32.756 45.234"
How would you separate out each number (-95.342) within the string into a float64?

Use fmt.Sscanf 

Steven Roste

unread,
Jun 21, 2012, 2:51:01 PM6/21/12
to golan...@googlegroups.com, Steven Roste
You must be psychic, because I just found that exact command, and was wondering "maybe this would work..."

Now for syntax...

Thanks!

Steven Roste

unread,
Jun 21, 2012, 3:32:30 PM6/21/12
to golan...@googlegroups.com, Steven Roste
I found a solution that didn't require Sscanf. It had to do with the syntax of how I was defining and storing information into each variable. The strconv.ParseFloat() was the way to go.

Steven Roste

unread,
Jun 27, 2012, 11:34:14 AM6/27/12
to golan...@googlegroups.com, Steven Roste
So Here's a copy of my current program: http://play.golang.org/p/PmQz_bOPz0
It's not finished and it requires some other files, so don't try running it, but you should be able to find the context.
Lines 310 through 347 should help you. You can ignore most of what's in there.

You need to import package "strconv" 
The trick is using "make" to make a slice of float64 values for your variable, using a for-loop, and cycling through the slice appropriately (which was difficult to get the syntax down correctly), and then using "strconv.ParseFloat" correctly with appropriate syntax.

It really came down to me not understanding Go's syntax well enough to implement it. It took a number of tries.


On Tuesday, June 26, 2012 2:24:11 PM UTC-5, snamber wrote:
Hi Steven,

could you please post your solution!? I've been running into the same problem and would appreciate any hints and any knowledge that you found very much.

Thanks,
snamber
Reply all
Reply to author
Forward
0 new messages