How to split a long list into pieces in Qore

I would like to split the list in Qore as follows:

list a = (1,2,3,4,5,6); list pieces = split_list_into_pieces(a, 2); printf("%y\n", pieces); 

Required Conclusion:

 [[1,2], [3,4], [5,6]] 

those. I want to take a (supposedly long) list and break it into pieces of a given (max.) Length.

I can do it like:

 list sub split_list_into_pieces(list a, int length) { int i = 0; list ret = (); list temp = (); foreach any x in (a) { temp += x; i++; if (i == length) { push ret, temp; temp = (); i = 0; } } if (temp) { push ret, temp; } return ret; } 

But it’s not very elegant, is it?

The best solution?

+5
source share
1 answer

you can do it like this:

 list sub list_chunk(list a, int length) { list result = (); while (a) push (result, extract (a, 0, length)); return result; } 
+7
source

Source: https://habr.com/ru/post/1264013/


All Articles