in reply to Breaking up a string?

How inefficient is it? How ugly is your code? How big is your string? What have you tried? Did you try a regex? What did it look like?

Replies are listed 'Best First'.
Re^2: Breaking up a string?
by Anonymous Monk on Feb 19, 2008 at 14:48 UTC
    What I tried was using substr to get the first 6 chars, reversing the string, chop'ing it 6 times and reversing it again - all in a repeating loop. There has to be a better way than that :\

      Here's a somewhat more Perlish solution:

      my @sets_of_six = ( $s =~ m{ (.{6}) }xmsg );

        [0] Perl> print $string;; 2648117951505438300028047571976236505346 @sets_of_six = ( $string =~ m{ (.{6}) }xmsg );; print for @sets_of_six;; 264811 795150 543830 002804 757197 623650

        Note the missing last four characters. Can be fixed (and the parens are extraneous):

        @sets_of_six = $string =~ m{ (.{0,6}) }xmsg;; print for @sets_of_six;; 264811 795150 543830 002804 757197 623650 5346

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
      If you got your six characters, why did you perform that reverse - 6 times chop - reverse operation on it? Can't you post a little example code that runs, some example input and the result you get, with an explanation on how your result differ from what you want?
        Because I then wanted to get the next 6 chars, then the 6 after that, until the end of the string (length is a multiple of 6).