<?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: Subsearch as eval expression works with strings, but not fields in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-as-eval-expression-works-with-strings-but-not-fields/m-p/348229#M103119</link>
    <description>&lt;P&gt;Before the search executes, the subsearch in brackets executes and sets the value to the right of the equals sign to "a".  The middle statement executed in the search thus reads... &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval a = "if(1&amp;lt;2, 1, 0)", foo = a
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Your output seems pretty reasonable for that code.&lt;/P&gt;</description>
    <pubDate>Mon, 24 Apr 2017 23:27:01 GMT</pubDate>
    <dc:creator>DalJeanis</dc:creator>
    <dc:date>2017-04-24T23:27:01Z</dc:date>
    <item>
      <title>Subsearch as eval expression works with strings, but not fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-as-eval-expression-works-with-strings-but-not-fields/m-p/348225#M103115</link>
      <description>&lt;P&gt;I have a situation where I want to use a subsearch to resolve to a conditional expression in an if statement - a.k.a. &lt;CODE&gt;eval x = [mysubsearch]&lt;/CODE&gt;using &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/return"&gt;return&lt;/A&gt;.   &lt;/P&gt;

&lt;P&gt;When I return a string literal the eval works.  &lt;/P&gt;

&lt;P&gt;However, when return a field whose value is that same string the eval does not work.&lt;/P&gt;

&lt;P&gt;Anybody know why or how to address this?&lt;/P&gt;

&lt;H3&gt;examples:&lt;/H3&gt;

&lt;H4&gt;works:&lt;/H4&gt;

&lt;P&gt;&lt;EM&gt;search:&lt;/EM&gt;&lt;BR /&gt;
&lt;CODE&gt;| makeresults | eval foo = [|makeresults | eval str = "if(1&amp;lt;2, 1, 0)" | return $str] | table foo&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;&lt;EM&gt;result:&lt;/EM&gt;&lt;/P&gt;

&lt;H2&gt;`foo&lt;/H2&gt;

&lt;P&gt;1`&lt;/P&gt;

&lt;H4&gt;does not work:&lt;/H4&gt;

&lt;P&gt;&lt;EM&gt;search:&lt;/EM&gt;&lt;BR /&gt;
&lt;CODE&gt;| makeresults |  eval a = "if(1&amp;lt;2, 1, 0)", foo = [|makeresults | eval str = a | return $str] | table foo&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;&lt;EM&gt;result:&lt;/EM&gt;&lt;BR /&gt;
&lt;CODE&gt;FATAL: Error in 'eval' command: Failed to parse the provided arguments. Usage: eval dest_key = expression&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Apr 2017 11:55:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Subsearch-as-eval-expression-works-with-strings-but-not-fields/m-p/348225#M103115</guid>
      <dc:creator>jmeyers_splunk</dc:creator>
      <dc:date>2017-04-24T11:55:12Z</dc:date>
    </item>
    <item>
      <title>Re: Subsearch as eval expression works with strings, but not fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-as-eval-expression-works-with-strings-but-not-fields/m-p/348226#M103116</link>
      <description>&lt;P&gt;Mostly a guess, but I believe it is not working, because your subsearch is parsed before your outer eval assigns the value to field a, which results in your subsearch eval to be &lt;EM&gt;eval str = NULL&lt;/EM&gt;. &lt;BR /&gt;
If I change your search to &lt;CODE&gt;| makeresults | eval a = "if(1&amp;lt;2, 1, 0)" | eval foo = [|makeresults | eval a = 123 | eval str = a | return $str] | table foo&lt;/CODE&gt;&lt;BR /&gt;
the error disappears, because a is defined within the subsearch. Obviously, it doesn't have the outcome you are looking for.&lt;/P&gt;

&lt;P&gt;Now, whether that is a bug or intended behavior, I cannot say.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Apr 2017 19:32:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Subsearch-as-eval-expression-works-with-strings-but-not-fields/m-p/348226#M103116</guid>
      <dc:creator>s2_splunk</dc:creator>
      <dc:date>2017-04-24T19:32:03Z</dc:date>
    </item>
    <item>
      <title>Re: Subsearch as eval expression works with strings, but not fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-as-eval-expression-works-with-strings-but-not-fields/m-p/348227#M103117</link>
      <description>&lt;P&gt;First, you need to understand when subsearches are performed.  Basically, they are performed first, before the search they are part of.  &lt;/P&gt;

&lt;P&gt;Therefore, in your first (working) example, $str is returned by the subsearch, THEN the &lt;CODE&gt;eval foo&lt;/CODE&gt; is performed.  &lt;/P&gt;

&lt;P&gt;In your second example, the subsearch is executed, but the token "a" has no value at all on the right side of the subsearch eval.  The middle statement in the subsearch is therefore &lt;CODE&gt;| eval str =  |&lt;/CODE&gt;, which is invalid.  (It is not interpreted the same as &lt;CODE&gt;| eval str = null() |&lt;/CODE&gt; would have been.)  &lt;/P&gt;

&lt;P&gt;For certain kinds of subsearches, such as those created by &lt;CODE&gt;map&lt;/CODE&gt;, you can have a token that is filled in from the outer search.  Those tokens will have a $ before and after them.  For instance, something like this would work, subject to the restrictions of &lt;CODE&gt;map&lt;/CODE&gt;...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval a = "if(1&amp;lt;2, 1, 0)" 
| map search="makeresults | eval foo = [|makeresults | eval str = $a$ | return $str] " 
| table foo
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Apr 2017 23:12:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Subsearch-as-eval-expression-works-with-strings-but-not-fields/m-p/348227#M103117</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-04-24T23:12:02Z</dc:date>
    </item>
    <item>
      <title>Re: Subsearch as eval expression works with strings, but not fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-as-eval-expression-works-with-strings-but-not-fields/m-p/348228#M103118</link>
      <description>&lt;P&gt;I do understand that point.  Perhaps my confusion comes from this other scenario that someone shared:&lt;BR /&gt;
&lt;CODE&gt;| makeresults |  eval a = "if(1&amp;lt;2, 1, 0)", foo = [|makeresults | eval str = "a" | return $str] | table foo&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Which yields:&lt;/P&gt;

&lt;H2&gt;`foo&lt;/H2&gt;

&lt;P&gt;if(1&amp;lt;2, 1, 0)`&lt;/P&gt;</description>
      <pubDate>Mon, 24 Apr 2017 23:17:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Subsearch-as-eval-expression-works-with-strings-but-not-fields/m-p/348228#M103118</guid>
      <dc:creator>jmeyers_splunk</dc:creator>
      <dc:date>2017-04-24T23:17:18Z</dc:date>
    </item>
    <item>
      <title>Re: Subsearch as eval expression works with strings, but not fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-as-eval-expression-works-with-strings-but-not-fields/m-p/348229#M103119</link>
      <description>&lt;P&gt;Before the search executes, the subsearch in brackets executes and sets the value to the right of the equals sign to "a".  The middle statement executed in the search thus reads... &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval a = "if(1&amp;lt;2, 1, 0)", foo = a
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Your output seems pretty reasonable for that code.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Apr 2017 23:27:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Subsearch-as-eval-expression-works-with-strings-but-not-fields/m-p/348229#M103119</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-04-24T23:27:01Z</dc:date>
    </item>
    <item>
      <title>Re: Subsearch as eval expression works with strings, but not fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-as-eval-expression-works-with-strings-but-not-fields/m-p/348230#M103120</link>
      <description>&lt;P&gt;I'll just chalk that up to "strange occurrences"&lt;/P&gt;</description>
      <pubDate>Mon, 24 Apr 2017 23:39:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Subsearch-as-eval-expression-works-with-strings-but-not-fields/m-p/348230#M103120</guid>
      <dc:creator>jmeyers_splunk</dc:creator>
      <dc:date>2017-04-24T23:39:05Z</dc:date>
    </item>
  </channel>
</rss>

