From: Peng Yu on
@array=("a", "b", "c");
print "@array\n";

The above code will not print a separator (say a newline) between the
elements. I could use a foreach loop to do so. But I feel that there
might be a more convenient way. Could somebody let me know if there is
one?

foreach (@array) {
print;
print "\n";
}
From: sreservoir on
On 1/25/2010 5:25 PM, Peng Yu wrote:
> @array=("a", "b", "c");
> print "@array\n";
>
> The above code will not print a separator (say a newline) between the
> elements. I could use a foreach loop to do so. But I feel that there
> might be a more convenient way. Could somebody let me know if there is
> one?
>
> foreach (@array) {
> print;
> print "\n";
> }

perldoc -fjoin

--

"Six by nine. Forty two."
"That's it. That's all there is."
"I always thought something was fundamentally wrong with the universe"
From: John Bokma on
Peng Yu <pengyu.ut(a)gmail.com> writes:

> @array=("a", "b", "c");
> print "@array\n";

@array = qw( a b c );
$" = "\n";
print "@arr";

$" is the list separator, if you do:

use English;

you can use
$LIST_SEPARATOR = "\n";

which is default a space. (Note that modifying $" or the English variant
stays in effect which might be what you want, or not).

Other options:

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

> foreach (@array) {
> print;
> print "\n";
> }

shorter:

for ( @array) {
print "$_\n";
}

or:

print "$_\n" for @array;

or:

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

and one I which I don't like much:

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

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
From: John W. Krahn on
John Bokma wrote:
>
> and one I which I don't like much:
>
> map { print "$_\n" } @array;

Because it *should* be:

print map "$_\n", @array;



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
From: Tad McClellan on
Peng Yu <pengyu.ut(a)gmail.com> wrote:

> @array=("a", "b", "c");


You should always enable strictures in your Perl programs:

use strict;

then make that

my @array=("a", "b", "c");


> print "@array\n";
>
> The above code will not print a separator (say a newline) between the
> elements.


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


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
 |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8
Prev: LibXML (XPATH) and escape
Next: Module name access