Alex Gill wrote:
> push @powers, 2**$_ for 0..7;
> 
> sub bases {
>  for (@_) {
>      sprintf ("%08b", $_).
>      sprintf ("%4d", $_).
>      sprintf ("%4o", $_).
>      sprintf ("%4X", $_).
>      "\n";
>  }
> }
> 
> print bases @powers;
> --------------------------------------------------------------------------------------
> 
> I'm learning about Networking and wrote this to display ways numbers can
> be represented.
> 
> My question is:
> How can the 'for' loop within bases() output a list for 'print' as
> invoked in the last line of code?
> I've tried to using return() in the for loop, but that exits the bases()
> too soon.
> Some solutions: (1) put the print within the bases().  (2) push() into
> an array and then iterate over for printing.  --- These work but I am
> wondering if there is a way to return() a list from the for-loop?

You probably need to use map:

$ perl -e'
@powers = map 2 ** $_, 0 .. 7;
print map sprintf( "%08b%1\$4d%1\$4o%1\$4X\n", $_ ), @powers;
'
00000001   1   1   1
00000010   2   2   2
00000100   4   4   4
00001000   8  10   8
00010000  16  20  10
00100000  32  40  20
01000000  64 100  40
10000000 128 200  80




John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to