For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2006 > Returning Values fr nested FOR w/o RETURNing fr sub









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Returning Values fr nested FOR w/o RETURNing fr sub
Alex Gill

2006-07-30, 9:57 pm

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?

TIA,
Alex Gill
Jeff Peng

2006-07-30, 9:57 pm


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


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


Charles K. Clarkson

2006-07-31, 3:57 am

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( \@powers );

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.

John W. Krahn

2006-07-31, 3:57 am

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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com