Hi everyone,
I'm trying to get my head around this foreach statement but no luck so far ...
Foreach seems like the most obscure command ever ....
foreach * means everything ... but everything in what?
foreach ... in the example on the docs ... they use new variables but I want to know how you can/are supposed to run through your dataset?
Say you have a simple dataset as a result:
bytes in bytes out
www1 1200 3000
www2 4500 3600
db1 4900 5000
these are values in bytes and I want them converted in kBs but let's say I only want it to be done to the bytes out of every webserver (www*) and the bytes in need to stay in bytes.
How can I match that with this foreach?
Thanks
Kristof
Hi, Kristof!
foreach
is used when you need to apply the same command (of several commands) to multiple columns (fields). For example, if you need to transform both bytes in
and bytes out
to kB, you could write smth like that:
| foreach bytes*
[ eval <<FIELD>>_kB = round('<<FIELD>>' / 1024) ]
In your case foreach
command is not so necessary. But you can use it in this way:
| foreach "bytes out"
[ eval <<FIELD>> = if(like(server, "www%"), round('<<FIELD>>' / 1024), '<<FIELD>>') ]
The foreach
command works on specified columns of every rows in the search result. So, | foreach * [
, will run the foreach expression (whatever you specify within square brackets) for each column in your search result. If you only want it to be applied for specific columns, you need to provide either names of those columns, either full names (e.g. | foreach "bytes out" "bytes in" "other field" [...
) or wildcarded names to match all columns names following that pattern (e.g. | foreach "bytes*" [...
) . If you think the values of field can be of different data type, your expression should handle them accordingly. (e.g. check if its' number using isnum
function, before manipulation).
Thanks for the detailed explanation!
Hi guys ...
Thanks for the answers. So each value in your output will be replaced with 1/1024th of your value if you would convert to KB basically?
What happens if <> contains non numeric values?
Still confused ... 😞
This seems like it should be an edit, a comment or a new question rather than an answer.
you have to declare this
| foreach [eval totalinKB=0.001*<>]]
Please response if you are unclear
Technically, one KB is 1024 bytes, not 1000 bytes.
Hi, Kristof!
foreach
is used when you need to apply the same command (of several commands) to multiple columns (fields). For example, if you need to transform both bytes in
and bytes out
to kB, you could write smth like that:
| foreach bytes*
[ eval <<FIELD>>_kB = round('<<FIELD>>' / 1024) ]
In your case foreach
command is not so necessary. But you can use it in this way:
| foreach "bytes out"
[ eval <<FIELD>> = if(like(server, "www%"), round('<<FIELD>>' / 1024), '<<FIELD>>') ]