hello
im in trouble when adding identical elements into a hash like in the
script below.
i want to print:
2 kr
3 %
2 kr
4 kr
but fail on the third line which is identical to the first.
i have tried with the exists function but i reckon the problem must be
in the foreach loop thru the hash elements (which is one element too
short and therefore will fail subsequently).
are there any hash-workarounds for this - (im able to solve the script
task otherwise, but would really like to use a hash due to performance).
thanks
allan
#!perl
my $input = "<dcv id=1 more vartype=1>\n<dcv id=2 more vartype=2>\n<dcv
id=1 more vartype=1>\n<dcv id=3 more vartype=1>\n";
my %xmlHash;
my $aspVariable = perlMainLoop($input);
print $aspVariable;
sub perlMainLoop {
my $output = $_[0];
perlGetXml($output);
foreach my $key (keys %xmlHash) {
my @temp = split(/::/, $xmlHash{$key});
my $var = $temp[1] == 1 ? " kr" : " %";
my $repValue = calculate($temp[0]);
$output =~s/$key/$repValue$var/;
}
return $output;
}
sub calculate {
$_ = $_[0];
my $use = ($_ + 4) -3;
return $use
}
sub perlGetXml {
$_ = $_[0];
my $temp;
while (/(<dcv[^>]+id=(\d+)[^>]+vartype=(\d+)>)/ig) {
$temp = $1;
$xmlHash{$temp} = "$2::$3";
}
return %xmlHash;
}