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

Re: pairs of separators from a string

Thread Previous | Thread Next
From:
William Michels via perl6-users
Date:
August 22, 2021 19:18
Subject:
Re: pairs of separators from a string
Message ID:
CAA99HCxwna60oEAtKa8yyR3g_H=gxnLe0NtWA1JKf9co7suTLA@mail.gmail.com
Hi Marc!

Is it just even/odd elements that you want to separate out? If so, maybe
.grep() is your friend here:

> my $string0 = q!""''(){}[]!
""''(){}[]
> $string0.comb[(0..*-1).grep(* %% 2)].say;
(" ' ( { [)
> $string0.comb[(0..*-1).grep(* % 2)].say;
(" ' ) } ])

It sounds like you have some interest in 'pairing' the results, in which
case this works (using .pairs()):

> .say for [Z] $string0.comb[(0..*-1).grep(* %% 2)],
$string0.comb[(0..*-1).grep(* % 2)];
(" ")
(' ')
(( ))
({ })
([ ])
> .pairs.say for [Z] $string0.comb[(0..*-1).grep(* %% 2)],
$string0.comb[(0..*-1).grep(* % 2)];
(0 => " 1 => ")
(0 => ' 1 => ')
(0 => ( 1 => ))
(0 => { 1 => })
(0 => [ 1 => ])
>
> $*VM
moar (2021.06)


HTH, Bill.



On Sun, Aug 22, 2021 at 6:24 AM Marc Chantreux <eiro@phear.org> wrote:

> hello William,
>
> > your string, or whether-or-not some might be nested within each other.
> You
> > show a lone ampersand ("&") at the beginning of your example, but other
> > strings may not be so simple.
>
> really sorry about this artefact from previous attempts :(
>
> > > $string.comb(/ ( <:Ps> ~ <:Pe> .?) /, :global).raku.say
> > ("()", "\{}", "[]").Seq
>
> those regexes i can't read for the moment is a good excuse to learn more
> about raku. i'll read them later and come back here if i have questions.
>
> thanks and regards.
> marc
>

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