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!

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...

Let’s Get You Certified – Vegas-Style at .conf24

Are you ready to level up your Splunk game? Then, let’s get you certified live at .conf24 – our annual user ...