in reply to Unpacking to hash, with context
++ for taking unpack and making it nearly readable :-). However, I have to wonder why, if you're writing it anyway, you're joining on spaces, and then splitting on spaces. Wouldn't it be slightly more readable if you did:
This would arguably be faster (arguable because even going down that road might be a premature optimisation), but I also just think it cleaner. Of course, if you're doing this anyway, you could also just pre-split a bunch of the text, e.g.:my $h = unpack2hash( [ '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);
Also think about reversing the order of the parameters, then we could get rid of the array-ref:my $h = unpack2hash( [ [ qw(l $ songid) ], [ qw(c8 @ signature) ], #...
These are all mere suggestions, so take, or not, as you want - which is why I didn't repeat the previous suggestion to possibly pre-split. Of course, you could also be dynamic and if passed a scalar, split it yourself, but if you're passed an array ref, you could treat it as pre-split. But that would depend on usage.my $h = unpack2hash( $data, 'l:$songid', 'c8:@signature', #... );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Unpacking to hash, with context
by Fox (Pilgrim) on Feb 17, 2010 at 14:01 UTC |