in reply to seperating a string with substr...

I think you meant substr($data,0,8,'') .. otherwise $data doesn't get modified and it'll be an infinite loop :)

Note that re: your #1, $data is also logically false if it is an empty string '' or undef (see True or False? A Quick Reference Guide for a good reference)

As for an alternative, you could also use a regex (there's probably a split solution as well--this is close: my @pieces = split /(.{1,3})/, $x):
my @pieces = $data =~ /(.{1,8})/g;

Replies are listed 'Best First'.
Re^2: seperating a string with substr...
by blokhead (Monsignor) on Jun 30, 2006 at 00:07 UTC
    A little cleaner than m//g or split is to use unpack for this purpose:
    my @pieces = unpack "(A8)*", $data;
    Someone correct me if I'm wrong, but I seem to remember that this unpack syntax (paren-groupings and star) may not have been around for all of Perl 5, so it's worth double-checking with your version of perl.

    blokhead

Re^2: seperating a string with substr...
by ysth (Canon) on Jun 30, 2006 at 05:05 UTC
    If $data may contain newlines, you'd want instead:
    my @pieces = $data =~ /.{1,8}/gs;
    (Also note that your () were unnecessary, since list context //g will return the entire match if there are no submatches.)
Re^2: seperating a string with substr...
by abachus (Monk) on Jun 30, 2006 at 00:04 UTC

    ooops :) fwiw i have an open window with the code and it has substr($data,0,8,'') in it! I was trying to word the english properly and overlooked the most important bit, the code...previewed like about 3 times aswell, 'shrug' oh well.

    As for if $data contains no bytes, or undef or a zero its false isn't it ? nothing else ?

    The regex bit looks interesting though, i think i'll go have a play with that, thanks !

    Isaac.