<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Arithmetic on multi field values in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Arithmetic-on-multi-field-values/m-p/469309#M132075</link>
    <description>&lt;P&gt;I am new to Splunk, and I need to perform arithmetic on some multi-field values.  What is the best way to do this?  Here is an example of an event (where the "stuff" field is an array containing any number of key-value pairs with "A" and "B"):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;   event1 {
         name:   foo
         stuff: [
            {
             A:  10 
             B:  220.0  
            }   
            {
             A:  2  
             B:  50.0   
            }   
        ]   
    }
   event2 {
         name:   foo
         stuff: [
            {
             A:  2  
             B:  100.0  
            }   
        ]   
    }
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Here is the search I am using:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;my search&amp;gt;
| mvexpand stuff{} 
| rename stuff{}.* as *  
| eval test=B/A
| table _time A B test
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;However, test is empty whenever there is more than 1 "stuff" in my event.  In the example above: test=null, null, 50&lt;/P&gt;

&lt;P&gt;My goal is to calculate "test" so that: test=22, 25, 50&lt;/P&gt;</description>
    <pubDate>Sun, 09 Feb 2020 00:49:15 GMT</pubDate>
    <dc:creator>rtakatsuka</dc:creator>
    <dc:date>2020-02-09T00:49:15Z</dc:date>
    <item>
      <title>Arithmetic on multi field values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Arithmetic-on-multi-field-values/m-p/469309#M132075</link>
      <description>&lt;P&gt;I am new to Splunk, and I need to perform arithmetic on some multi-field values.  What is the best way to do this?  Here is an example of an event (where the "stuff" field is an array containing any number of key-value pairs with "A" and "B"):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;   event1 {
         name:   foo
         stuff: [
            {
             A:  10 
             B:  220.0  
            }   
            {
             A:  2  
             B:  50.0   
            }   
        ]   
    }
   event2 {
         name:   foo
         stuff: [
            {
             A:  2  
             B:  100.0  
            }   
        ]   
    }
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Here is the search I am using:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;my search&amp;gt;
| mvexpand stuff{} 
| rename stuff{}.* as *  
| eval test=B/A
| table _time A B test
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;However, test is empty whenever there is more than 1 "stuff" in my event.  In the example above: test=null, null, 50&lt;/P&gt;

&lt;P&gt;My goal is to calculate "test" so that: test=22, 25, 50&lt;/P&gt;</description>
      <pubDate>Sun, 09 Feb 2020 00:49:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Arithmetic-on-multi-field-values/m-p/469309#M132075</guid>
      <dc:creator>rtakatsuka</dc:creator>
      <dc:date>2020-02-09T00:49:15Z</dc:date>
    </item>
    <item>
      <title>Re: Arithmetic on multi field values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Arithmetic-on-multi-field-values/m-p/469310#M132076</link>
      <description>&lt;P&gt;your current result:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval _raw="\"event1\":{\"name\":\"foo\",\"stuff\":[{\"A\":10,\"B\":220.0},{\"A\":2,\"B\":50.0}]}
\"event2\":{\"name\":\"foo\",\"stuff\":[{\"A\":2,\"B\":100.0}]}"
| makemv delim="
" _raw
| stats count by _raw
| rex "(?&amp;lt;json&amp;gt;{.*)"
| spath input=json
| rename stuff{}.* as *
| eval test=B / A
| table _time A B test
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;recommend:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your search
| rename stuff{}.* as *
| eval temp=mvzip(A,B)
| mvexpand temp
| table _time temp
| eval A = mvindex(split(temp,","),0), B = mvindex(split(temp,","),1), test = A / B
| table _time A B test
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;HI, how about this?&lt;/P&gt;</description>
      <pubDate>Sun, 09 Feb 2020 02:03:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Arithmetic-on-multi-field-values/m-p/469310#M132076</guid>
      <dc:creator>to4kawa</dc:creator>
      <dc:date>2020-02-09T02:03:39Z</dc:date>
    </item>
  </channel>
</rss>

