3

So say I created a set of arrays like so:

my (@device, @mount, @type, @options, @dump, @pass) = ();

Then later I wanted to create an array with those arrays inside it. How would I do that? I tried to use:

my @columns = (@device, @mount, @type, @options, @dump, @pass);

and

my @columns = ([@device], [@mount], [@type], [@options], [@dump], [@pass]);

The issue seems to be that @columns remains empty. I feel like I am making a really simple syntax mistake. What have I done wrong?

1
  • Your second attempt does exactly what I thought you wanted: it produces an array that holds a copy of each of your original arrays. Please explain how that solution falls short of your produces so that we can help you further Commented Sep 22, 2012 at 21:21

1 Answer 1

8
  1. When you use, you just assign all values from given arrays to array @columns.

    my @columns = (@device, @mount, @type, @options, @dump, @pass);
    
  2. Here, you just say all array by reference, if you use Data::Dumper you can see structure of your array @columns.

    use Data::Dumper;
    
    my @columns = ([@device], [@mount], [@type], [@options], [@dump], [@pass]);
    print Dumper(\@columns);
    

You can do it using array references. Read this tutorial: perlreftut - Mark's very short tutorial about references

my @columns = (\@device, \@mount, \@type, \@options, \@dump, \@pass);

Then use dereferencing:

my @new_device = @{ $columns[0] };
Sign up to request clarification or add additional context in comments.

1 Comment

use 1. not 1) to denote a list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.