Printing Array's content with carriage return (\n)

J

John Bokma

Edward said:
Hi,

If I have an array: @myarray = (ab, bc, cd, ...)

currently if I use the print command directly like this:

print "@myarray\n";

it returns:
ab bc cd ....

is there anyway I can tweak the print command?

yes, but read on...
so that it gives

ab
bc
cd
...

Namely, it print out in vertical forms.

try: print join("\n", @array), "\n";
 
J

Jim Cochrane

Hi,

If I have an array: @myarray = (ab, bc, cd, ...)

currently if I use the print command directly like this:

print "@myarray\n";

it returns:
ab bc cd ....

is there anyway I can tweak the print command?
so that it gives

ab
bc
cd
...

If by tweak you're implying overriding print, it's probably possible, but
might not be a good idea. Altenratively, you could do:

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

Edward Wijaya

Hi,

If I have an array: @myarray = (ab, bc, cd, ...)

currently if I use the print command directly like this:

print "@myarray\n";

it returns:
ab bc cd ....

is there anyway I can tweak the print command?
so that it gives

ab
bc
cd
....

Namely, it print out in vertical forms.

Thanks so much for your time

Regards
Edward WIJAYA
SINGAPORE
 
T

Tad McClellan

Edward Wijaya said:
Subject: Printing Array's content with carriage return (\n)


A "carriage return" is the ASCII CR character.

A "line feed" is the ASCII LF character.

A "newline" (\n) is a *logical* end-of-line, and it is different
on different operating systems:

LF - *nix
CR - mac
CR+LF - windows (and many of the common protocols)


So your Subject is only accurate on a Mac. :)

I think you meant this instead:

Subject: Printing Array's content with newline (\n)

currently if I use the print command directly like this:

print "@myarray\n";
[snip]

is there anyway I can tweak the print command?


I dunno, but there are several ways to get what you want without
tweaking the print function/operator (not a "command").

Namely, it print out in vertical forms.


# tweek how arrays are interpolated, rather than print() itself
{ local $" = "\n"; # see perlvar.pod
print "@myarray\n";
}

or

# _say_ that you want them joined with newlines
print join("\n", @myarray), "\n";



I'd prefer the second one, it is more self-documenting.
 
J

John W. Krahn

Tad said:
I dunno, but there are several ways to get what you want without
tweaking the print function/operator (not a "command").


# tweek how arrays are interpolated, rather than print() itself
{ local $" = "\n"; # see perlvar.pod
print "@myarray\n";
}

TMTOWTDI :)

{ local $, = local $\ = "\n"; # see perlvar.pod
print @myarray;
}



John
 
D

David Efflandt

If I have an array: @myarray = (ab, bc, cd, ...)

currently if I use the print command directly like this:

print "@myarray\n";

it returns:
ab bc cd ....

is there anyway I can tweak the print command?
so that it gives

ab
bc
cd
...

Namely, it print out in vertical forms.

If you want to print like that, but NOT insert newlines in your data, it
may be better to simply:

foreach (@myarray) { print "$_\n"; }
 
U

Uri Guttman

lv> I usually use with preference on #2:

lv> print map {"$_\n"} @myarray;

often i do that with some common variations

lv> or

lv> map {print "$_\n"} @myarray;

alert! map in void context! (hush abigail!).

anyhow try benchmarking those two. i will wager that the former is much
faster than the latter. this segues right into my rule about printing:

print rarely, print late.

uri
 
T

Tad McClellan

l v said:
I usually use with preference on #2:

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

or

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


_Why_ do you prefer #2?


I think most people would prefer #1 because it says "print bunch 'o stuff"
while #2 says "bunch 'o stuff print".
 
M

Matija Papec

X-Ftn-To: David Efflandt

If you want to print like that, but NOT insert newlines in your data, it
may be better to simply:

foreach (@myarray) { print "$_\n"; }

Second that, I would only drop braces and use modifier,

print "$_\n" for @myarray;
 
T

Tad McClellan

l v said:
"print bunch 'o stuff" -- I like it!


It is just an uneconomical way of saying "list". :)

#2 purely for comprehension by those in my work area which are VERY
inexperienced with Perl.


map() is a foreach() in disguise.

They see the print block and understand what
is to occur.


These inexperienced people are expected to know that map is
a foreach in disguise?

Why not just use a literal foreach?

foreach ( @myarray )
{print "$_\n"}

or

print "$_\n" foreach @myarray;


Seems that would be even easier to understand...

I've never really studied which is faster, 'print map ...'
or 'map {print ...' or for. I guess I now have something to do tonight
after all.


Don't do anything tonight.

You should be optimizing for labor (comprehension) before speed,
so choose whichever is easier to read and understand without
regard to how many microseconds can be saved.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top