From:

Date:

November 2, 2021 05:06Subject:

Re: how do I turn a real into and array of Integers?Message ID:

CAA99HCxXW5waJ1r1aE38_ibqe_-679wNiJ5OGi33ZoYQ1J2fWA@mail.gmail.comMaybe my favorite example of the differential applications of `comb` vs `split`. Below, sometimes you want to precisely tease-apart text, and `comb` works towards that end. Other times you want to destructively blast-away at text--eliminating the destractions--so you use `split`, until you see the desired result: ~$ raku -e ' "122333444455555666666".comb(/ \d**2..* % <same> /).say;' (22 333 4444 55555 666666) ~$ raku -e ' "122333444455555666666".split(/ \d**2..* % <same> /).say;' (1 ) ~$ raku -e ' "122333444455555666666".split(/ \d**2..* % <same> /, :skip-empty).say;' (1) https://stackoverflow.com/a/69489459/7270649 Best, Bill. On Mon, Nov 1, 2021 at 7:07 PM Patrick R. Michaud <pmichaud@pobox.com> wrote: > This is a place where .comb() is likely much better than .split() -- > .comb() > allows you to specify what you're wanting instead of what you're wanting to > avoid: > > $ raku -e "say sqrt(2).comb(/\d/).join(', ');" > 1, 4, 1, 4, 2, 1, 3, 5, 6, 2, 3, 7, 3, 0, 9, 5, 1 > > If you want only the first 10 digits, then: > > $ raku -e "say sqrt(2).comb(/\d/)[^10].join(', ');" > 1, 4, 1, 4, 2, 1, 3, 5, 6, 2 > > Pm > > > On Mon, Nov 01, 2021 at 06:55:40PM -0700, William Michels via perl6-users > wrote: > > You did great for not knowing Raku! > > > > ~$ raku -e "say sqrt(2).split(/\.|''/);" > > ( 1 4 1 4 2 1 3 5 6 2 3 7 3 0 9 5 1 ) > > ~$ raku -e "say sqrt(2).split(/\.|''/).raku;" > > ("", "1", "", "4", "1", "4", "2", "1", "3", "5", "6", "2", "3", "7", "3", > > "0", "9", "5", "1", "").Seq > > ~$ raku -e "say sqrt(2).split(/\.|''/, :skip-empty);" > > (1 4 1 4 2 1 3 5 6 2 3 7 3 0 9 5 1) > > ~$ raku -e "say sqrt(2).split(/\.|''/, :skip-empty).join(', ');" > > 1, 4, 1, 4, 2, 1, 3, 5, 6, 2, 3, 7, 3, 0, 9, 5, 1 > > > > I moved the sqrt(2) call to the head of the method chain, then visualized > > elements using the `.raku` method (`.perl` works also, but don't tell > > anyone). You can see an empty element at the beginning/end, as well as > > where the decimal point used to reside. Including :skip-empty in your > > `.split` call sets it to True, removing empty elements. > > > > For Mac/Linux people (swapped single/double quotes): > > > > ~$ raku -e 'say sqrt(2).split(/\.|""/);' > > ( 1 4 1 4 2 1 3 5 6 2 3 7 3 0 9 5 1 ) > > ~$ raku -e 'say sqrt(2).split(/\.|""/).raku;' > > ("", "1", "", "4", "1", "4", "2", "1", "3", "5", "6", "2", "3", "7", "3", > > "0", "9", "5", "1", "").Seq > > ~$ raku -e 'say sqrt(2).split(/\.|""/, :skip-empty);' > > (1 4 1 4 2 1 3 5 6 2 3 7 3 0 9 5 1) > > ~$ raku -e 'say sqrt(2).split(/\.|""/, :skip-empty).join(", ");' > > 1, 4, 1, 4, 2, 1, 3, 5, 6, 2, 3, 7, 3, 0, 9, 5, 1 > > > > HTH, Bill. > > > > On Sun, Oct 31, 2021 at 5:51 AM sisyphus <sisyphus359@gmail.com> wrote: > > > > > > > > > > > > On Sun, Oct 31, 2021 at 10:10 PM ToddAndMargo via perl6-users < > > perl6-users@perl.org> wrote: > > >> > > >> On 10/31/21 01:43, Shlomi Fish wrote: > > >> > > >> > > > >> >> ("" ~ sqrt(2)).comb().grep(* ne ".").map(+*) > > >> > (1 4 1 4 2 1 3 5 6 2 3 7 3 0 9 5 1) > > >> > > >> Cool! > > >> > > >> my Int @x = ("" ~ sqrt(2)).comb().grep(* ne ".").map(+*) > > >> [1 4 1 4 2 1 3 5 6 2 3 7 3 0 9 5 1] > > >> > > >> Is there a way to set how many digits I get? > > > > > > > > > In perl we can do: > > > C:\>perl -e "@x = split(/\.|/, sqrt(2)); print for @x"; > > > 14142135623731 > > > C:\>perl -e "@x = split(/\.|/, sprintf('%.7g', sqrt(2))); print for > @x"; > > > 1414214 > > > C:\>perl -e "@x = split(/\.|/, sprintf('%.8g', sqrt(2))); print for > @x"; > > > 14142136 > > > > > > But the same split() expression is unacceptable to Raku. > > > Closest I could get with that approach: > > > > > > C:\>raku -e "say split(/\.|''/, sqrt 2);" > > > ( 1 4 1 4 2 1 3 5 6 2 3 7 3 0 9 5 1 ) > > > > > > C:\_32>raku -e "say split(/\.|''/, sprintf('%.5g', sqrt 2));" > > > ( 1 4 1 4 2 ) > > > > > > It's that additional space between the first 2 digits that has me beat. > > > (I don't know raku at all.) > > > > > > Cheers, > > > Rob > > > > > > >Thread Previous | Thread Next

- how do I turn a real into and array of Integers? by ToddAndMargo via perl6-users
- Re: how do I turn a real into and array of Integers? by Andinus via perl6-users
- Re: how do I turn a real into and array of Integers? by Sean McAfee
- Re: how do I turn a real into and array of Integers? by ToddAndMargo via perl6-users
- Re: how do I turn a real into and array of Integers? by ToddAndMargo via perl6-users
- Re: how do I turn a real into and array of Integers? by Kevin Pye
- Re: how do I turn a real into and array of Integers? by ToddAndMargo via perl6-users
- Re: how do I turn a real into and array of Integers? by Sean McAfee
- Re: how do I turn a real into and array of Integers? by ToddAndMargo via perl6-users
- Re: how do I turn a real into and array of Integers? by sisyphus
- Re: how do I turn a real into and array of Integers? by Sean McAfee
- Re: how do I turn a real into and array of Integers? by Alice
- Re: how do I turn a real into and array of Integers? by Shlomi Fish
- Re: how do I turn a real into and array of Integers? by ToddAndMargo via perl6-users
- Re: how do I turn a real into and array of Integers? by Bruce Gray
- Re: how do I turn a real into and array of Integers? by sisyphus
- Re: how do I turn a real into and array of Integers? by William Michels via perl6-users
- Re: how do I turn a real into and array of Integers? by Patrick R. Michaud
**Re: how do I turn a real into and array of Integers?**by William Michels via perl6-users- Re: how do I turn a real into and array of Integers? by ToddAndMargo via perl6-users

nntp.perl.org: Perl Programming lists via nntp and http.

Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About