Hi all,

I am trying to make a perl code which would read a matrix (basically an
array which contains references to other arrays) and initialize a hash with
the first value of each dereferenced array acting as a key and remaining
elements as values using references.

The source code and output are mentioned below :-

Source Code
-----------------

[root]# cat k1.pl
#! /usr/bin/perl

use strict;
use warnings;

my @arr =       (
                        [1, 2, 3, "a", "b"],
                        [4, 5, 6, "c", "d"],
                        [7, 8, 9, "e", "f"]
                );
my @arr1;
my %h;
my $a;
my $key1;
my $i;
my $n;

print "Matrix is as follows :- \n";
foreach $a (@arr)
{
        print "Row = @{$a}\n";

        @arr1 = "";
        $n = @{$a};

        for ($i=1; $i<=$n-1;$i++)
        {
                $arr1[$i-1] = ${$a}[$i];
        }

        $h{ ${$a}[0] } = [EMAIL PROTECTED];

}

print "Hash is as follows :- \n";
foreach $key1 (keys %h)
{
        print "Key -> [$key1] : Value -> [EMAIL PROTECTED]";
}

print "\n";
[root]#


Actual Output
-------------------

[root]# perl k1.pl
Matrix is as follows :-
Row = 1 2 3 a b
Row = 4 5 6 c d
Row = 7 8 9 e f
Hash is as follows :-
Key -> [4] : Value -> [8 9 e f]
Key -> [1] : Value -> [8 9 e f]
Key -> [7] : Value -> [8 9 e f]

[root]#



Expected Output
-------------------

[root]# perl k1.pl
Matrix is as follows :-
Row = 1 2 3 a b
Row = 4 5 6 c d
Row = 7 8 9 e f
Hash is as follows :-
Key -> [4] : Value -> [5 6 c d]
Key -> [1] : Value -> [2 3 a b]
Key -> [7] : Value -> [8 9 e f]

[root]#




Thanks & Regards,
Amit Saxena

Reply via email to