Solved

assign all elements of an array to a variable

Posted on 2011-03-15
9
377 Views
Last Modified: 2012-05-11
Hi,
Is there a way to assign all elements of an array to a variable WITHOUT using a loop and  then join them using a character like ","?

Something like:

my $Files = join("," flushAll(@fileList));

Open in new window


Thanks,

0
Comment
Question by:Tolgar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35142353
I'm not sure what you're asking.  Provided flashAll returns a list, the code you have should (almost) do exactly what you seem to be asking.

You are missing a comma:

my $Files = join(",", flushAll(@fileList));

What is @fileList and what does flushAll do?

If you mean can you take the contents of files and assign it to a variable, then you can use File::Slurp.

my $Files = join(',', map { slurp($_) } @fileList);
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35142382
$Files="@fileList";
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35142412
If you want to convert into a string with list items separated by spaces, use this.
$Files="@fileList";

Open in new window


If you want some other separator, you may set the special variable $" to that value.

 
#Example
$" = "\t";

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!

 
LVL 26

Accepted Solution

by:
wilcoxon earned 350 total points
ID: 35142416
If you do mean just assign all values of an array to a variable, farzanj's response will do that (separated by spaces).

If that is what you're asking and you want commas, then this will work:

my $Files = join(',', @fileList);
0
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 150 total points
ID: 35142449
You can still create a comma separated list by doing my trick again

$" = ',';
$Files = "@fileList";

Open in new window

0
 

Author Comment

by:Tolgar
ID: 35142612
Hi,
Thank you for all your replies. I am little puzzled so I want to make clear what I have understood:

This one:
my $Files = join(',', @fileList);

Open in new window


is equivalent to
$" = ',';
$Files = "@fileList";

Open in new window


And they both get the elements of the array and concatenate them by using "," and assign it to a variable.

Am I right?


Thanks,
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35142625
Make a comma separated list of Array items.
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35142638
True.  However, when I wrote my response your second post wasn't up yet.  Also, I prefer the simplicity of using join instead (single line, no special var to remember, and (should be) identical performance).
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35142680
Yes, that is correct.  Given:

@fileList = (qw(1 abc 23 14));

Both of those will result in

$Files = "1,abc,23,14";
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
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…

740 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