Splunk Search

Foreach weird bug in variable

greggz
Communicator

So I have this chunk of code

eval matched=0 | foreach UF* [eval matched = if(like('<<FIELD>>',valMask),matched+1,matched) | eval Property = if(matched>=1,Property,null)]

Amusingly if the matched variable equals 1, the Property will get null value, when I explicity say that if it is bigger or equal than 1, the Property should remain with her original value.

Proof matched = 1

alt text

Thoughts on this ?

Thanks

0 Karma
1 Solution

greggz
Communicator

So apparently, matched inside the foreach is still 0. Something like Lazy Evaluation, but I don't really know. Anyway.. this works

foreach UF* [eval matched = if(like('<<FIELD>>',valMask),matched+1,matched)] | eval Property =  if(matched>=1,Property,null) 

View solution in original post

0 Karma

greggz
Communicator

So apparently, matched inside the foreach is still 0. Something like Lazy Evaluation, but I don't really know. Anyway.. this works

foreach UF* [eval matched = if(like('<<FIELD>>',valMask),matched+1,matched)] | eval Property =  if(matched>=1,Property,null) 
0 Karma

elliotproebstel
Champion

The structure generally looks ok, so I'm kind of reaching here. Any chance Property was null before the second half of the foreach ran? If you remove the second eval clause, is Property always populated?

0 Karma

greggz
Communicator

The only possibility I'm seeing is that matched is still 0 inside the foreach. Like a lazy interation

0 Karma

greggz
Communicator

I removed the eval clause, and all the Properties get populated..

0 Karma

mayurr98
Super Champion

Can you try

eval matched=0 | foreach UF* [eval matched = if(like('<<FIELD>>',valMask),matched+1,matched) ]| foreach UF* [eval Property=if(matched>=1,Property,null)]
0 Karma

greggz
Communicator

Same result.. What was your reasoning ?

0 Karma
Get Updates on the Splunk Community!

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...

Tech Talk | Elevating Digital Service Excellence: The Synergy of Splunk RUM & APM

Elevating Digital Service Excellence: The Synergy of Real User Monitoring and Application Performance ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...