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!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...