in reply to Difficulties with split

Think about it. You are splitting ON any two characters. If you were splitting on, say, a comma (","), would you expect commas to end up in the result? Some options, one, use a zero-width look behind assertion:
split /(?<=..)/, $in; # johngg is right...bad answer
or capture what you're splitting on, and filter out empty elements:
grep { length($_) > 0 } split /(..)/, $in;
or do like almut says above and use unpack :-)

Replies are listed 'Best First'.
Re^2: Difficulties with split
by johngg (Canon) on Apr 28, 2009 at 13:51 UTC
    split /(?<=..)/, $in;

    I don't think that is going to do quite what you expected. It will split the string at every point that is preceded by two characters.

    $ perl -le ' -> $str = q{abcdefg}; -> @bits = split m{(?<=..)}, $str; -> print for @bits;' ab c d e f g $

    It seems that split is not perhaps the handiest tool for this task.

    Cheers,

    JohnGG

Re^2: Difficulties with split
by transiency (Sexton) on Apr 27, 2009 at 23:22 UTC
    That is a good point, i forgot split removes the delimitter heh, and should've tried capping it.

    I've never heard of a look behind assertion, it sounds interesting...

    Thanks runrig :)

    foreach(@the_wicked){sleep(0);}