flexvault has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Force 'sub' return to be treated in list context?
by GrandFather (Saint) on Apr 04, 2012 at 10:57 UTC | |
by flexvault (Monsignor) on Apr 04, 2012 at 11:46 UTC | |
|
Re: Force 'sub' return to be treated in list context?
by moritz (Cardinal) on Apr 04, 2012 at 09:45 UTC | |
by flexvault (Monsignor) on Apr 04, 2012 at 10:33 UTC | |
|
Re: Force 'sub' return to be treated in list context?
by jwkrahn (Abbot) on Apr 04, 2012 at 17:45 UTC | |
by flexvault (Monsignor) on Apr 05, 2012 at 08:39 UTC |