I am trying to create hash of arrays. I am taking data from a txt file and converting this into hash of arrays.
Txt file data is as below
group1 : usr1 usr4 usr6
group2 : usr2 usr1 usr5
group3 : usr1 usr2 usr3
so on ......
I am converting this hash of arrays like
%hash = (group1 => [usr1 usr4 usr6], group2 => [usr2 usr1 usr5]);
Following code i am trying
%hash = ();
open (FH, "2.txt") or die "file not found";
while (<FH>) {
@array = split (":", $_);
$array[1] =~ s/^\s*//;
$array[1] =~ s/\s*$//;
@arrayRef = split (" ", $array[1]);
$hash{$array[0]} = [ @arrayRef ];
#print @array;
#print "\n";
}
close FH;
print $hash{group1}[0];
print @{ $hash{group2}};
I am not getting output. There is something wrong in the code. Please help me understanding it better
split ""will split on the empty string, which meansusr1will be split up intoqw(u s r 1).