That's a fair question. So, first let me break the search to a less nested form, then describe the intentions/semantics. (As always, consult syntax and usage in Search Reference.) | makeresul...
See more...
That's a fair question. So, first let me break the search to a less nested form, then describe the intentions/semantics. (As always, consult syntax and usage in Search Reference.) | makeresults
| eval ip = split("119.0.6.159,62.0.3.75,63.0.3.84,75.0.3.80,92.0.4.159", ",")
``` data emulation above ```
| eval idx = mvrange(0,4)
| foreach ip mode=multivalue
[| eval interim1 = split(<<ITEM>>, "."),
interim2 = mvmap(idx, printf("%.3d", tonumber(mvindex(interim1, idx)))),
interim3 = mvjoin(interim2, "."),
sorted_ip_padded = mvappend(sorted_ip_padded, interim3)]
| eval sorted_ip_padded = mvsort(sorted_ip_padded)
| foreach sorted_ip_padded mode=multivalue
[| eval interim4 = split(<<ITEM>>, "."),
interim5 = mvmap(idx, printf("%d", tonumber(mvindex(interim4, idx)))),
interim6 = mvjoin(interim5, "."),
sorted_ip = mvappend(sorted_ip, interim6)] (I'm using a different approach to fill your homework from @tscroggins's proposal because foreach is easier to break down. I also use a more symmetric approach so it can be explained more readily.) Here is a key card behind the formula: split An IPv4 address will result will contain a 4-element array, indexed from 0 to 3. mvrange Generate a 4-element array with values from 0 to 3. mvsort This is lexicographic sort of array elements. For 0-padded IPv4 addresses, it is equivalent to numeric sort that you desired. printf Pad and unpad To examine the formula, first take a look at output below: idx interim1 interim2 interim3 interim4 interim5 interim6 ip sorted_ip sorted_ip_padded 0 1 2 3 92 0 4 159 092 000 004 159 092.000.004.159 119 000 006 159 119 0 6 159 119.0.6.159 119.0.6.159 62.0.3.75 63.0.3.84 75.0.3.80 92.0.4.159 62.0.3.75 63.0.3.84 75.0.3.80 92.0.4.159 119.0.6.159 062.000.003.075 063.000.003.084 075.000.003.080 092.000.004.159 119.000.006.159 Inside the foreach loop, the following steps are followed: Breakdown IPv4 into octets. (interim1, interim4) Pad/unpad each octet. (interim2, interim5) Reassemble IPv4 with/without padding. (interim3, interim6) Assemble padded/unpadded IPv4 into array. (sorted_ip_padded, sorted_ip) Now, the only nested element is padding/unpadding: mvmap(idx, printf("%.3d", tonumber(mvindex(interim1, idx))))/mvmap(idx, printf("%d", tonumber(mvindex(interim4, idx)))). Because of the way tonumber works in SPL, it cannot be broken down further. But the principle is not too complicated. idx is the index of octet. So, it is an iteration of printf("%d", octet) over each octet, where octet = mvindex(interim4, idx) wrapped in tonumber(). Hope this helps.