How to assign array elements to a variable by appending them with new line character in PERL?

Hi,
I have an array and I want to append the elements of this array using one new line character between each element in Unix.

how can I do it in one simple line?

Thanks,
TolgarAsked:
Who is Participating?
 
nemws1Database AdministratorCommented:
If you just want to print it out, use join() instead:
print join("\n", @array), "\n";

Open in new window

0
 
nemws1Database AdministratorCommented:
I'm not fully understanding your question.  Do you want to add a newline to every element?  If so, line 2 of this is what you want:
my(@array)=qw(hello how are you today);
for (@array) { $_ .= "\n"; }
print "@data\n";

Open in new window

0
 
nemws1Database AdministratorCommented:
Line 3 should have @array - not @data (was trying to make my code look nicer and complety missed it)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
TolgarAuthor Commented:
@nemws1:
Can I do this with join?

my $var = join("\n", @array)

Thanks,
0
 
parparovCommented:
Yes you can. The resulting string will NOT have a newline at its end.
0
 
TolgarAuthor Commented:
So, will this be ok with a new line at the end?

my $var = (join("\n", @array))."\n"

Open in new window



Thanks,
0
 
parparovCommented:
yes

don't forget the semicolon at the end.
0
 
TolgarAuthor Commented:
I modified the solution that was given to me and I selected it as well for future reference.
0
 
ozoCommented:
#you could also do
my $var = join"\n", @array,'';
0
 
TolgarAuthor Commented:
I have a quick additional question:

This is my array:

DB<5> x @Files
0  ARRAY(0x15b3530)
   0  "st/ert/variants/variants5.c\cM\cJ"
   1  "st/ert/variants/variants6.c\cM\cJ"

Open in new window



but this code does not work as I expect:

$FileList = (join("\n", @Files))."\n";

Open in new window


It returns:

DB<12> x $FileList
0  'ARRAY(0x15b3530)
'

Open in new window


but I expect to see:

0  "st/ert/variants/variants5.c\cM\cJ"
1  "st/ert/variants/variants6.c\cM\cJ"

Open in new window



How can I do it?

Thanks,
0
 
nemws1Database AdministratorCommented:
Drop the extra parens around the join:
$FileList = join("\n", @Files)."\n";

Open in new window

0
 
TolgarAuthor Commented:
well, it didn't work.

Here is the output:

321:                                            $FileList = join("\n", @Files)."\n";
  DB<2> n
main::process_cache_file(procStat:322):
322:                                            $CommentList = (join("\n", @Comments))."\n";
  DB<2> x $FileList
0  'ARRAY(0x15af1a0)
'
  DB<3>

Open in new window

0
 
nemws1Database AdministratorCommented:
When you do something like this:

$var = (something);

Then it'll return an array reference.  You're doing the some thing with $CommentList, change it to:

$CommentList = join("\n", @Comments)."\n";
0
 
TolgarAuthor Commented:
I know but it works fine with comment list. It is just there because I was debugging. You can ignore $CommentList.

However, I changed $FileList as you suggested and it still does not return the expected output.


Thanks,
0
 
ozoCommented:
It looks like @Files contains a single element which is a reference to an array
which means you would want
join("", @$Files[0])
0
 
ozoCommented:
sorry, I should have said  @{$Files[0]}
0
 
ozoCommented:
But maybe you did not intend @Files to contain a single element which is a reference to an array

How did you create @Files?
0
 
TolgarAuthor Commented:
This worked

Thanks a lot
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.