the template syntax is a bit different, see the example:sub unpack2hash { my ($template, $source) = @_; my $hash = {}; foreach(split / /,$template) { my ($temp,$type,$var) = split /:(.)/; if($type eq '@') { my @r = unpack $temp, $source; $hash->{$var} = \@r; my $pack = pack $temp, @r; substr $source, 0, length $pack, ''; }elsif($type eq '$') { my $r = unpack $temp, $source; $hash->{$var} = $r; my $pack = pack $temp, $r; substr $source, 0, length $pack, ''; } else{ die "need context type\n" } } return $hash; }
each value is separated by whitespace, in the format <template>:<context><key>my $h = unpack2hash(join(' ',( 'l:$songid', 'c8:@signature', 'l:$genre', 'f:$bpm', 's3:@level', 's:$unk', 'l12:@unk2', 'a24:$genres', 'l2:@unk3', 'a32:$title', 'a32:$subtitle', 'a32:$artist', 'a32:$noter', 'a32:$musicfile', 'l:$jpg', 'l3:@unk4', 'l4:@notepos' )), $data);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Unpacking to hash, with context
by Tanktalus (Canon) on Feb 16, 2010 at 19:48 UTC | |
by Fox (Pilgrim) on Feb 17, 2010 at 14:01 UTC |