Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print list elements separated by line feeds in Perl?

Tags:

perl

What is the easiest way to print all a list's elements separated by line feeds in Perl?

like image 972
Sergey Stadnik Avatar asked Dec 08 '09 01:12

Sergey Stadnik


People also ask

How do I print an array of elements in a new line in Perl?

When you specify a newline character in this context, make sure to surround it with double quotes, not single quotes, or a backslash and an ``n'' will be used to join the pieces of the array. Thus, to print the numbers from 1 to 10, each on a single line, you could use the following perl statement: print join("\n",1..

What is Perl splice?

In Perl, the splice() function is used to remove and return a certain number of elements from an array. A list of elements can be inserted in place of the removed elements. Syntax: splice(@array, offset, length, replacement_list) Parameters: @array – The array in consideration.

How do I go to a new line in Perl?

Perl newline and tab examples print "Here is a tab\t"; print "Here is a form feed\f"; print "Finally, here's a bell a newline\a\n"; As a practical matter, it's very common to print Perl newline characters, and it's relatively common to print tab characters.

How do I print a Perl script?

print() operator – print operator in Perl is used to print the values of the expressions in a List passed to it as an argument. Print operator prints whatever is passed to it as an argument whether it be a string, a number, a variable or anything. Double-quotes(“”) is used as a delimiter to this operator.


2 Answers

print "$_\n" for @list;

In Perl 5.10:

say for @list;

Another way:

print join("\n", @list), "\n";

Or (5.10):

say join "\n", @list;

Or how about:

print map { "$_\n" } @list;
like image 163
Chris Lutz Avatar answered Oct 26 '22 23:10

Chris Lutz


Why not abuse Perl's global variables instead

local $\ = "\n";
local $, = "\n";
print @array;

If you get excited for unnecessary variable interpolation feel free to use this version instead:

local $" = "\n";
print "@array\n";
like image 22
jonchang Avatar answered Oct 27 '22 00:10

jonchang