in reply to printing all elements of arrays

(I)s there any way to print all the elements of any array instead of referencing each element like so [0], [1], [2] ...etc?

If you want to print out each element of an array on its own line,

print join("\n", @array), "\n";
is probably the easiest way to do it. If the extra "\n" offends you,
print map {$_ . "\n"} @array;
will do much the same thing, but it's less idiomatic (I think).

--
F o x t r o t U n i f o r m
Found a typo in this node? /msg me
% man 3 strfry

Replies are listed 'Best First'.
Re^2: printing all elements of arrays
by ysth (Canon) on Jul 13, 2004 at 01:17 UTC
    I like print join("\n", @array, ""), myself.
Re^2: printing all elements of arrays
by drock (Beadle) on Jul 13, 2004 at 01:26 UTC
    what does the print map state? what perldoc can I read about the map call?
      what does the print map state?

      Okay,

      print join("\n", @array), "\n";
      says "Stick a newline between each element in @array, return that as a string, print it, and print a newline at the end." On the other hand,
      print map {$_ . "\n"} @array;
      says "Build a list whose elements are the elements of @array with "\n" appended to them, then print that list."

      what perldoc can I read about the map call?

      You can read about any function with perldoc -f func_name.

      --
      F o x t r o t U n i f o r m
      Found a typo in this node? /msg me
      % man 3 strfry

        Here is another way to print the elements of an array on separate lines, using the $, variable (see perlvar:

        #!/usr/bin/perl use strict; use warnings; my @array = (1..10); local $, = "\n"; print @array;
      perldoc -f map.

      map is a list-transformer; you give it a block of code and a list, and it applies the code to each element of the list, and returns a list made up of what the code returned for each element.

      You can read perlfunc to find out about map. Also, the easiest way to print all the elements of an array is:

      print "@array\n";