Solved

assign all elements of an array to a variable

Posted on 2011-03-15
9
339 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
  • 4
  • 4
9 Comments
 
LVL 26

Expert Comment

by:wilcoxon
Comment Utility
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
Comment Utility
$Files="@fileList";
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
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
 
LVL 26

Accepted Solution

by:
wilcoxon earned 350 total points
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 31

Assisted Solution

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

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

Open in new window

0
 

Author Comment

by:Tolgar
Comment Utility
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
Comment Utility
Make a comma separated list of Array items.
0
 
LVL 26

Expert Comment

by:wilcoxon
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now