From: Ben Morrow on

Quoth Peng Yu <pengyu.ut(a)gmail.com>:
> @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?

See $" in perlvar, and remember to restrict the scope of any changes
with local.

Ben

From: Brad Baxter 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";
> }

The above code positively does printer a separator (a space)
between the elements. Change $" to the separator you want.

use warnings;
use strict;

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

$" = "\n";

print "@array\n";

--
Brad

From: John Bokma on
"John W. Krahn" <someone(a)example.com> writes:

> John Bokma wrote:
>>
>> and one I which I don't like much:
>>
>> map { print "$_\n" } @array;
>
> Because it *should* be:
>
> print map "$_\n", @array;

Duh, that one is nearly the same as the first one I listed under "other
options" ;-)

As for *should*, I don't like map in void context but that's nowadays
more a matter of taste than anything else as far as I know.

I wouldn't use it though, but like Abigail, IIRC, once wrote, there is
no reason to have a problem with map in void context because we use
other functions in void context without problems (like print).

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
From: Uri Guttman on
>>>>> "JB" == John Bokma <john(a)castleamber.com> writes:

JB> I wouldn't use it though, but like Abigail, IIRC, once wrote, there is
JB> no reason to have a problem with map in void context because we use
JB> other functions in void context without problems (like print).

the point with map in void context is not efficiency but in conveying
meaning to the reader. map is intended to generate a list, not execute
side effects. for modifier does the same thing and is meant for side
effects as it doesn't generate a list. perl has many related things like
this and you should choose the one with better semantics for your
intentions. map generates lists so use it that way. for modifier doesn't
generate lists so use it for side effects.

uri

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: Dr.Ruud on
Peng Yu wrote:

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

1. "array" is the stupidest name for an array.
2. whitespace is cheap
0. use strict; use warnings;

my @word = qw/ a b c /;

my @word = "a" .. "c";

--
Ruud
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8
Prev: LibXML (XPATH) and escape
Next: Module name access