in reply to Incrementing a hash of array value
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash=(); %hash=( ##keys between "" because of dots "M01.001" =>["M01.001"], "M01.001.111" =>["M01.001.111"], "M01.001.111.111" =>["M01.001.111.111"], "M01.002" =>["M01.002"], "M01.002.003" =>["M01.002.003"], ); print Dumper(%hash), "\n"; for my $k(sort keys %hash){ my $temp = reverse($k); $temp =~ s/^\d{3}\.?//; my $t= reverse ($temp); if (exists $hash{$t}){ $hash{$k}[1] += 1; } } print Dumper(%hash); __END__
As for the $hash{$k}[1] += 1; "not working", it is working. It might not be doing what you wanted, but it's doing what somebody would expect, which is to create a second element in the array $hash{$k}, and then add 1 to it, to produce the result 1. If you meant to add 1 to the string "M01.001", what exactly do you think that means? You can't add the integer 1 to a string. It doesn't make sense.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Incrementing a hash of array value
by Anonymous Monk on Jul 23, 2004 at 21:28 UTC |