Re: Returning Values fr nested FOR w/o RETURNing fr sub

2006-07-30 Thread John W. Krahn
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;
'
0001   1   1   1
0010   2   2   2
0100   4   4   4
1000   8  10   8
0001  16  20  10
0010  32  40  20
0100  64 100  40
1000 128 200  80




John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Returning Values fr nested FOR w/o RETURNing fr sub

2006-07-30 Thread Charles K. Clarkson
Alex Gill wrote:

: My question is:
: How can the 'for' loop within bases() output a list for 'print'
: as invoked in the last line of code?

   It can't. You'll have to wait until the loop finishes unless
you print directly from the loop which is less robust.

my @powers;
push @powers, 2 ** $_ for 0 .. 7;

print bases( @powers );

sub bases {
my @bases;
foreach my $base (@_) {
push @bases,
sprintf( '%08b', $base ) .
sprintf( '%4d',  $base ) .
sprintf( '%4o',  $base ) .
sprintf( '%4X',  $base ) .
"\n";
}
return @bases;
}

To save some time you could pass a reference to @powers.

print bases( [EMAIL PROTECTED] );

sub bases {
my $powers_ref = shift;
my @bases;
foreach my $base ( @$powers_ref ) {
push @bases,
sprintf( '%08b', $base ) .
sprintf( '%4d',  $base ) .
sprintf( '%4o',  $base ) .
sprintf( '%4X',  $base ) .
"\n";
}
return @bases;
}

If you understand what it does, map() can compact things.

sub bases {
my $powers_ref = shift;

return
map {
sprintf( '%08b', $_ ) .
sprintf( '%4d',  $_ ) .
sprintf( '%4o',  $_ ) .
sprintf( '%4X',  $_ ) .
"\n";
}
@$powers_ref;
}


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
Free Market Advocate
Web Programmer

254 968-8328

Don't tread on my bandwidth. Trim your posts.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Returning Values fr nested FOR w/o RETURNing fr sub

2006-07-30 Thread Jeff Peng



 for (@_) {
 sprintf ("%08b", $_).
 sprintf ("%4d", $_).
 sprintf ("%4o", $_).
 sprintf ("%4X", $_).
 "\n";
 }


Would you maybe use 'printf' instead of 'sprintf' above?



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]