The assignment to the array will eat up all the values so no you can't do it in just one go (if the array comes before the individual scalars; if the scalars came first it would have just worked as you have it now); you could however capture everything in @ids then use splice or pop to remove $text and $length afterwards.

Update: D'oh, yeah it'll work fine with a slice of fixed length as psini proffers below. Ned moar caffeine apparently . . .

Update 2: Well, will work with a more appropriate unpack string ("c40" returns a list of 40 characters-as-numbers, not a string of 40 characters); "a", "A", or "Z" is probably more what the OP's looking for.

use strict; use warnings; use YAML::Syck qw( Dump ); my $src = pack( "N10 a5 n", 0..9, "abcde", 99 ); my( @a, $b, $c ); (@a[0..9],$b,$c) = unpack( "N10 a5 n", $src ); print Dump( { a => \@a, b => $b, c => $c } ), "\n"; exit 0; __END__ --- a:   - 0   - 1   - 2   - 3   - 4   - 5   - 6   - 7   - 8   - 9 b: abcde c: 99

The cake is a lie.
The cake is a lie.
The cake is a lie.


In reply to Re: unpack() to several variables by Fletch
in thread unpack() to several variables by Saladino

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.