?
Solved

assign all elements of an array to a variable

Posted on 2011-03-15
9
Medium Priority
?
385 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 26

Accepted Solution

by:
wilcoxon earned 1400 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 600 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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

777 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