fishpen0
fishpen0

Reputation: 618

How to declare array with existing arrays in perl?

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?

Upvotes: 3

Views: 536

Answers (1)

Pavel Vlasov
Pavel Vlasov

Reputation: 3465

  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] };

Upvotes: 8

Related Questions