This subroutine works fine for cases where 'n' is greater than one. I had to create an additional sub called UnPackOne for the unique case of 1 input. When I call this routine with one parameter, the routine always returns '1' and not the array (which it did create correctly). I think it forces the context to scalar!
Is there a way to force the return value to always be treated as a array list?# our $Zeropack = "\0" x 4; # our $MaxNum32bit = 2 ** 32; # Use: ## n - number of 64bit pack +ed numbers in string # my @Numbers = UnPack( n, $stringpack ); ## string input, array outp +ut sub UnPack { my $todo = shift; my $input = shift; my @output = (); my $arno = 0; while( $arno < $todo ) { my $pack = substr( $input, $arno * 8, 8 ); if ( substr($pack,0,4) eq $Zeropack ) { $output[$arno] = unpack("N", substr( $pack, 4, 4 ) ); } else { my ( $upper, $lower ) = unpack( "N N", $pack ); if ( ! defined $upper ) { $upper = 0; } if ( ! defined $lower ) { $lower = 0; } $output[$arno] = ( $upper * $MaxNum32bit ) + $lower ; } $arno++ } return ( @output ); }
Thank you
"Well done is better than well said." - Benjamin Franklin
In reply to Force 'sub' return to be treated in list context? by flexvault
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |