Solved

assign all elements of an array to a variable

Posted on 2011-03-15
9
361 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
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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl match and sort unique result 2 136
Move Function in Perl Script 2 68
how to print sub line with  sed /awk /perl in bash file 11 24
perl syntax 3 4
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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

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