Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

using unpack

by indapa (Monk)
on Apr 18, 2001 at 03:26 UTC ( #73389=perlquestion: print w/replies, xml ) Need Help??

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 (Prior) 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';

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://73389]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2019-12-13 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?