Experts,
I have two questions about "hash declaration".
1) How to declare a constant HASH? Once it's declared, the total numbers keys can not be changed and the values for each key also can not be changed.
One way I can think of is to use Readonly module. However, it is not a core module so that my code is not very portable.
2) See the example below first:
Both %h1 and %h2 are static. In reality, '%h1' can have many many keys. To make the program short, here is what I did:
I wish there could be something like operator overloding in java/c++:
Is there an elegant way to do this?
I have two questions about "hash declaration".
1) How to declare a constant HASH? Once it's declared, the total numbers keys can not be changed and the values for each key also can not be changed.
One way I can think of is to use Readonly module. However, it is not a core module so that my code is not very portable.
2) See the example below first:
Code:
my %h1 = (
'x1', 'y1',
'x2', 'y2',
'x3', 'y3',
...
'xn', 'yn',
);
my %h2 = (
'x1', 'y1',
'x2', 'y2',
'x3', 'y3',
...
'xn', 'yn',
'a1', 'b1',
'a2', 'b2',
'a3', 'b3',
...
'an', 'bn',
);
Code:
my %h2;
foreach (keys(%h1)) {
$h2{$_} = $h1{$_};
}
$h2{'a1'} = 'b1';
$h2{'a2'} = 'b2';
...
$h2{'an'} = 'bn';
Code:
my %tmp = (
'a2', 'b2',
'a3', 'b3',
...
'an', 'bn',
);
my %h2 = %h1 + %tmp;