in reply to printing elements of an array on new lines

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;
  • Replies are listed 'Best First'.
    Re: Re: printing elements of an array on new lines
    by Not_a_Number (Prior) on Aug 23, 2003 at 18:07 UTC

      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