develooper Front page | perl.perl6.users | Postings from October 2021

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

Thread Previous | Thread Next
From:
Shlomi Fish
Date:
October 31, 2021 08:43
Subject:
Re: how do I turn a real into and array of Integers?
Message ID:
20211031104337.11731e46@shlomifish.org
Hi all!

On Sun, 31 Oct 2021 15:14:07 +0800
Alice <alice@coakmail.com> wrote:

> Hello
> 
> Try this one:
> 
> use strict;
> 
> use Data::Dumper;
> 
> 
> my $x = sqrt 2;
> 
> my @li= grep{/\d+/} split//,$x;
> 
> 
> print Dumper \@li;
> 

Or in raku:

> ("" ~ sqrt(2)).comb().grep(* ne ".").map(+*)
(1 4 1 4 2 1 3 5 6 2 3 7 3 0 9 5 1)

> 
> 
> $ perl t1.pl
> 
> $VAR1 = [
> 
>           '1',
> 
>           '4',
> 
>           '1',
> 
>           '4',
> 
>           '2',
> 
>           '1',
> 
>           '3',
> 
>           '5',
> 
>           '6',
> 
>           '2',
> 
>           '3',
> 
>           '7',
> 
>           '3',
> 
>           '1'
> 
>         ];
> 
> 
> Regards
> 
> Alice
> 
> On Sun, Oct 31, 2021 at 2:58 PM ToddAndMargo via perl6-users <
> perl6-users@perl.org> wrote:
> 
> > Hi All,
> >
> > Without going into why (it is a secret), how do I turn
> >  
> >  > my $x = sqrt 2  
> > 1.4142135623730951
> >
> > into an array of integers?
> > @y[0] = 1
> > @y[1] = 4
> > @y[2] = 1
> > @y[3] = 4
> > @y[4] = 2
> > @y[5] = 1
> >
> > etc.
> >
> >
> > Many thanks,
> > -T
> >
> >
> >  



-- 

Shlomi Fish       https://www.shlomifish.org/
https://www.shlomifish.org/open-source/projects/fortune-mod/

Summer Glau gave Richard III a horse free-of-charge and let him keep his
kingdom for himself.
    — https://www.shlomifish.org/humour/bits/facts/Summer-Glau/

Please reply to list if it's a mailing list post - https://shlom.in/reply .

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About