EchoAngel has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, say i have an array and this array could contain 2 or 3 elements. first case is element has : apple , oranges. i would go through some foreach loop to access the elements but I want to print it to the screen like
Missing 1 element , current list : apple oranges
i want to display those elements with specific spacing like the printf statement but i dont' know how u do that with appending to other text.

Replies are listed 'Best First'.
Re: Printf Question
by tachyon (Chancellor) on Aug 31, 2004 at 14:30 UTC
    my @print_elements = map{ sprintf "%20s", $_ } @elements; my $missing = $WANTED - @elements; if ( $missing > 0 ) { print "Missing $missing element(s), current list: @print_elements\ +n"; } else { # blah }

    cheers

    tachyon

Re: Printf Question
by ikegami (Patriarch) on Aug 31, 2004 at 15:34 UTC

    You want spacing like printf without the line break? Just omit the "\n".

    if (...some are missing...) { printf("Missing %d element , current list : ", $missing_count); printf("%-9s", $_) foreach (@list); print("\n"); }
Re: Printf Question
by revdiablo (Prior) on Aug 31, 2004 at 18:16 UTC

    Another approach would be to dynamically generate a template for printf. Here's an example:

    my @missing = qw(apple oranges); my $template = "Missing %d element , current list :" . ("%9s") x @missing . "\n"; printf $template, scalar @missing, @missing;
Re: Printf Question
by ccn (Vicar) on Aug 31, 2004 at 14:21 UTC
Re: Printf Question
by pelagic (Priest) on Aug 31, 2004 at 14:29 UTC
    Maybe it's that simple:
    use strict; my @fruit = qw/apple oranges/; print '<', join (' ', @fruit), '>'; ___OUTPUT___ <apple oranges>

    pelagic
Re: Printf Question
by qumsieh (Scribe) on Aug 31, 2004 at 18:51 UTC
    i dont' know how u do that with appending to other text.

    In addition to what the others said, you can use sprintf() which returns a formatted string which you can append to a variable.