[go-nuts] Re: Compare values in a for loop that ranges over string array of array

2018-03-06 Thread timilsina . ashish . 03
Awesome, that makes so much sense. Thanks for explaining it and your help!

On Monday, March 5, 2018 at 4:03:33 PM UTC-7, Ignacio Gómez wrote:
>
> Hi, Ashish.
>
> If you have a map[string]int (or int 64, float 64, etc.) "m", doing
>
> m[key] += value
>
>
> is equivalent to this:
>
> m[key] = m[key] + value.
>
>
> Thus, on each iteration we sum the value at dataarray[j][1] (which you 
> stored at sumFloat) to the current value of sums[dataarray[j][0]] (on first 
> iteration, it just gets initialized to the zero value, in this case 0.0) in 
> order to sum values among the same letter, which are used as keys for the 
> map. So, in short, yes, that's accurate. Take a look at this to get some 
> more info on maps: https://blog.golang.org/go-maps-in-action
>
> El lunes, 5 de marzo de 2018, 18:35:11 (UTC-3), Ashish Timilsina escribió:
>>
>> Hi Ignacio,
>>
>> This is excellent, works perfectly. Thank you so much. 
>> I will try to do my research but just out of curiosity and make sure I 
>> understand the code, what does this line do? I also don't have proper 
>> understanding of golang maps 
>> sums[dataarray[j][0]] += sumFloat
>>
>>
>> So 'sum' map is taking the first index in dataarray as key, summing the 
>> second index and assigning it as the value to the key. As it is looping 
>> through the array, if it sees that the key is the same, it automatically 
>> sums it up? Is that accurate?
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Compare values in a for loop that ranges over string array of array

2018-03-05 Thread Ignacio Gómez
Hi, Ashish.

If you have a map[string]int (or int 64, float 64, etc.) "m", doing

m[key] += value


is equivalent to this:

m[key] = m[key] + value.


Thus, on each iteration we sum the value at dataarray[j][1] (which you 
stored at sumFloat) to the current value of sums[dataarray[j][0]] (on first 
iteration, it just gets initialized to the zero value, in this case 0.0) in 
order to sum values among the same letter, which are used as keys for the 
map. So, in short, yes, that's accurate. Take a look at this to get some 
more info on maps: https://blog.golang.org/go-maps-in-action

El lunes, 5 de marzo de 2018, 18:35:11 (UTC-3), Ashish Timilsina escribió:
>
> Hi Ignacio,
>
> This is excellent, works perfectly. Thank you so much. 
> I will try to do my research but just out of curiosity and make sure I 
> understand the code, what does this line do? I also don't have proper 
> understanding of golang maps 
> sums[dataarray[j][0]] += sumFloat
>
>
> So 'sum' map is taking the first index in dataarray as key, summing the 
> second index and assigning it as the value to the key. As it is looping 
> through the array, if it sees that the key is the same, it automatically 
> sums it up? Is that accurate?
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Compare values in a for loop that ranges over string array of array

2018-03-05 Thread timilsina . ashish . 03
Hi Ignacio,

This is excellent, works perfectly. Thank you so much. 
I will try to do my research but just out of curiosity and make sure I 
understand the code, what does this line do? I also don't have proper 
understanding of golang maps 
sums[dataarray[j][0]] += sumFloat


So 'sum' map is taking the first index in dataarray as key, summing the 
second index and assigning it as the value to the key. As it is looping 
through the array, if it sees that the key is the same, it automatically 
sums it up? Is that accurate?

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Compare values in a for loop that ranges over string array of array

2018-03-05 Thread Ignacio Gómez
You can use a map to keep track of a letters sum. Here's your example 
slightly modified (it uses float64 as you were using 
that): https://play.golang.org/p/98L9fDXSN_A

El lunes, 5 de marzo de 2018, 15:28:54 (UTC-3), Ashish Timilsina escribió:
>
> Hi,
>
> I have an array of array string ([][]string{}). I am looping through them 
> and trying to sum the second value of the arrays based on the first value. 
> For example: 
>  a := []string{"a", "1"}
>  a1 := []string{"a", "2"}
>  a2 := []string{"a", "3"}
>  b := []string{"b", "4"}
>  b1 := []string{"b", "1"}
>
> Sum all the 'a' values and 'b' values. 
> Here's the sample code: 
> package main
>
>
> import (
>  "fmt"
>  "log"
>  "strconv"
> )
>
>
> func main() {
>
>
>  a := []string{"a", "1"}
>  a1 := []string{"a", "2"}
>  a2 := []string{"a", "3"}
>  b := []string{"b", "4"}
>  b1 := []string{"b", "2"}
>  b2 := []string{"b", "1"}
>  dataarray := [][]string{}
>  dataarray = append(dataarray, a, a1, a2, b, b1, b2)
>  var totalSum float64
>  for j, _ := range dataarray {
>  sumFloat, err := strconv.ParseFloat(dataarray[j][1], 64)
>  if err != nil {
>  log.Fatal(err)
>  }
>  totalSum += sumFloat
>
>
>  }
>  fmt.Println(totalSum)
> }
>
> Right now, its summing all the values. The result I want is:
> a: 6
> b: 7
>
> Let me know if there's a way to do this. Here's the link to go playground: 
> https://play.golang.org/p/KL8GQ7LPJNi
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.