2

What's a neat one-liner to fill an array (of given possibly dynamic size) with its own indices?

I was trying this:

data: copy {}
repeat tilenum totaltiles [ append data rejoin [tilenum " "] ]

...and then looking to convert it into an array, but there must be a more Rebolish way of doing such a thing, I think.

My end goal is actually to then randomize the order of the array's contents, which I think would be the output of random myarrayhere, but perhaps there's even a way to do all of this in one fell swoop?

Kev
  • 15,899
  • 15
  • 79
  • 112

2 Answers2

3

If you want random array (block!) why not start with block instead of string in the first place?

>> random array/initial length: 10 does [-- length]
== [3 10 7 9 2 5 8 6 1 4]
rebolek
  • 1,281
  • 7
  • 16
  • Starting with a block is exactly what I was looking for, except I didn't realize you could use `does` as an argument to `array/initial`, which makes total sense now that I see it. – Kev Feb 16 '15 at 11:29
2

Here is another option to show you a few more Rebol tricks. collect and keep are very cool in this setting.

random collect [ repeat tilenum 10 [ keep tilenum ] ]

I updated my first answer to use repeat as suggested by @rebolek as I agree that the for syntax is less than perfect.

If you want really random numbers and don't mind the risk of a repeated number there is more concise option using the loop construct

collect [ loop 10 [ keep random 100 ] ]
johnk
  • 1,102
  • 7
  • 12
  • 1
    COLLECT/KEEP is nice trick and you can use `repeat tilenum 10 [...]` - I really don't like FOR :) – rebolek Feb 16 '15 at 10:59
  • Super, I like this syntax, but the accept goes to @rebolek solely for being first. – Kev Feb 16 '15 at 11:30