indapa has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to assign the result of unpack to an array:
@a = unpack(A10, $some_string);
My thinking was that the array would have the list of 10-length substrings taken from $some_string. But it appears that you cannot assign the result of unpack to an array. What I get is just the first 10 characters of the string.

Is there a way to work around this? I have list of of length >300, that I want to print out in length 10 substrings.

Thanks for the help.

Replies are listed 'Best First'.
Re: using unpack
by Albannach (Monsignor) on Apr 18, 2001 at 06:15 UTC
    While greenfox has a good solution, if you really must have your strings 10 characters long, then you'd better use 'a' instead of 'A', as the latter will drop trailing spaces or nulls when unpacking.

    --
    I'd like to be able to assign to an luser

Re: using unpack
by I0 (Priest) on Apr 18, 2001 at 04:11 UTC
    @a = unpack(A10 x ((length$some_string)/10),$some_string)

      which loses characters if the string length is not an exact multiple of 10 (may not be a problem in this case?) One solution-

      my @a = unpack('A10' x (length($string)/10) . 'A' . length($string)%10 +,$string);

      --
      my $chainsaw = 'Perl';