📜  珀尔 |多维数组

📅  最后修改于: 2022-05-13 01:55:17.242000             🧑  作者: Mango

珀尔 |多维数组

Perl 中的多维数组是具有多于一维的数组。从技术上讲,Perl 中没有多维数组这样的东西,但是数组被用来充当它们具有多个维度的行为。多维数组以行和列的形式表示,也称为矩阵。数组的每个元素都可以是对另一个数组的引用,但在语法上,它们看起来像一个二维数组。

多维数组只能保存标量值,不能保存数组或散列。

多维数组的初始化和声明

下面给出的示例清楚地说明了多维数组的初始化和声明。
在这个例子中,我们将简单地通过@array_name = ([…], […], […]);初始化数组。 .

Perl
#!/usr/bin/perl
use strict;
use warnings;
 
# Initializing and defining the array
my @items = ( ['book', 'pen', 'pencil'],
              ['Bat', 'Ball', 'Stumps'],
              ['snake', 'rat', 'rabbit'] );
 
# Printing items from the array
print $items[0][0], "\n";
print $items[1][1], "\n";
print $items[2][2], "\n";


Perl
#!/usr/bin/perl
use strict;
use warnings;
 
# Array Declaration
my @arrayA = qw(1 0 0); 
my @arrayB = qw(0 1 0); 
my @arrayC = qw(0 0 1); 
 
# Merging 3 arrays into One Final array 
my @result = (\@arrayA, \@arrayB, \@arrayC);
  
print "Resultant 3*3 Matrix:\n"; 
 
# Using For loop
for(my $m = 0; $m <= $#result; $m++)
{  
   for(my $n = 0; $n <= $#result ; $n++)
   { 
      print "$result[$m][$n] "; 
   } 
   print "\n"; 
}


Perl
#!usr/bin/perl
use strict;
use warnings;
 
my (@MatrixA, @MatrixB, @Resultant) = ((), (), ());
 
# Asking for User Input for Matrix A
print "Please Provide the order of MatrixA :\n";
print "\tMatrixA rows:";
chomp(my $rowA = <>);  # CHOMP TO TAKE USER INPUT
print "\tMatrixA columns:";
chomp(my $columnA = <>);
 
# Asking for User Input for Matrix B
print "Please Provide the order of MatrixB :\n";
print "\tMatrixB rows:";
chomp(my $rowB = <>);
print "\tMatrixB columns:";
chomp(my $columnB = <>);
 
# Asking User to input elements of matrices
if ($rowA == $rowB and $columnA == $columnB)
{
    print "Enter $rowA * $columnA elements in MatrixA:\n";
    foreach my $m (0..$rowA - 1)
    {
        foreach my $n (0..$columnA - 1)
        {
            chomp($MatrixA[$m][$n] = <>);  # TO READ THE VALUES
        }
    }
     
    print "Enter $rowB * $columnB elements in MatrixB:\n";
    foreach my $m (0..$rowB - 1)
    {
        foreach my $n (0..$columnB - 1)
        {
            chomp($MatrixB[$m][$n] = <>);  # TO READ THE VALUES
        }
    }
     
    # Performing Addition operation
    foreach my $m (0..$rowB - 1)
    {
        foreach my $n (0..$columnB - 1)
        {
            $Resultant[$m][$n] = $MatrixA[$m][$n] +
                                 $MatrixB[$m][$n];
        }
    }
     
    # Printing Matrix A
    print "MatrixA is :\n";
    foreach my $m (0..$rowB - 1)
    {
        foreach my $n (0..$columnB - 1)
        {
            print "$MatrixA[$m][$n] ";
        }
        print "\n";
    }
     
    # Printing Matrix B
    print "MatrixB is :\n";
    foreach my $m (0..$rowB - 1)
    {
        foreach my $n (0..$columnB - 1)
        {
            print "$MatrixB[$m][$n] ";
        }
        print "\n";
    }
     
    # Printing the sum of Matrices
    print "SUM of MatrixA and MatrixB is :\n";
    foreach my $m (0..$rowB - 1)
    {
        foreach my $n (0..$columnB - 1)
        {
            print "$Resultant[$m][$n] ";
        }
        print "\n";
    }
}
 
# Error if Matrices are of different order
else
{
    print "Matrices order does not MATCH, Addition is not Possible";
}


Perl
#!/usr/bin/perl
use strict;
use warnings;
 
# Array of Arrays
my @GfG = ( ["Geek",  "For",  "Geeks"],
            ["Is",    "Best", "For"],
            ["Those", "In",  "Need"] );
 
# Printing internal array elements
print "Accessing Array elements:\n";
print  @{$GfG[0]}[1], "\n"; # For accessing particular array element
print  @{$GfG[1]}[0], "\n";
print  @{$GfG[2]}[2], "\n";
 
# Printing whole Arrays
print "\nAccessing whole arrays: \n";
print @{$GfG[0]}, "\n"; # For accessing particular array
print @{$GfG[1]}, "\n";
print @{$GfG[2]}, "\n";
print "\n";


Perl
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
 
my @GfG = ( ["SAM",  "SHABANAM",  "SOHAM"],
            ["DHONI", "GONI", "AVNI"],
            ["VIRAT", "STUART", "ROHIT"] );
my @test;
foreach (@GfG)
{
    if ($_ =~ /M/ )
    {
        push ( @{$test[0]}, $_);
    }
    elsif ($_ =~ /I/ )
    {
        push ( @{$test[1]}, $_);
    }
    else
    {
        push ( @{$test[2]}, $_);
    }
}
 
print Dumper(\@test), "\n";


Perl
#!/usr/bin/perl
use strict;
use warnings;
 
my @hashtest = ({ First => "Geek",
                  Middle => "For",
                  Last => "Geeks" },
                { Street => "Royal Kapsons",
                  City => "Noida-Uttar Pradesh" },
                { About => "Computer Science portal",
                  Content => "Technical" });
 
# To access any of the hash reference.
print $hashtest[0], "\n";
 
# To access keys of any hash reference.
print keys %{$hashtest[2]}, "\n";
 
# To access any value of any key.
print $hashtest[0]->{"First"}, "\n";
print $hashtest[0]->{"Middle"}, "\n";
print $hashtest[0]->{"Last"}, "\n";


Perl
#!/usr/bin/perl
use strict;
use warnings;
 
use Data::Dumper;
 
my @array = ("Item - Java Course",
             "Cost - 5000",
             "Ratings - 5 star");
 
# Empty array of hashes
my @array_1;
 
foreach (@array)
{
    my @array = split(" - ", $_);
    $array_1[0]->{$array[0]} = $array[1];
}
 
print Dumper (\@array_1);


输出:

创建矩阵

矩阵是看起来不止一维的数组行和列的集合。
让我们看一个示例,以更清楚地说明在 Perl 中创建矩阵。

示例 1:
在这个例子中,我们首先声明了三个带有值的数组,然后将它们合并成一个最终的结果数组,形成一个3*3 矩阵。为了控制变量$m 和 $n ,使用了两个 for 循环。

Perl

#!/usr/bin/perl
use strict;
use warnings;
 
# Array Declaration
my @arrayA = qw(1 0 0); 
my @arrayB = qw(0 1 0); 
my @arrayC = qw(0 0 1); 
 
# Merging 3 arrays into One Final array 
my @result = (\@arrayA, \@arrayB, \@arrayC);
  
print "Resultant 3*3 Matrix:\n"; 
 
# Using For loop
for(my $m = 0; $m <= $#result; $m++)
{  
   for(my $n = 0; $n <= $#result ; $n++)
   { 
      print "$result[$m][$n] "; 
   } 
   print "\n"; 
} 

输出:

示例 2:
在此示例中,我们将从键盘获取输入,然后将两个矩阵相加并使用结果矩阵打印结果。

Perl

#!usr/bin/perl
use strict;
use warnings;
 
my (@MatrixA, @MatrixB, @Resultant) = ((), (), ());
 
# Asking for User Input for Matrix A
print "Please Provide the order of MatrixA :\n";
print "\tMatrixA rows:";
chomp(my $rowA = <>);  # CHOMP TO TAKE USER INPUT
print "\tMatrixA columns:";
chomp(my $columnA = <>);
 
# Asking for User Input for Matrix B
print "Please Provide the order of MatrixB :\n";
print "\tMatrixB rows:";
chomp(my $rowB = <>);
print "\tMatrixB columns:";
chomp(my $columnB = <>);
 
# Asking User to input elements of matrices
if ($rowA == $rowB and $columnA == $columnB)
{
    print "Enter $rowA * $columnA elements in MatrixA:\n";
    foreach my $m (0..$rowA - 1)
    {
        foreach my $n (0..$columnA - 1)
        {
            chomp($MatrixA[$m][$n] = <>);  # TO READ THE VALUES
        }
    }
     
    print "Enter $rowB * $columnB elements in MatrixB:\n";
    foreach my $m (0..$rowB - 1)
    {
        foreach my $n (0..$columnB - 1)
        {
            chomp($MatrixB[$m][$n] = <>);  # TO READ THE VALUES
        }
    }
     
    # Performing Addition operation
    foreach my $m (0..$rowB - 1)
    {
        foreach my $n (0..$columnB - 1)
        {
            $Resultant[$m][$n] = $MatrixA[$m][$n] +
                                 $MatrixB[$m][$n];
        }
    }
     
    # Printing Matrix A
    print "MatrixA is :\n";
    foreach my $m (0..$rowB - 1)
    {
        foreach my $n (0..$columnB - 1)
        {
            print "$MatrixA[$m][$n] ";
        }
        print "\n";
    }
     
    # Printing Matrix B
    print "MatrixB is :\n";
    foreach my $m (0..$rowB - 1)
    {
        foreach my $n (0..$columnB - 1)
        {
            print "$MatrixB[$m][$n] ";
        }
        print "\n";
    }
     
    # Printing the sum of Matrices
    print "SUM of MatrixA and MatrixB is :\n";
    foreach my $m (0..$rowB - 1)
    {
        foreach my $n (0..$columnB - 1)
        {
            print "$Resultant[$m][$n] ";
        }
        print "\n";
    }
}
 
# Error if Matrices are of different order
else
{
    print "Matrices order does not MATCH, Addition is not Possible";
}

输出:

仅当矩阵具有相同顺序时,才能执行矩阵相加。

数组数组

数组数组是具有数组引用列表的数据结构。数组中的元素是数组引用。这些参考可以单独打印,也可以根据需要打印整个阵列。

句法:

@ARRAY_NAME = ([value1], [value2], [value3], ..., [valueN]);

示例 1:

Perl

#!/usr/bin/perl
use strict;
use warnings;
 
# Array of Arrays
my @GfG = ( ["Geek",  "For",  "Geeks"],
            ["Is",    "Best", "For"],
            ["Those", "In",  "Need"] );
 
# Printing internal array elements
print "Accessing Array elements:\n";
print  @{$GfG[0]}[1], "\n"; # For accessing particular array element
print  @{$GfG[1]}[0], "\n";
print  @{$GfG[2]}[2], "\n";
 
# Printing whole Arrays
print "\nAccessing whole arrays: \n";
print @{$GfG[0]}, "\n"; # For accessing particular array
print @{$GfG[1]}, "\n";
print @{$GfG[2]}, "\n";
print "\n";

输出:

示例 2:

在此示例中,我们使用单个数组生成了一个数组数组。

Perl

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
 
my @GfG = ( ["SAM",  "SHABANAM",  "SOHAM"],
            ["DHONI", "GONI", "AVNI"],
            ["VIRAT", "STUART", "ROHIT"] );
my @test;
foreach (@GfG)
{
    if ($_ =~ /M/ )
    {
        push ( @{$test[0]}, $_);
    }
    elsif ($_ =~ /I/ )
    {
        push ( @{$test[1]}, $_);
    }
    else
    {
        push ( @{$test[2]}, $_);
    }
}
 
print Dumper(\@test), "\n";

输出:

哈希数组

哈希数组是数组具有哈希列表的数据结构。哈希引用是数组的对象。为了访问键和值,我们需要取消引用它们。
如果我们想以数字方式遍历散列,散列数组是一个很好的数据结构。

句法:

@ARRAY_NAME = ({KEY1 => VALUE1}, {KEY2 => VALUE2});

示例 1:

Perl

#!/usr/bin/perl
use strict;
use warnings;
 
my @hashtest = ({ First => "Geek",
                  Middle => "For",
                  Last => "Geeks" },
                { Street => "Royal Kapsons",
                  City => "Noida-Uttar Pradesh" },
                { About => "Computer Science portal",
                  Content => "Technical" });
 
# To access any of the hash reference.
print $hashtest[0], "\n";
 
# To access keys of any hash reference.
print keys %{$hashtest[2]}, "\n";
 
# To access any value of any key.
print $hashtest[0]->{"First"}, "\n";
print $hashtest[0]->{"Middle"}, "\n";
print $hashtest[0]->{"Last"}, "\n";

输出:

示例 2:
从一个简单的数组生成一个哈希数组。

Perl

#!/usr/bin/perl
use strict;
use warnings;
 
use Data::Dumper;
 
my @array = ("Item - Java Course",
             "Cost - 5000",
             "Ratings - 5 star");
 
# Empty array of hashes
my @array_1;
 
foreach (@array)
{
    my @array = split(" - ", $_);
    $array_1[0]->{$array[0]} = $array[1];
}
 
print Dumper (\@array_1);

输出: