A tiny program for a heavy duty... its a so slick regexp, I couldnt miss to post it here for all to see!
Originally ideated for a post someone did, but at which I missed to answer. The point was on dividing a string on 100 characters until it was possible. Well everybody used SUBSTR, but it didn't satisfy me... regexp is the way!

Follows the £337 version
#!usr/bin/perl $initial = "qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwerty +uiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcv +bnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdf +ghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm"; $_= $initial; $vardivider= 33; $initiallength= length($initial); @varformposts= (/(.{$vardivider}|.+$)/g); #THIS IS IT! print "\nLength: " . $initiallength ." - Division by $vardivider: " . +int($initiallength/ $vardivider) . " - Mod: " . $initiallength % $var +divider . "\n"; print join("*\n", @varformposts);
AAaauuuuuhhhh! One row for such a nasty splitting job! £337!
And now for the £173 version
#!usr/bin/perl $initial = "qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwerty +uiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcv +bnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdf +ghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm"; $_= $initial; @varformposts= (/(.{33}|.+$)/g); #OH MY! All the job in one single reg +exp! print join("*\n", @varformposts);


Man I feel better now, knowing that such a beauty would not be left alone in my HD.

Cheers

Replies are listed 'Best First'.
Re: So nice, its poetry
by japhy (Canon) on Apr 30, 2002 at 16:12 UTC
    You can do it without alternation, too: @chunks = /.{1,33}/g; You might want to add the /s modifier, just in case...

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;