Re: I need sorting help
On 3/2/24 05:13, Elizabeth Mattijsen wrote: $ raku -e '.say for .sort(*.split(/\d+/, :kv).map({ (try .Numeric) // $_}).List) afoo2 afoo12 On 2 Mar 2024, at 07:26, ToddAndMargo via perl6-users wrote: Hi All, @Sorted_List = @Sorted_List.sort: { .comb(/ \d+ | \D+ /) .map({ .Int // .self })}; gives me Element [0] Element [1] Element [2] Element [3] Element [4] Element [5] Element [6] Element [7] Element [8] Element [9] I need it to say Element [0] Element [1] Element [2] Element [3] Element [4] Element [5] Element [6] Element [7] Element [8] Element [9] What did I goof up, this time? Many thanks, -T Hi Liz, Look what you have done to me! $ raku -e '.say for bk-3.41.0.0>.sort(*.split(/\d+/, :kv).map({ (try .Numeric) // $_}).List)' bk-1.0.4.5 bk-1.1.0.9 bk-2.1.4.3 bk-3.4.1.5 bk-3.41.0.0 Awesome! -T -- ~~ Computers are like air conditioners. They malfunction when you open windows ~~
Re: I need sorting help
On Sat, Mar 9, 2024 at 7:52 PM yary wrote: > how to sort by alpha & numeric segments (b3b, a1a, c20c) => (a1a, b3b, c2c) Ahhh. D'oh. Thanks! Now I see what was required, or at least think I do: .sort: {m:g/ \d+{make +$/} | \D+{make ~$/} /».made} That does what was meant, right? -- love, raiph
Re: I need sorting help
Using my quick-intuition, are these methods sorting on the earliest number withing the string, ignoring the non-digits? $ raku -e '.say for .sort: {m/ \d+ /.Int}' $ raku -e '.say for .sort: +*.match: / \d+ /' let's see $ raku -e '.say for .sort: {m/ \d+ /.Int}' it1 does2 not3 matter10 what20 the30letters40 say31 What's been interesting about this thread is not only the variety of solutions, also that the solutions are for two different questions- how to sort by alpha & numeric segments (b3b, a1a, c20c) => (a1a, b3b, c2c) how to sort by a numeric segment only (b3b, a1a, c20c) => (a1a, b3b, c20c) -y On Thu, Mar 7, 2024 at 4:40 PM Ralph Mellor wrote: > On Tue, Mar 5, 2024 at 7:01 AM ToddAndMargo via perl6-users > wrote: > > > >> $ raku -e '.say for .sort(*.split(/\d+/, > :kv).map({ (try .Numeric) // $_}).List)' > > >> > > >> Yippee! > > > > raku -e '.say for .sort: { .comb(/ \d+ | \D+ > /).map({ .Int // .self }).cache };' > > > > Awesome! Now I have two different methods! > > And now 4: > > $ raku -e '.say for .sort: {m/ \d+ /.Int}' > bk1 > bk2 > bk10 > bk34 > > or, same logic, but spelled differently: > > $ raku -e '.say for .sort: +*.match: / \d+ /' > bk1 > bk2 > bk10 > bk34 > > -- > love, raiph >
Re: I need sorting help
On Tue, Mar 5, 2024 at 7:01 AM ToddAndMargo via perl6-users wrote: > >> $ raku -e '.say for .sort(*.split(/\d+/, :kv).map({ > >> (try .Numeric) // $_}).List)' > >> > >> Yippee! > > raku -e '.say for .sort: { .comb(/ \d+ | \D+ /).map({ > > .Int // .self }).cache };' > > Awesome! Now I have two different methods! And now 4: $ raku -e '.say for .sort: {m/ \d+ /.Int}' bk1 bk2 bk10 bk34 or, same logic, but spelled differently: $ raku -e '.say for .sort: +*.match: / \d+ /' bk1 bk2 bk10 bk34 -- love, raiph
Re: I need sorting help
On 3/4/24 23:01, ToddAndMargo via perl6-users wrote: No I have Sould have been Now I have
Re: I need sorting help
On 3/4/24 22:09, Bruce Gray wrote: On Mar 4, 2024, at 15:55, ToddAndMargo via perl6-users wrote: --snip-- $ raku -e '.say for .sort(*.split(/\d+/, :kv).map({ (try .Numeric) // $_}).List)' bk1 bk2 bk10 bk34 Yippee! tony@rn6:/home/linuxutil1$ raku -e '.say for .sort: { .comb(/ \d+ | \D+ /) .map({ .Int // .self })};' bk1 bk10 bk2 bk34 Rats! Thank for trying anyway! -- love, todd Wow! Very subtle trap with Seq! The short answer is: Just add `.cache`: raku -e '.say for .sort: { .comb(/ \d+ | \D+ /).map({ .Int // .self }).cache };' bk1 bk2 bk10 bk34 When the elements that `.sort` is sorting are of type List or Array, the sorting happens one slot at a time; the sort happens on element [0] of each List, and ties are settled by [1] (or [2] if [1] are also a tie, etc). This enables `.sort` to behave very intuitively (after the shock wears off). I did not detect the problem by eye, but when I broke it down on the commandline I see that Seq objects are being created by `.comb` and `.map`. Objects of Seq type can only be read *once*, so the multiple reads that `.sort` needs would not play well with Seq. It does not matter whether you convert the numbers to a Numeric type (which you did correctly in the `.map`), they are still part of a Seq. The .cache method turns the Seq into a List, and the sort behaves as you intended. https://docs.raku.org/routine/cache Awesome! No I have two different methods! -T
Re: I need sorting help
> On Mar 4, 2024, at 15:55, ToddAndMargo via perl6-users > wrote: --snip-- > $ raku -e '.say for .sort(*.split(/\d+/, :kv).map({ (try > .Numeric) // $_}).List)' > bk1 > bk2 > bk10 > bk34 > > Yippee! > > > tony@rn6:/home/linuxutil1$ raku -e '.say for .sort: { > .comb(/ \d+ | \D+ /) .map({ .Int // .self })};' > bk1 > bk10 > bk2 > bk34 > > Rats! Thank for trying anyway! > > -- > love, todd Wow! Very subtle trap with Seq! The short answer is: Just add `.cache`: raku -e '.say for .sort: { .comb(/ \d+ | \D+ /).map({ .Int // .self }).cache };' bk1 bk2 bk10 bk34 When the elements that `.sort` is sorting are of type List or Array, the sorting happens one slot at a time; the sort happens on element [0] of each List, and ties are settled by [1] (or [2] if [1] are also a tie, etc). This enables `.sort` to behave very intuitively (after the shock wears off). I did not detect the problem by eye, but when I broke it down on the commandline I see that Seq objects are being created by `.comb` and `.map`. Objects of Seq type can only be read *once*, so the multiple reads that `.sort` needs would not play well with Seq. It does not matter whether you convert the numbers to a Numeric type (which you did correctly in the `.map`), they are still part of a Seq. The .cache method turns the Seq into a List, and the sort behaves as you intended. https://docs.raku.org/routine/cache -- Hope this helps, Bruce Gray (Util of PerlMonks)
Re: I need sorting help
On 3/4/24 13:55, ToddAndMargo via perl6-users wrote: On 3/4/24 12:40, Ralph Mellor wrote: On Sat, Mar 2, 2024 at 6:26 AM ToddAndMargo via perl6-users wrote: @Sorted_List = @Sorted_List.sort: { .comb(/ \d+ | \D+ /) .map({ .Int // .self })}; In case another answer is helpful... First, a simplified sort that produces the same result as your code above: @Sorted_List .= sort: *.match: / \d+ /; I can explain that in a later comment if you want, but it's all stuff I recall you figuring out in the past, so for now I'll just move on to address the change you wanted. The problem you had was that sort defaults to a string sort. In string sorting 1, 10, 2 are already sorted, but you instead want numeric order, which sorts those three into 1, 2, 10. To do that, coerce the sort value to numeric by inserting a `+`: @Sorted_List .= sort: +*.match: / \d+ /; -- love, raiph $ raku -e '.say for .sort(*.split(/\d+/, :kv).map({ (try .Numeric) // $_}).List)' bk1 bk2 bk10 bk34 Yippee! tony@rn6:/home/linuxutil1$ raku -e '.say for .sort: { .comb(/ \d+ | \D+ /) .map({ .Int // .self })};' bk1 bk10 bk2 bk34 Rats! Thank for trying anyway! $ raku -e '.say for .sort: { .comb(/ \d+ | \D+ /) .map({ .Str // .self })};' bk1 bk10 bk2 bk34 Double Rats!
Re: I need sorting help
On 3/4/24 12:40, Ralph Mellor wrote: On Sat, Mar 2, 2024 at 6:26 AM ToddAndMargo via perl6-users wrote: @Sorted_List = @Sorted_List.sort: { .comb(/ \d+ | \D+ /) .map({ .Int // .self })}; In case another answer is helpful... First, a simplified sort that produces the same result as your code above: @Sorted_List .= sort: *.match: / \d+ /; I can explain that in a later comment if you want, but it's all stuff I recall you figuring out in the past, so for now I'll just move on to address the change you wanted. The problem you had was that sort defaults to a string sort. In string sorting 1, 10, 2 are already sorted, but you instead want numeric order, which sorts those three into 1, 2, 10. To do that, coerce the sort value to numeric by inserting a `+`: @Sorted_List .= sort: +*.match: / \d+ /; -- love, raiph $ raku -e '.say for .sort(*.split(/\d+/, :kv).map({ (try .Numeric) // $_}).List)' bk1 bk2 bk10 bk34 Yippee! tony@rn6:/home/linuxutil1$ raku -e '.say for .sort: { .comb(/ \d+ | \D+ /) .map({ .Int // .self })};' bk1 bk10 bk2 bk34 Rats! Thank for trying anyway! -- love, todd
Re: I need sorting help
> @Sorted_List .= sort: *.match: / \d+ /; > ... > @Sorted_List .= sort: +*.match: / \d+ /; Or perhaps easier to understand: @Sorted_List .= sort: {m/ \d+ /.Str} vs @Sorted_List .= sort: {m/ \d+ /.Int} love, raiph
Re: I need sorting help
On Sat, Mar 2, 2024 at 6:26 AM ToddAndMargo via perl6-users wrote: > > @Sorted_List = @Sorted_List.sort: { .comb(/ \d+ | \D+ /) .map({ .Int // .self > })}; In case another answer is helpful... First, a simplified sort that produces the same result as your code above: @Sorted_List .= sort: *.match: / \d+ /; I can explain that in a later comment if you want, but it's all stuff I recall you figuring out in the past, so for now I'll just move on to address the change you wanted. The problem you had was that sort defaults to a string sort. In string sorting 1, 10, 2 are already sorted, but you instead want numeric order, which sorts those three into 1, 2, 10. To do that, coerce the sort value to numeric by inserting a `+`: @Sorted_List .= sort: +*.match: / \d+ /; -- love, raiph
Re: I need sorting help
Easy multisort! my @h = ( { name => "albert", age => 40, size => 2 }, { }, { ); # ({age => 40, name => albert, size => 2} {age => 69, name => albert, size => 3} {age => 22, name => andy, size => 3}) On Sat, Mar 2, 2024 at 9:29 PM ToddAndMargo via perl6-users < perl6-users@perl.org> wrote: > On 3/2/24 05:13, Elizabeth Mattijsen wrote: > > .sort(*.split(/\d+/, :kv).map({ (try .Numeric) // > $_}).List) > > Hi Elizabeth, > > It works perfectly. Thank you! > > I have no idea why, I will ask you in another post > > -T >
Re: I need sorting help
On 3/2/24 05:13, Elizabeth Mattijsen wrote: .sort(*.split(/\d+/, :kv).map({ (try .Numeric) // $_}).List) Hi Elizabeth, It works perfectly. Thank you! I have no idea why, I will ask you in another post -T
RE: I need sorting help
Elizabeth, I have been looking for that one to sort operating system devices. Exceptional timing! Thanks, Mark Devine (202) 878-1500 -Original Message- From: Elizabeth Mattijsen Sent: Saturday, March 2, 2024 8:14 AM To: ToddAndMargo via perl6-users Subject: Re: I need sorting help $ raku -e '.say for .sort(*.split(/\d+/, :kv).map({ (try .Numeric) // $_}).List) afoo2 afoo12 > On 2 Mar 2024, at 07:26, ToddAndMargo via perl6-users > wrote: > > Hi All, > > @Sorted_List = @Sorted_List.sort: { .comb(/ \d+ | \D+ /) .map({ .Int // .self > })}; > > gives me > > Element [0] > Element [1] > Element [2] > Element [3] > Element [4] > Element [5] > Element [6] > Element [7] > Element [8] > Element [9] > > I need it to say > > Element [0] > Element [1] > Element [2] > Element [3] > Element [4] > Element [5] > Element [6] > Element [7] > Element [8] > Element [9] > > What did I goof up, this time? > > Many thanks, > -T
Re: I need sorting help
$ raku -e '.say for .sort(*.split(/\d+/, :kv).map({ (try .Numeric) // $_}).List) afoo2 afoo12 > On 2 Mar 2024, at 07:26, ToddAndMargo via perl6-users > wrote: > > Hi All, > > @Sorted_List = @Sorted_List.sort: { .comb(/ \d+ | \D+ /) .map({ .Int // .self > })}; > > gives me > > Element [0] > Element [1] > Element [2] > Element [3] > Element [4] > Element [5] > Element [6] > Element [7] > Element [8] > Element [9] > > I need it to say > > Element [0] > Element [1] > Element [2] > Element [3] > Element [4] > Element [5] > Element [6] > Element [7] > Element [8] > Element [9] > > What did I goof up, this time? > > Many thanks, > -T
I need sorting help
Hi All, @Sorted_List = @Sorted_List.sort: { .comb(/ \d+ | \D+ /) .map({ .Int // .self })}; gives me Element [0] Element [1] Element [2] Element [3] Element [4] Element [5] Element [6] Element [7] Element [8] Element [9] I need it to say Element [0] Element [1] Element [2] Element [3] Element [4] Element [5] Element [6] Element [7] Element [8] Element [9] What did I goof up, this time? Many thanks, -T