Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)

by LanX (Saint)
on Oct 20, 2021 at 14:25 UTC ( [id://11137799]=note: print w/replies, xml ) Need Help??


in reply to Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)

I was missing a split solution with lookbehind and lookahead here, so I wanted to add it.

(step by step demo with 'perl -de0')

DB<134> p $in ZBBBCZZ DB<135> x split /(?<=(.))(?!\1)/, $in 0 'Z' 1 'Z' 2 'BBB' 3 'B' 4 'C' 5 'C' 6 'ZZ' 7 'Z'

Alas I couldn't find a way to "forget" the group needed for the backreference \1 °

But this could be turned into something more flexible by applying "pair" functions from List::Util (which were probably not available back then)

DB<137> use List::Util qw/pairkeys pairvalues/ DB<138> x pairkeys split /(?<=(.))(?!\1)/, $in 0 'Z' 1 'BBB' 2 'C' 3 'ZZ' DB<139> x pairvalues split /(?<=(.))(?!\1)/, $in 0 'Z' 1 'B' 2 'C' 3 'Z' DB<140>

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

°) but I didn't search long, what I found is /n modifier, to turn each (...) into (?:...) , but then \1 isn't usable anymore

Update

Tho shorter tye's solution without split fits better here

DB<8> use List::Util qw/pairkeys/ DB<9> $_='ZBBBCZZ' DB<10> x pairkeys /((.)\2*)/g 0 'Z' 1 'BBB' 2 'C' 3 'ZZ' DB<11>

Replies are listed 'Best First'.
Re^2: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)
by tybalt89 (Monsignor) on Oct 20, 2021 at 16:21 UTC

    Here's a way to "forget" the group...

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11137799 use warnings; $_='ZBBBCZZ'; my @list = split ' ', s/(.)\K(?!\1)/ /gr; use Data::Dump 'dd'; dd "given $_ got", \@list;

    Outputs:

    ("given ZBBBCZZ got", ["Z", "BBB", "C", "ZZ"])

        And I see pKai is from Erlangen - an easy 216.6 km scenic drive from Darmstadt. :)

        Nice necropost BTW.

        Update: Maybe you should try to hire pKai to replace your new Python guy. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11137799]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-24 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found