Thread overview | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 08, 2018 Working with ranges | ||||
---|---|---|---|---|
| ||||
Hi guys, I have created an array of strings with "string[12] ps = ["cat", "dog", "lion", "wolf", "coin", "chest", "money", "gold", "A", "B", "C", "D"];". I want to use the array as a range and I want to randomize it, like I want to transform that into several other ranges with the same elements but in different orders, how do I do that? I tried using the function choice() from std.random but it gives an error message for some reason. |
December 08, 2018 Re: Working with ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Murilo | On Saturday, 8 December 2018 at 03:37:56 UTC, Murilo wrote:
> Hi guys, I have created an array of strings with "string[12] ps
string[12] isn't a range, but string[] is.
Try passing `ps[]` to the function instead of plain `ps` and see what happens.
|
December 08, 2018 Re: Working with ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Saturday, 8 December 2018 at 03:46:11 UTC, Adam D. Ruppe wrote:
> On Saturday, 8 December 2018 at 03:37:56 UTC, Murilo wrote:
>> Hi guys, I have created an array of strings with "string[12] ps
>
> string[12] isn't a range, but string[] is.
>
> Try passing `ps[]` to the function instead of plain `ps` and see what happens.
How do I transform an array into a range?
|
December 08, 2018 Re: Working with ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Murilo | On Saturday, 8 December 2018 at 03:48:10 UTC, Murilo wrote:
>> Try passing `ps[]` to the function instead of plain `ps` and see what happens.
>
> How do I transform an array into a range?
With the slicing operator, [].
|
December 08, 2018 Re: Working with ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Saturday, 8 December 2018 at 03:51:02 UTC, Adam D. Ruppe wrote:
> On Saturday, 8 December 2018 at 03:48:10 UTC, Murilo wrote:
>>> Try passing `ps[]` to the function instead of plain `ps` and see what happens.
>>
>> How do I transform an array into a range?
>
> With the slicing operator, [].
Thank you very much, it worked now.
What is the difference between declaring "int[3] a = [1,2,3];" and declaring "int[] a = [1,2,3];"? Is the first an array and the second a range?
I always thought that leaving the square brackets empty would create an array of flexible size, it never occurred to me that it was creating something else.
|
December 07, 2018 Re: Working with ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Friday, December 7, 2018 8:46:11 PM MST Adam D. Ruppe via Digitalmars-d- learn wrote:
> On Saturday, 8 December 2018 at 03:37:56 UTC, Murilo wrote:
> > Hi guys, I have created an array of strings with "string[12] ps
>
> string[12] isn't a range, but string[] is.
>
> Try passing `ps[]` to the function instead of plain `ps` and see what happens.
Specifically, the problem is that static arrays have a fixed length, which means that you can't pop elements off as is required for ranges. Dynamic arrays on the other hand are ranges (at least as long as you import std.range.primitives to get the range functions for dynamic arrays). Slicing a static array gives you a dynamic array which is a slice of the static array. So, mutating the elements of the dynamic array will mutate the elements of the static array, but the dynamic array can have elements popped off as is required for ranges, whereas the static array can't.
- Jonathan M Davis
|
December 08, 2018 Re: Working with ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Murilo | On Saturday, 8 December 2018 at 04:11:03 UTC, Murilo wrote: > What is the difference between declaring "int[3] a = [1,2,3];" and declaring "int[] a = [1,2,3];"? Is the first an array and the second a range? They are both arrays, just the former one has a fixed size and the latter does not. Ranges require a way to iterate and consume elements, meaning they cannot be fixed size. > I always thought that leaving the square brackets empty would create an array of flexible size, it never occurred to me that it was creating something else. That's what it is, just a flexible array also happens to be an array, whereas a fixed-size array is not one. But a slice of a fixed size one yields a flexible one.. which is why the ps[] thing works to create a range out of it. |
December 08, 2018 Re: Working with ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Saturday, 8 December 2018 at 04:16:25 UTC, Adam D. Ruppe wrote:
> On Saturday, 8 December 2018 at 04:11:03 UTC, Murilo wrote:
>> What is the difference between declaring "int[3] a = [1,2,3];" and declaring "int[] a = [1,2,3];"? Is the first an array and the second a range?
>
> They are both arrays, just the former one has a fixed size and the latter does not. Ranges require a way to iterate and consume elements, meaning they cannot be fixed size.
>
>> I always thought that leaving the square brackets empty would create an array of flexible size, it never occurred to me that it was creating something else.
>
> That's what it is, just a flexible array also happens to be an array, whereas a fixed-size array is not one.
>
> But a slice of a fixed size one yields a flexible one.. which is why the ps[] thing works to create a range out of it.
Thank you guys so much for the explanation, it is all making more sense now.
|
December 09, 2018 Re: Working with ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On 12/7/18 11:16 PM, Adam D. Ruppe wrote:
> On Saturday, 8 December 2018 at 04:11:03 UTC, Murilo wrote:
>> What is the difference between declaring "int[3] a = [1,2,3];" and declaring "int[] a = [1,2,3];"? Is the first an array and the second a range?
>
> They are both arrays, just the former one has a fixed size and the latter does not. Ranges require a way to iterate and consume elements, meaning they cannot be fixed size.
>
>> I always thought that leaving the square brackets empty would create an array of flexible size, it never occurred to me that it was creating something else.
>
> That's what it is, just a flexible array also happens to be an array, whereas a fixed-size array is not one.
I think, you mean "a flexible array also happens to be *a range*..."
-Steve
|
December 10, 2018 Re: Working with ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Murilo | Hi guys, thank you for helping me out here, there is this facebook group for the D language, here we can help and teach each other. It is called Programming in D. Please join. https://www.facebook.com/groups/662119670846705/?ref=bookmarks |
Copyright © 1999-2021 by the D Language Foundation