?
Solved

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

Posted on 2011-10-05
18
Medium Priority
?
309 Views
Last Modified: 2012-05-12
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,
0
Comment
Question by:Tolgar
  • 7
  • 5
  • 4
  • +1
18 Comments
 
LVL 23

Expert Comment

by:nemws1
ID: 36922090
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
 
LVL 23

Expert Comment

by:nemws1
ID: 36922092
Line 3 should have @array - not @data (was trying to make my code look nicer and complety missed it)
0
 
LVL 23

Accepted Solution

by:
nemws1 earned 2000 total points
ID: 36922097
If you just want to print it out, use join() instead:
print join("\n", @array), "\n";

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Tolgar
ID: 36922125
@nemws1:
Can I do this with join?

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

Thanks,
0
 
LVL 9

Expert Comment

by:parparov
ID: 36922132
Yes you can. The resulting string will NOT have a newline at its end.
0
 

Assisted Solution

by:Tolgar
Tolgar earned 0 total points
ID: 36922141
So, will this be ok with a new line at the end?

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

Open in new window



Thanks,
0
 
LVL 9

Expert Comment

by:parparov
ID: 36922213
yes

don't forget the semicolon at the end.
0
 

Author Closing Comment

by:Tolgar
ID: 36941313
I modified the solution that was given to me and I selected it as well for future reference.
0
 
LVL 85

Expert Comment

by:ozo
ID: 36922921
#you could also do
my $var = join"\n", @array,'';
0
 

Author Comment

by:Tolgar
ID: 36924971
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
 
LVL 23

Expert Comment

by:nemws1
ID: 36925185
Drop the extra parens around the join:
$FileList = join("\n", @Files)."\n";

Open in new window

0
 

Author Comment

by:Tolgar
ID: 36925214
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
 
LVL 23

Expert Comment

by:nemws1
ID: 36925230
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
 

Author Comment

by:Tolgar
ID: 36925268
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
 
LVL 85

Expert Comment

by:ozo
ID: 36927503
It looks like @Files contains a single element which is a reference to an array
which means you would want
join("", @$Files[0])
0
 
LVL 85

Expert Comment

by:ozo
ID: 36927522
sorry, I should have said  @{$Files[0]}
0
 
LVL 85

Expert Comment

by:ozo
ID: 36927535
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
 

Author Comment

by:Tolgar
ID: 36927611
This worked

Thanks a lot
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question