<?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 Re: foreach with more than one FIELD? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/foreach-with-more-than-one-FIELD/m-p/461851#M130209</link>
    <description>&lt;P&gt;Thank you Rich.  It works beautifully.  Now I understand what the MATCHSTR does.  &lt;/P&gt;</description>
    <pubDate>Wed, 16 Oct 2019 19:12:39 GMT</pubDate>
    <dc:creator>hriazi</dc:creator>
    <dc:date>2019-10-16T19:12:39Z</dc:date>
    <item>
      <title>foreach with more than one FIELD?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/foreach-with-more-than-one-FIELD/m-p/461849#M130207</link>
      <description>&lt;P&gt;Hello,&lt;BR /&gt;
In the code below, the first foreach sums the values in field A, and returns 21  (5+3+2+6+1+4=21).&lt;BR /&gt;
The second foreach counts the number of B field that has a value of 1, and it returns 4  (for B1,B2,B3,B6).&lt;/P&gt;

&lt;P&gt;|makeresults 1&lt;BR /&gt;
| eval  Selected_As_Total =0 &lt;BR /&gt;
| eval  A1=5 | eval A2=3 | eval A3=2 | eval A4=6 | eval A5=1 | eval A6=4 | eval B1=1 | eval B2=1 | eval B3=1 | eval B4=2 | eval B5=2 | eval B6=1    &lt;/P&gt;

&lt;P&gt;| foreach A* [eval Selected_As_Total = Selected_As_Total + &amp;lt;&amp;gt;]&lt;/P&gt;

&lt;P&gt;| eval Selected_Bs_Count = 0&lt;BR /&gt;
| foreach B* [ eval Selected_Bs_Count = Selected_Bs_Count + case(&amp;lt;&amp;gt;=1,1,1=1,0)]&lt;/P&gt;

&lt;P&gt;Question/problem: I need to get the total only for those A fields that their corresponding B field has a value of 1.&lt;BR /&gt;
So the in this example we would select  A1=5,A2=3,A3=2,A6=4  because B1=1,B2=1,B3=1,B6=1,  and the result would come to 5+3+2+4=14.&lt;/P&gt;

&lt;P&gt;How do I do that?  Is there a way to combine these two foreach commands into one?  In other words, is there way to have a foreach with two FIELDs?&lt;BR /&gt;
Is there another way to accomplish what I need?&lt;/P&gt;

&lt;P&gt;BTW, in each event there is a B field for every A field, but the number of A/B pairs could be different from one event to the next.&lt;/P&gt;

&lt;P&gt;I have searched the docs and Q/As and have not found a similar case. Thank you for any help you could offer.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Sep 2020 02:38:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/foreach-with-more-than-one-FIELD/m-p/461849#M130207</guid>
      <dc:creator>hriazi</dc:creator>
      <dc:date>2020-09-30T02:38:29Z</dc:date>
    </item>
    <item>
      <title>Re: foreach with more than one FIELD?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/foreach-with-more-than-one-FIELD/m-p/461850#M130208</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| foreach A* [eval Selected_As_Total = Selected_As_Total + if(B&amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;=1, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,0)]
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Oct 2019 18:57:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/foreach-with-more-than-one-FIELD/m-p/461850#M130208</guid>
      <dc:creator>richgalloway</dc:creator>
      <dc:date>2019-10-16T18:57:46Z</dc:date>
    </item>
    <item>
      <title>Re: foreach with more than one FIELD?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/foreach-with-more-than-one-FIELD/m-p/461851#M130209</link>
      <description>&lt;P&gt;Thank you Rich.  It works beautifully.  Now I understand what the MATCHSTR does.  &lt;/P&gt;</description>
      <pubDate>Wed, 16 Oct 2019 19:12:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/foreach-with-more-than-one-FIELD/m-p/461851#M130209</guid>
      <dc:creator>hriazi</dc:creator>
      <dc:date>2019-10-16T19:12:39Z</dc:date>
    </item>
  </channel>
</rss>

