1

Could anyone tell me how could I print values of array in different rows without using loop?

#!/usr/bin/perl -w

my @a = ('Test1','Test2','Test3');
print "@a";# output like **Test1 Test2 Test3** but i want **Test2 in next line and Test3 next to next line**

Is it Possible?

4 Answers 4

8

You can just do:

print join("\n", @ar);
Sign up to request clarification or add additional context in comments.

Comments

4

You can set $" variable

 $" = "\n";

It's probably better to do

{
     local $" = "\n";
     print "@ar";
}

EDIT:

according to the camel book :

$" (or the alternative $LIST_SEPERATOR) specifies the string to put between individual elements when an array is interpolated into a double-quoted string, this for the case you want to say:

print "@ar";

$, (or the alternative $OUTPUT_FIELD_SEPERATOR) specifies the string to put between individual elements when you want to print a list. It's initially empty. You can set $, for the case you want to say:

 print @ar;

Comments

1

You can set the $, special variable to be whatever you want to separate your list elements. This should do what you want:

$, = "\n";
my @a = ('Test1','Test2','Test3');
print @a;

1 Comment

that would only work with multiple elements passed to print (print @a) instead of a single string.
1

use map function

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.