<?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 Multiple operations from a single if condition in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Multiple-operations-from-a-single-if-condition/m-p/690567#M235182</link>
    <description>&lt;P&gt;Is it possible to action multiple operations in a single if condition, like what can be done in other languages?&lt;BR /&gt;&lt;BR /&gt;For example, in other scripting languages this can be done:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;if(field==1){
  group=group+1;
  groups=groups+","+group;
}
else
{
  //this is a comment, do nothing
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can this be done in splunk?&lt;/P&gt;</description>
    <pubDate>Wed, 12 Jun 2024 23:00:58 GMT</pubDate>
    <dc:creator>cjohnk</dc:creator>
    <dc:date>2024-06-12T23:00:58Z</dc:date>
    <item>
      <title>Multiple operations from a single if condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Multiple-operations-from-a-single-if-condition/m-p/690567#M235182</link>
      <description>&lt;P&gt;Is it possible to action multiple operations in a single if condition, like what can be done in other languages?&lt;BR /&gt;&lt;BR /&gt;For example, in other scripting languages this can be done:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;if(field==1){
  group=group+1;
  groups=groups+","+group;
}
else
{
  //this is a comment, do nothing
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can this be done in splunk?&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2024 23:00:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Multiple-operations-from-a-single-if-condition/m-p/690567#M235182</guid>
      <dc:creator>cjohnk</dc:creator>
      <dc:date>2024-06-12T23:00:58Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple operations from a single if condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Multiple-operations-from-a-single-if-condition/m-p/690569#M235183</link>
      <description>&lt;P&gt;You can't do block ifs in Splunk, so you have to do all conditionals inside the &lt;STRONG&gt;| eval x=if(...) &lt;/STRONG&gt;construct&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2024 23:29:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Multiple-operations-from-a-single-if-condition/m-p/690569#M235183</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2024-06-12T23:29:28Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple operations from a single if condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Multiple-operations-from-a-single-if-condition/m-p/690570#M235184</link>
      <description>&lt;P&gt;Short answer is no.&lt;/P&gt;&lt;P&gt;Splunk SPL is not a procedural language (like some other languages). Essentially, the if function can be used to modify what is assigned by an eval command to a new or existing field in the event, although you can have multiple assignments in the same eval command e.g. | eval a=value1, b=value2&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2024 23:31:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Multiple-operations-from-a-single-if-condition/m-p/690570#M235184</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2024-06-12T23:31:14Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple operations from a single if condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Multiple-operations-from-a-single-if-condition/m-p/690581#M235187</link>
      <description>&lt;P&gt;As&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/225168"&gt;@ITWhisperer&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/6367"&gt;@bowesmana&lt;/a&gt;&amp;nbsp;said, SPL is not a procedural language and does not provide code block. &amp;nbsp;I do understand the semantic clarity, and maintainability of a code block. &amp;nbsp;So, I am going to use the specifics in your sample to give a very silly "block".&lt;/P&gt;&lt;P&gt;Obviously I have no idea what values are in field, group and groups. &amp;nbsp;So I made something up, with the constraint that group be numeric.&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;field&lt;/TD&gt;&lt;TD&gt;group&lt;/TD&gt;&lt;TD&gt;groups&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;60&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;The following will read like a block:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval bingo = if(field == 1, mvrange(group, group+1), null())
| foreach bingo mode=multivalue
    [eval group = &amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt; + 1, groups = groups . "," . &amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and the output is equivalent to your block code&lt;/P&gt;&lt;TABLE width="133px"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;field&lt;/TD&gt;&lt;TD&gt;group&lt;/TD&gt;&lt;TD&gt;groups&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="40px"&gt;0&lt;/TD&gt;&lt;TD width="40px"&gt;10&lt;/TD&gt;&lt;TD width="52.515625px"&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="40px"&gt;1&lt;/TD&gt;&lt;TD width="40px"&gt;21&lt;/TD&gt;&lt;TD width="52.515625px"&gt;30,20&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="40px"&gt;2&lt;/TD&gt;&lt;TD width="40px"&gt;30&lt;/TD&gt;&lt;TD width="52.515625px"&gt;60&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Is that code block? Not really. &amp;nbsp;Does it achieve semantic clarity? &amp;nbsp;Questionable. &amp;nbsp;But you are not repeating condition evaluation.&lt;/P&gt;&lt;P&gt;Also, if maintainability is super important, you can also do something like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| tojson group groups
| eval _raw = if(field == 1, json_set(_raw, "group", group + 1, "groups", groups . "," . group), _raw)
| fields - group groups
| spath&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In a roundabout way, this has the true spirit of a code block.&lt;/P&gt;&lt;P&gt;The above mock data is produced with the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults format=csv data="field, group
0, 10
1, 20
2, 30"
| streamstats sum(group) as groups
``` data emulation above ```&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2024 06:34:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Multiple-operations-from-a-single-if-condition/m-p/690581#M235187</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2024-06-13T06:34:33Z</dc:date>
    </item>
  </channel>
</rss>

