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

Perhaps this is a newbie question, but please be kind. I'm a newbie.

Given the following code:

my @array = qw/zero one two three/; print @array, $/; print "@array\n"; print "${@array}\n";
I get the following output:
14:38:57 ~/perl/source $ perl -w array zeroonetwothree zero one two three 14:38:59 ~/perl/source $
Without resorting to something akin to the following:
my @array = qw/zero one two three/; my $n = scalar @array; print "$n\n";
...which upon output yields:
14:44:42 ~/perl/source $ perl -w array 4 14:44:49 ~/perl/source $
How can I modify the interpolated string such that the element count of the list/array is substituted instead of the array contents?

Thanks.

Janitored by Arunbear - replaced pre tags with code tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: forcing array to scalar context in string interpolation?
by borisz (Canon) on Oct 20, 2004 at 21:57 UTC
    my @aa = qw/a b c d/; print scalar @aa, "\n"; print "@{[scalar(@aa)]}\n";
    Boris
Re: forcing array to scalar context in string interpolation?
by BrowserUk (Patriarch) on Oct 20, 2004 at 22:01 UTC

    print @a . "\n"; print "${\(0+@a)}";

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: forcing array to scalar context in string interpolation?
by kvale (Monsignor) on Oct 20, 2004 at 21:56 UTC
    No need to put everthing inside double quotes. Try
    print scalar @array, "\n";

    -Mark

Re: forcing array to scalar context in string interpolation?
by qumsieh (Scribe) on Oct 20, 2004 at 23:16 UTC
    print 0+@array, "\n"; print "@{[scalar @array]}\n";
Re: forcing array to scalar context in string interpolation?
by TedPride (Priest) on Oct 21, 2004 at 04:34 UTC
    Heh, all he wants is the number of elements in the array:
    my @array = qw/zero one two three/; print $#array;
    Where the number printed is the number of elements -1.