icon Top 9 categories map      RocketAware > Perl >


Tips: Browse or Search all pages for efficient awareness of Perl functions, operators, and FAQs.


Search Perl pages


By activity
Professions, Sciences, Humanities, Business, ...

User Interface
Text-based, GUI, Audio, Video, Keyboards, Mouse, Images,...

Text Strings
Conversions, tests, processing, manipulation,...

Integer, Floating point, Matrix, Statistics, Boolean, ...

Algorithms, Memory, Process control, Debugging, ...

Stored Data
Data storage, Integrity, Encryption, Compression, ...

Networks, protocols, Interprocess, Remote, Client Server, ...

Hard World
Timing, Calendar and Clock, Audio, Video, Printer, Controls...

File System
Management, Filtering, File & Directory access, Viewers, ...

Unpack does the reverse of pack: it takes a string representing a structure and expands it out into a list value, returning the array value. (In a scalar context, it returns merely the first value produced.) The TEMPLATE has the same format as in the pack function. Here's a subroutine that does substring:

    sub substr {
        local($what,$where,$howmuch) = @_;
        unpack("x$where a$howmuch", $what);

and then there's

    sub ordinal { unpack("c",$_[0]); } # same as ord()

In addition, you may prefix a field with a %<number> to indicate that you want a <number>-bit checksum of the items instead of the items themselves. Default is a 16-bit checksum. For example, the following computes the same number as the System V sum program:

    while (<>) {
        $checksum += unpack("%16C*", $_);
    $checksum %= 65536;

The following efficiently counts the number of set bits in a bit vector:

    $setbits = unpack("%32b*", $selectmask);

Source: Perl builtin functions
Copyright: Larry Wall, et al.
Next: untie VARIABLE

Previous: unlink LIST

(Corrections, notes, and links courtesy of RocketAware.com)

[Overview Topics]

Up to: Data structures (In memory)

Rapid-Links: Search | About | Comments | Submit Path: RocketAware > Perl > perlfunc/unpack.htm
RocketAware.com is a service of Mib Software
Copyright 2000, Forrest J. Cavalier III. All Rights Reserved.
We welcome submissions and comments