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

Dear Monks, I want to print out each element of an @array to a new line rather than all in one line separated by spaces when I:
print @array;
Is there an easy way to do this?

Replies are listed 'Best First'.
Re: printing elements of an array on new lines
by Limbic~Region (Chancellor) on Aug 23, 2003 at 04:57 UTC
    Anonymous Monk,

    print "$_\n" for @array;

    Cheers - L~R

    Update: I love Perl

    In a short span of time numerous solutions have been provided. For such a simple task as printing the elements of an array - lots of Perl has been used:

  • bare blocks to limit scope
  • The need to use local instead of my on Perl built in global variables
  • printf for flexibility over print
  • Print many with a for loop versus printing once with join
  • Weird built in variables such as $_, $", $/, and $,

    Let me add a couple more using $\ and map

    { local $\ = "\n"; print $_ for @array; } or print map {$_ .= "\n"} @array;

      Limbic~Region

      Re this snippet:

      print map {$_ .= "\n"} @array;

      As the OP is obviously a beginner, I believe that the proviso to this, namely that it changes @array (adding a newline to each element), should be clearly stated.

      dave

        dave,
        You ever start typing a word and end up automatically type something else out of habit? That should have been:
        print map {$_ . "\n"} @array;
        Thanks - L~R
Re: printing elements of an array on new lines
by Zaxo (Archbishop) on Aug 23, 2003 at 05:11 UTC

    Without stringification beyond what print does to a list,

    { local $, = $/; print @array; }

    After Compline,
    Zaxo

Re: printing elements of an array on new lines
by bobn (Chaplain) on Aug 23, 2003 at 04:59 UTC

    { local $" = "\n"; print "@array"; }
    or:
    print join("\n", @array);

    --Bob Niederman, http://bob-n.com

    All code given here is UNTESTED unless otherwise stated.

Re: printing elements of an array on new lines
by blue_cowdawg (Monsignor) on Aug 23, 2003 at 04:56 UTC

    printf "%s\n",join("\n",@array);
    just that simple.


    Peter @ Berghold . Net

    Sieze the cow! Bite the day!

    Nobody expects the Perl inquisition!

    Test the code? We don't need to test no stinkin' code!
    All code posted here is as is where is unless otherwise stated.

    Brewer of Belgian style Ales

Re: printing elements of an array on new lines
by jeffa (Bishop) on Aug 23, 2003 at 14:06 UTC
    And here is another that uses the -l command switch (and that's dash elle, not dash one).
    #!/usr/bin/perl -l my @array = 0..9; print for @array;

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Why not simple over fancy?? my @people = ('fred', 'bill', 'charley'); foreach my $person (@people) { print "$person\n" };
        Because this is Perl and, repeat after me, "There Is More Then One Way To Do It"
        And why don't I learn to format my answer :)