in reply to unpack() to several variables

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.

Replies are listed 'Best First'.
Re^2: unpack() to several variables
by psini (Deacon) on Jul 22, 2009 at 13:10 UTC

    Maybe using an array slice? As in (untested):

    my(@ids,$text,$length); (@ids[0..9],$text,$length) = unpack("N10C40n",$buff) ;

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

      Please, note that with those lines of code instead of geting the C40 to $text I get C1 to $text and C2 to $length