Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Can split split against more than one character in a string of the same character?

by Don Coyote (Hermit)
on Jul 15, 2019 at 20:29 UTC ( [id://11102892]=perlquestion: print w/replies, xml ) Need Help??

Don Coyote has asked for the wisdom of the Perl Monks concerning the following question:

Using Padre's Regex Editor today, very nice tool. I thought I might be able to split a pattern on a string of the same character, but that didn't seem to want to play. I could use something like unpack, but wondered if there were any tricks to doing this using split?

my @splitted = split /(?:(?=a{2})(?<=a{2}))/, 'aaaaaaaaa'; print join " ", @splitted; output recieved :aa a a a a a aa output desired :aa aa aa aa a

The main issue is that at each position there is a match, so the string is split at every position. I tried a mix of assertions and literals but the best I got was to match two at the start and end with single entities in between

I have a solution with s/// that I am happy about. I'm getting a count and any remainders modulo the length of the test string remain in the string. If I need to I can write a while loop rather than a map, though this is probably already better for what I want.

my $prim_to_test = ${ num_to_nat(2) }[0]; #get_wildmarks(); 'aa' my $split_pattern = qr/(\Q$prim_to_test\E)/; print "split_pat: $split_pattern\n"; my @splitted = map "$_ x $1", ( $wildmarks =~ s/(?=$split_pattern)$split_pattern//g ); print "splitted: ", join " \N{U+68} ", @splitted, "r: $wildmarks"; output:splitted: 4 x aa h r: a

But can split do it?

SPoiler ALert: Yes.

Further, it would appear the ability to use arbitrary characters is also expanded

Replies are listed 'Best First'.
Re: Can split split against more than one character in a string of the same character?
by holli (Abbot) on Jul 15, 2019 at 20:52 UTC
    split returns the separators when there are captures in the separator regex. So you can simply do
    D:\ENV>perl -e "$x = qq#aaaaaaa#; print join '#', split /(..)/, $x" #aa##aa##aa#a
    and grep away the empty elements.


    holli

    You can lead your users to water, but alas, you cannot drown them.

      Ah, I did think about placing dots in the backward assertion for a brief moment, but even that is ott. I can just split into an array and join that on print. Thanks

Re: Can split split against more than one character in a string of the same character?
by hippo (Bishop) on Jul 15, 2019 at 21:17 UTC

    No need to mess with split when you just want to divide up a string of the same chars:

    use strict; use warnings; use Test::More tests => 1; my $in = 'aaaaaaaaa'; my $want = 'aa aa aa aa a'; my $have = join ' ', ($in =~ /a{1,2}/g); is $have, $want;

      I had an inkling that split could do this, but was approaching it from too complicated a place. Starting to get the feeling I should have perldoc -f split now.

      This is a clean and efficient way of doing this with match though, thanks

      I like the simplicity of the test structure, allowing for documenting attempts. That's really great.

Re: Can split split against more than one character in a string of the same character?
by johngg (Canon) on Jul 15, 2019 at 22:12 UTC

    Another approach would be to use unpack.

    johngg@shiraz:~/perl/Monks$ perl -Mstrict -Mwarnings -E ' my $str = q{aaaaaaaaa}; my @parts = unpack q{(a2)*}, $str; say qq{@parts};' aa aa aa aa a

    I hope this is helpful.

    Cheers,

    JohnGG

      That looks fast!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11102892]
Approved by holli
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-16 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found