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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

nemws1Database AdministratorCommented:
Line 3 should have @array - not @data (was trying to make my code look nicer and complety missed it)
nemws1Database AdministratorCommented:
If you just want to print it out, use join() instead:
print join("\n", @array), "\n";

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

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

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

Thanks,
parparovCommented:
Yes you can. The resulting string will NOT have a newline at its end.
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,
parparovCommented:
yes

don't forget the semicolon at the end.
TolgarAuthor Commented:
I modified the solution that was given to me and I selected it as well for future reference.
ozoCommented:
#you could also do
my $var = join"\n", @array,'';
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,
nemws1Database AdministratorCommented:
Drop the extra parens around the join:
$FileList = join("\n", @Files)."\n";

Open in new window

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

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";
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,
ozoCommented:
It looks like @Files contains a single element which is a reference to an array
which means you would want
join("", @$Files[0])
ozoCommented:
sorry, I should have said  @{$Files[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?
TolgarAuthor Commented:
This worked

Thanks a lot
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.