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]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to