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

Splitting a String into n-character parts, without using a regex

by leons (Pilgrim)
on Jan 26, 2001 at 03:15 UTC ( #54413=perlquestion: print w/ replies, xml ) Need Help??
leons has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

How can I easily split a certain string ... Let's say:
my $string='aabbccddee';

Into n-character parts, without using a regex. The only thing
I came up with was (assuming I'd want 2-character parts):
push @row,$& while('aabbccddee'=~/\w{2}/g);
Which would result in @row being:
@row=('aa','bb','cc','dd','ee');
Is there a better way ... i.e. by using split or map or whatever ?
Because I really don't want to use a regex or a while-loop

Thanks a Lot,

Bye, Leon

Comment on Splitting a String into n-character parts, without using a regex
Select or Download Code
Re: Splitting a String into n-character parts, without using a regex
by Anonymous Monk on Jan 26, 2001 at 03:19 UTC
    @row = grep(/./, split(/(.{$num})/, $string));
    Where $num is the number of characters you want each element to contain (obviously, the last element may contain less characters than the others).
Re: Splitting a String into n-character parts, without using a regex
by chipmunk (Parson) on Jan 26, 2001 at 03:22 UTC
    You say without using a regex, but do you know that you can use //g in a list context to return all the matches at once? @row = 'aabbccddee' =~ /\w{2}/g;
    I use this approach a lot, because it's simple but powerful.
      That approach works in this case; but for general strings, it would miss the last part of the string if it's too short (ie. less than 2 \w chars).
      @row = 'aabbccddee' =~ /\w{1,2}/g;
      will capture the last section too
Re: Splitting a String into n-character parts, without using a regex
by runrig (Abbot) on Jan 26, 2001 at 03:26 UTC
    JAWTDI:
    #!/usr/local/bin/perl -l -w use strict; my $str = "aabbcc"; my @arr = unpack("A2" x (length($str)/2), $str); # Or marginally better #my @arr = unpack("A2" x (length($str) >> 1), $str); print for @arr;
    Neither of these is significantly better than using a regex, though.
Re: Splitting a String into n-character parts, without using a regex
by Hot Pastrami (Monk) on Jan 26, 2001 at 03:27 UTC
    Could your string contain non-word characters or whitespace that you want paired out as well? If so, this should work:

    @row = $string =~ /../g;

    Update: If you also need to match newlines (\n) you could use the "s" modifier:

    @row = $string =~ /../sg;

    Hot Pastrami
Re: Splitting a String into n-character parts, without using a regex
by Fastolfe (Vicar) on Jan 26, 2001 at 03:28 UTC
    I might approach it like this:
    push(@row, substr($string, 0, $len, '')) while length $string;
    Of course, that still uses a while, but I suspect it'll still be faster than a regex.
Re: Splitting a String into n-character parts, without using a regex
by clintp (Curate) on Jan 26, 2001 at 03:35 UTC
    $_='aAbBcCdDeE'; $_=reverse; push @r, chop().chop while length;
Re: Splitting a String into n-character parts, without using a regex
by MeowChow (Vicar) on Jan 26, 2001 at 03:44 UTC
    I'm confused, are you looking to split the string into arbitrary-length sets of contiguous repeated characters, or just into n-length strings, regardless of whether or not the characters in them repeat? Your choice of strings, 'aabbcccddee', makes this somewhat ambiguous. If you want the former, HAWTDI:
    $string='aabbbbbbcccccaaaddeefghhhh'; my @l; push @l, $1.$2 while $string =~ /(\w)(\1*)/g; print join ',', @l; # outputs aa,bbbbbb,ccccc,aaa,dd,ee,f,g,hhhh
    Of course, this uses a regex and a while, etc...
Re: Splitting a String into n-character parts, without using a regex
by merlyn (Sage) on Jan 26, 2001 at 03:50 UTC
    The "without using a regex" really smells like homework, unless there's more motivation for it. As the other examples have shown here, most people would simply use a regex. What do you find disturbing about using such?

    -- Randal L. Schwartz, Perl hacker


    Update: ok, so it may or may not be homework. But I'm still confused about why people ask for pessimized solutions. I really had a legitimate curiousity about the unstated motivation to use something other than an optimal solution. So far, I only know of ignorance, homework, or a hidden subtext to be reasons. This might have been something else, but I don't know until I ask.
      Goodmorning,

      No, It isn't homework ... (However, when I start asking
      Java-related questions here, you could suspect me of doing
      so ;-)
      I was trying to make an obfuscation. And I really wanted it
      to be just 1 line, using the while loop in it, would cause
      problems ... because I wanted to feed the @row to a map.
      Something like:
      my @newrow=map hex, while('aabbccdd'=~/\w{2}/g);
      (That is want I wanted but, which apparently was not possible. while
      just doesn't return a row) However, using a regex apparently was a good
      option. Only for one reason or the other, the first time I
      tried using the @row='aabbccddeefforwhatever'=~/\w{2}/g, it
      didn't work. So I must have done something wrong. That's more
      or less the reason I thought it couldn't be done using a regex
      ... which ofcourse was pretty silly and ignorant of me ;-)

      Btw. Everybody thanks for their help !!! (I almost finished
      the obfuscation) and next time I'll try longer before bothering
      you ;-)
        See, that would have helped me tremendously, and perhaps many of the other posters. "I'm trying to make it an obfuscation". Please use that phrase in the future if that's your goal... I'm sure many of us can come up with pulling out solutions. In fact some of us here spend far too much time doing that. {grin}

        Otherwise, I'm just left with "why the odd goal", and I'm very goal directed so I have to understand the context of each one. I probably wouldn't survive long in a corporate world where things get done just because the boss says so. {grin}

        Thanks for the clarification.

        -- Randal L. Schwartz, Perl hacker

Re: Splitting a String into n-character parts, without using a regex
by Coyote (Deacon) on Jan 26, 2001 at 03:53 UTC
    I would stick with the regex, but since you asked and I've been looking for a good reason to use the unpack function...
    use strict; use warnings; use Data::Dumper; my $size = 2; my $string='aabbccddee'; my $template = "a$size" x (length($string)/$size); if (length($string) % $size){ $template .= "a" . (length($string) % $size); } print "Unpack Template: $template \n\n"; my @parts = unpack($template, $string); print Dumper(\@parts);
    Output:

    Unpack Template: a2a2a2a2a2 $VAR1 = [ 'aa', 'bb', 'cc', 'dd', 'ee' ];

    ----
    Coyote

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://54413]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2014-10-26 08:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (152 votes), past polls