<?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: Why is my eval if() not working consistantly in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Why-is-my-eval-if-not-working-consistantly/m-p/297301#M89711</link>
    <description>&lt;P&gt;I know you say you tried renaming the field before the eval, but I suspect that there might have been an error when you were testing that. Compare the following two:&lt;BR /&gt;
&lt;CODE&gt;| stats count | eval test.test1.testing="thing", test2="other", test3="thing" | foreach test.testing1.testing test2 test3 [ eval &amp;lt;&amp;gt;=if(&amp;lt;&amp;gt;="thing","renamed",&amp;lt;&amp;gt;) ]&lt;/CODE&gt;&lt;BR /&gt;
and&lt;BR /&gt;
&lt;CODE&gt;| stats count | eval test.test1.testing="thing", test2="other", test3="thing" | rename test.test1.testing AS test1 | foreach test1 test2 test3 [ eval &amp;lt;&amp;gt;=if(&amp;lt;&amp;gt;="thing","renamed",&amp;lt;&amp;gt;) ]&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;When I run these, the first shows this - which matches your test results (note that the value of the first variable remains unchanged):&lt;BR /&gt;
&lt;PRE&gt;count  test.test1.testing  test2   test3&lt;BR /&gt;
0       thing                other  renamed&lt;/PRE&gt;&lt;/P&gt;

&lt;P&gt;and the second (which renames the field name &lt;CODE&gt;test.test1.testing&lt;/CODE&gt; to &lt;CODE&gt;test1&lt;/CODE&gt; before the &lt;CODE&gt;foreach&lt;/CODE&gt; and &lt;CODE&gt;eval&lt;/CODE&gt; loop) gives me the desired result:&lt;BR /&gt;
&lt;PRE&gt;count  test1   test2   test3&lt;BR /&gt;
0       renamed  other  renamed&lt;/PRE&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 13 Oct 2017 20:34:44 GMT</pubDate>
    <dc:creator>elliotproebstel</dc:creator>
    <dc:date>2017-10-13T20:34:44Z</dc:date>
    <item>
      <title>Why is my eval if() not working consistantly</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-my-eval-if-not-working-consistantly/m-p/297300#M89710</link>
      <description>&lt;P&gt;I'm having a difficult time getting what I believe is a simple eval command to work as I would expect.  What I'm trying to accomplish is to convert a 1 or 0 into Yes or No respectively.  I'm able to do so just fine on one field, but 2 others are giving me a problem.  Here's the search I'm running:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rest /servicesNS/-/-/saved/searches 
| search action.myAction=1 
| foreach action.myAction.param.myParam1 action.myAction.param.myParam2 is_scheduled [eval &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=if(isnull(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;),&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,if(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=1,"Yes","No"))]
| rename action.myAction.param.myParam1 as param1, action.myAction.param.myParam2 as param2&amp;lt;/pre&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I had to add the isnull check as the 2 param fields do not always have data in them and the search would not run without the isnull.  This search does result in Yes/No values in the is_scheduled field, but the param fields remain unchanged.  To my knowledge, Splunk is treating them as numbers as they are right-justified in the results table.&lt;/P&gt;

&lt;P&gt;Here is some sample output from the above search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;title, param1, param2, is_scheduled
alert1, , yes
alert2, 1, 1, Yes
alert3, 1, 0, Yes
alert4, 0, 0, Yes
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I have also tried adding another field to test whether the data is a string, number or null, but end up with very strange results from that.  I added the following lines between the search and foreach lines to get the results below:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval isNumber=if(isNum(action.myAction.param.myParam1),"yes","no")
| eval isString=if(isStr(action.myAction.param.myParam1),"yes","no")
| eval isNull=if(isNull(action.myAction.param.myParam1),"yes","no")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Results:&lt;BR /&gt;
    title, param1, param2, is_scheduled, isNumber, isString, isNull&lt;BR /&gt;
    alert1, , yes, no, no, yes&lt;BR /&gt;
    alert2, 1, 1, Yes, no, no, yes&lt;BR /&gt;
    alert3, 1, 0, Yes, no, no, yes&lt;BR /&gt;
    alert4, 0, 0, Yes, no, no, yes&lt;/P&gt;

&lt;P&gt;I have copied and pasted the field name everywhere within the command to make sure I haven't typo'd anything and I have tried renaming the fields prior to the eval command and using the renamed field instead of the original, but that changes nothing.  I have also tried doing it outside a foreach loop, but still get the same results.&lt;/P&gt;

&lt;P&gt;What am I missing?  Is there a better way to accomplish what I'm trying to do?&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 19:10:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-my-eval-if-not-working-consistantly/m-p/297300#M89710</guid>
      <dc:creator>rrustong</dc:creator>
      <dc:date>2017-10-13T19:10:05Z</dc:date>
    </item>
    <item>
      <title>Re: Why is my eval if() not working consistantly</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-my-eval-if-not-working-consistantly/m-p/297301#M89711</link>
      <description>&lt;P&gt;I know you say you tried renaming the field before the eval, but I suspect that there might have been an error when you were testing that. Compare the following two:&lt;BR /&gt;
&lt;CODE&gt;| stats count | eval test.test1.testing="thing", test2="other", test3="thing" | foreach test.testing1.testing test2 test3 [ eval &amp;lt;&amp;gt;=if(&amp;lt;&amp;gt;="thing","renamed",&amp;lt;&amp;gt;) ]&lt;/CODE&gt;&lt;BR /&gt;
and&lt;BR /&gt;
&lt;CODE&gt;| stats count | eval test.test1.testing="thing", test2="other", test3="thing" | rename test.test1.testing AS test1 | foreach test1 test2 test3 [ eval &amp;lt;&amp;gt;=if(&amp;lt;&amp;gt;="thing","renamed",&amp;lt;&amp;gt;) ]&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;When I run these, the first shows this - which matches your test results (note that the value of the first variable remains unchanged):&lt;BR /&gt;
&lt;PRE&gt;count  test.test1.testing  test2   test3&lt;BR /&gt;
0       thing                other  renamed&lt;/PRE&gt;&lt;/P&gt;

&lt;P&gt;and the second (which renames the field name &lt;CODE&gt;test.test1.testing&lt;/CODE&gt; to &lt;CODE&gt;test1&lt;/CODE&gt; before the &lt;CODE&gt;foreach&lt;/CODE&gt; and &lt;CODE&gt;eval&lt;/CODE&gt; loop) gives me the desired result:&lt;BR /&gt;
&lt;PRE&gt;count  test1   test2   test3&lt;BR /&gt;
0       renamed  other  renamed&lt;/PRE&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 20:34:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-my-eval-if-not-working-consistantly/m-p/297301#M89711</guid>
      <dc:creator>elliotproebstel</dc:creator>
      <dc:date>2017-10-13T20:34:44Z</dc:date>
    </item>
    <item>
      <title>Re: Why is my eval if() not working consistantly</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-my-eval-if-not-working-consistantly/m-p/297302#M89712</link>
      <description>&lt;P&gt;You are correct. I know I did test with renaming before the eval, but must have done something wrong as it's working now.&lt;/P&gt;

&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 21:44:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-my-eval-if-not-working-consistantly/m-p/297302#M89712</guid>
      <dc:creator>rrustong</dc:creator>
      <dc:date>2017-10-13T21:44:52Z</dc:date>
    </item>
    <item>
      <title>Re: Why is my eval if() not working consistantly</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-my-eval-if-not-working-consistantly/m-p/297303#M89713</link>
      <description>&lt;P&gt;Glad I could help! I know how frustrating it is to test all possible error conditions for complex searches. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;/P&gt;</description>
      <pubDate>Sat, 14 Oct 2017 01:31:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-my-eval-if-not-working-consistantly/m-p/297303#M89713</guid>
      <dc:creator>elliotproebstel</dc:creator>
      <dc:date>2017-10-14T01:31:18Z</dc:date>
    </item>
  </channel>
</rss>

