<?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: How to use variable within foreach command? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-variable-within-foreach-command/m-p/347060#M102756</link>
    <description>&lt;P&gt;The &lt;CODE&gt;foreach&lt;/CODE&gt; command iterates over a set of fields - not events. So if I had 6 fields with names that all started with Percent, I could use the foreach command to do the same set of actions for each field Percent*.&lt;/P&gt;

&lt;P&gt;You don't specify iteration in Splunk like in programming languages. In fact, you generally don't specify iteration at all.&lt;/P&gt;

&lt;P&gt;I suspect that what you want is quite different. You have a set of events and you want to calculate a few field for the various subsets of events.  Try something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="snow_incident"
| head 1 
| spath path=result{} output=x
| fields - _raw
| mvexpand x
| spath input=x
| eval Uptime = if(match(major_inc_temp,u_major_incident),"100","200")
| eval major_inc_temp = u_major_incident
| table major_inc_temp, Uptime, cmdb_ci
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The next question is: what do you want to accomplish? Are you trying to find a maximum of something? More info would really be helpful.&lt;/P&gt;</description>
    <pubDate>Wed, 26 Apr 2017 20:53:53 GMT</pubDate>
    <dc:creator>lguinn2</dc:creator>
    <dc:date>2017-04-26T20:53:53Z</dc:date>
    <item>
      <title>How to use variable within foreach command?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-variable-within-foreach-command/m-p/347059#M102755</link>
      <description>&lt;P&gt;I'm looping through JSON array and compare each value using a temporary variable but due to some reason the temporary variable is not getting set when I iterate the JSON array.&lt;/P&gt;

&lt;P&gt;Please find the code snippet below&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="snow_incident"
| head 1 
| spath path=result{} output=x
| fields - _raw
| mvexpand x
| spath input=x
| eval major_inc_temp = ""
| foreach x [eval Uptime = if(match(major_inc_temp,u_major_incident),"100","200")
| eval major_inc_temp = u_major_incident] | table major_inc_temp,Uptime, cmdb_ci
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Please do let me know how do I address this problem&lt;/P&gt;</description>
      <pubDate>Wed, 26 Apr 2017 16:42:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-variable-within-foreach-command/m-p/347059#M102755</guid>
      <dc:creator>sats2020</dc:creator>
      <dc:date>2017-04-26T16:42:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to use variable within foreach command?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-variable-within-foreach-command/m-p/347060#M102756</link>
      <description>&lt;P&gt;The &lt;CODE&gt;foreach&lt;/CODE&gt; command iterates over a set of fields - not events. So if I had 6 fields with names that all started with Percent, I could use the foreach command to do the same set of actions for each field Percent*.&lt;/P&gt;

&lt;P&gt;You don't specify iteration in Splunk like in programming languages. In fact, you generally don't specify iteration at all.&lt;/P&gt;

&lt;P&gt;I suspect that what you want is quite different. You have a set of events and you want to calculate a few field for the various subsets of events.  Try something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="snow_incident"
| head 1 
| spath path=result{} output=x
| fields - _raw
| mvexpand x
| spath input=x
| eval Uptime = if(match(major_inc_temp,u_major_incident),"100","200")
| eval major_inc_temp = u_major_incident
| table major_inc_temp, Uptime, cmdb_ci
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The next question is: what do you want to accomplish? Are you trying to find a maximum of something? More info would really be helpful.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Apr 2017 20:53:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-variable-within-foreach-command/m-p/347060#M102756</guid>
      <dc:creator>lguinn2</dc:creator>
      <dc:date>2017-04-26T20:53:53Z</dc:date>
    </item>
  </channel>
</rss>

