1

I am trying to create a hash of array. Currently the array is of size 1 only.

$f1="/scratch/aime1/PerlCode/BeforeUpgrade.csv";
$f2="/scratch/aime1/PerlCode/AfterUpgrade.csv";
open(FIN, "< $f1");
while( $my_line = <FIN> )
{
    chomp $my_line;
    my @values = split(',', $my_line);
    my $key = shift @values;
    print "$key\n";     
    print "@values\n"; 
    $hash1{$key} = @values;

}
close(FIN);


for (keys %hash1) {
     my @value_array = $hash1{$_};
     print "Key is $_ and first Element of array is $value_array[0] \n";
}

So,the key is of the form /scratch/aime1/idmUpgrade/idmUpgrade and the value is its permission i.e. 0755

When I try to print this hash,output is:

Key is /scratch/aime1/idmUpgrade/idmUpgrade and first Element of array is 1

Array is always printed as 1 and not as 0755.

1 Answer 1

2

Always include use strict; and use warnings; at the top of EVERY perl script.

You're assigning an array to a hash key, to do that you need to take a reference. Otherwise, you're just assigning the array count (which is 1)

$hash1{$key} = \@values;

Similarly, when you want to retrieve, it, you'll need to dereference it:

my @value_array = @{$hash1{$_}};
Sign up to request clarification or add additional context in comments.

Comments

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.