简体   繁体   中英

How can I create a variable array name in Perl?

Array @p is a multiline array, eg $p[1] is the second line.

This code will explain what I want:

$size=@p;   # line number of array @p
for($i=0; $i<$size; $i++)
{ 
  @p{$i}= split(/ +/,$p[$i]);
}

I want the result should be like this:

@p0 = $p[0]          first line of array @p goes to array @p0;       
@p1 = $p[1]          second line of array @p goes to array @p1; 
...
...

and so on.

But above code does not work, how can I do it?

It is a bad idea to dynamically generate variable names.

I suggest the best solution here is to convert each line in your @p array to an array of fields.

Lets suppose you have a better name for @p , say @lines . Then you can write

my @lines = map [ split ], <$fh>;

to read in all the lines from the file handle $fh and split them on whitespace. The first field of the first line is then $lines[0][0] . The third field of the first line is $lines[0][2] etc.

First, the syntax @p{$i} accesses the entry with the key $i in a hash %p , and returns it in list context. I don't think you meant that. use strict; use warnings; to get warned about undeclared variables.

You can declare variables with my , eg my @p; or my $size = @p ;

Creating variable names on the fly is possible, but a bad practice. The good thing is that we don't need to: Perl has references . A reference to an array allows us to nest arrays, eg

my @AoA = (
   [1, 2, 3],
   ["a", "b"],
);
say $AoA[0][1]; # 2
say $AoA[1][0]; # a

We can create an array reference by using brackets, eg [ @array ] , or via the reference operator \\ :

my @inner_array = (1 .. 3);
my @other_inner = ("a", "b");
my @AoA = (\@inner_array, \@other_array);

But careful: the array references still point to the same array as the original names, thus

push @other_inner, "c";

also updates the entry in @AoA :

say $AoA[1][2]; # c

Translated to your problem this means that you want:

my @pn;
for (@p) { 
  push @pn, [ split /[ ]+/ ];
}

There are many other ways to express this, eg

my @pn = map [ split /[ ]+/ ], @p;

or

my @pn;
for my $i ( 0 .. $#p ) {
  $pn[$i] = [ split /[ ]+/, $p[$i] ];
}

To learn more about references, read

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM