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!

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...

New in Observability Cloud - Explicit Bucket Histograms

Splunk introduces native support for histograms as a metric data type within Observability Cloud with Explicit ...