-1

I'm having trouble with this:

Split: lista

li := Array new: 30.
aux := Array new: 30.
j := 0.
1 to: (lista size / 30) ceiling do: [ :i | 
    1 to: 30 do: [ :k | aux at: k put: (lista at: k + j) ].
    j := j + 30.
    li at: i put: aux ].
^ li

This is supossed to return the same list, but splitted in 30 groups of 30. I don't know what is failing, I've tried debugging it a lot of times, but it just doesn't work. At first I tried to split it at an index, but it's too hard.

Please, help me! :(

Diana
  • 140
  • 1
  • 13
  • 1
    What exactly is "failing"? Please provide the error message and the first couple of method selectors from the top of the error stack. – Max Leske Jul 07 '17 at 06:31
  • Why 30 groups of 30? This implicit knowledge of `lista size` is fragile. The protection `(lista size / 30) ceiling` is not going to protect from all possible edge case, notably the last chunk might be of size < 30, and there could also be more than 30 chunks... – aka.nice Jul 07 '17 at 12:18
  • 1
    There is a fundamental flaw: the Array `aux` containing a chunk is the same object for each chunk, overwritten at each external loop, so you'll end up with a list of 30 identical chunks. – aka.nice Jul 07 '17 at 12:20
  • It would be great if you could add an example of the input data and the desired output data (on a smaller array) e.g. for input array `#(1 2 3 4)` and index `2` I expect output `#(#(1 2) #(1 2) #(1 2) #(1 2))` or something. Right now it is unclear what you are asking. – Peter Uhnak Jul 11 '17 at 09:31

1 Answers1

0

Following the comments, I think you want something like

split: lista by: anInteger
| groups j |

groups := OrderedCollection new.
j := 0.
lista withIndexDo: [:element :i | | group |
    i // anInteger = 0 
       ifTrue: [ group := Array new: anInteger. 
                 j := 1 ].
    group at: j put: element.
    j := j + 1.
    (j > anInteger or: [ i = lista size ]) 
       ifTrue: [groups add: group]. ].
^ groups
Carlos E. Ferro
  • 930
  • 10
  • 21