Further to toolic's reply:
if(exists($final->{$x[$i]})){
print "second\n";
($final->{$x[$i]}, $f);
}
else{
print "first\n";
$final->{$x[$i]} = ($final->{$x[$i]}, $f);
}
The statement
($final->{$x[$i]}, $f);
does nothing. What do you expect it to do?
The statement
$final->{$x[$i]} = ($final->{$x[$i]}, $f);
is a bit more complex. It evaluates the list ($final->{$x[$i]}, $f) in the scalar context imposed by the scalar $final->{$x[$i]} on the LHS of the assignment. I doubt this statement does what you expect. Please see the Context tutorial, in particular the "Context clash" section.
c:\@Work\Perl>perl -wMstrict -MData::Dumper -le
"my $final = { qw(a 1 b 2 c 3), d => [ 9, 8, 7 ] };
my @x = qw(a b c);
print 'data to start';
print Dumper $final;
print Dumper \@x;
;;
my $f = 'ZOT';
;;
print 'void-context expression';
($final->{$x[1]}, $f);
print Dumper $final;
;;
print 'assign list in scalar context';
$final->{$x[1]} = ($final->{$x[1]}, $f);
print Dumper $final;
"
Useless use of hash element in void context at -e line 1.
Useless use of hash element in void context at -e line 1.
Useless use of private variable in void context at -e line 1.
data to start
$VAR1 = {
'c' => '3',
'a' => '1',
'b' => '2',
'd' => [
9,
8,
7
]
};
$VAR1 = [
'a',
'b',
'c'
];
void-context expression
$VAR1 = {
'c' => '3',
'a' => '1',
'b' => '2',
'd' => [
9,
8,
7
]
};
assign list in scalar context
$VAR1 = {
'c' => '3',
'a' => '1',
'b' => 'ZOT',
'd' => [
9,
8,
7
]
};
Please pay attention to the warning messages generated by
warnings. (The "
Useless use of..." messages will have useful line numbers associated with them if you run your code from a script rather than from the command line as I have.)
I think there may be other problems with the script, but this should get you going. Perhaps also see Perl Data Structures Cookbook.
Give a man a fish: <%-(-(-(-<