Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two questions about hash declaration

Status
Not open for further replies.

whn

Programmer
Oct 14, 2007
265
0
0
US
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:
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',
);
Both %h1 and %h2 are static. In reality, '%h1' can have many many keys. To make the program short, here is what I did:
Code:
my %h2;
foreach (keys(%h1)) {
  $h2{$_} = $h1{$_};
}
$h2{'a1'} = 'b1';
$h2{'a2'} = 'b2';
...
$h2{'an'} = 'bn';
I wish there could be something like operator overloding in java/c++:
Code:
my %tmp = (
  'a2', 'b2',
  'a3', 'b3',
  ...
  'an', 'bn',
);

my %h2 = %h1 + %tmp;
Is there an elegant way to do this?
 
my %h2 = (%h1,%tmp);

but any duplicate hash keys will be lost doing that.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
1) Can't see a way to build a constant hash, nor can I see the use of it.
In fact you can create a reference to a hash where the reference is a constant, but you can still change the underlying hash via its constant reference, and I assume this is not what you want.
On the other side a constant hash is nothing less and nothing more than a collection of constants with their corresponding (single, not list) values: that's why I don't see the use for it, unless you can explain better your goal.
A collection of constants is created with
Code:
use constant {
  X1 => 'y1',
  X2 => 'y2',
  ..
  XN => 'yn',
};
Of course in this way you can't use the [tt]each, values, keys, exists[/tt] functions, all depends on what you need to do.
If you only need to be sure that no change will be done on the constant hash, you should write a module where the hash is defined (with [tt]my[/tt] of course), that contains also all the functions required to return a value from a key, check whether a key exists, list the key values, and so on.
2)Kevin has the answer on how to fill a hash with another one. However once again I can't see the use of it, especially if both are used as static (constant) key-value pair lists.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Thank you, Kevin and Franco.

Currently, I implemented my hash exactly in the way you suggested -- declare it as a private hash in a module and another subroutine returns it.

Now, I have another question, when a subroutine returns a hash (only one hash), should the subroutine returne it by value or by reference? I always return by reference. Butin this case, I guess I'd better return it by value. Am I right?

With regarding to why I want to use a constant hash, my company's software support certain linux platforms. The combination of flavor/release/processor/kernel is huge. no one can remember all of them. And worse, this matrix is written in a PDF file and managed by human. I am writing a perl tool to manage it. The first step is to declare a multi-dimension hash to map this matrix. I was thinking to use a constant hash. Since I could not figure it out how, I put it in a module just as the way as you suggested.

Again, thank you both.
 
Well, it all depends on what you want to do.
Generally speaking, one would prefer to use a reference, as passing a hash by value may slow down your code.
However, knowing that reference, your code could change that hash, even if it declared and populated in a separate module.
So, if your main goal is to make that hash unmodifiable, you should return everything by value.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
I could have sworn there is a module on CPAN that "locks" a hash but maybe I am wrong.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
To Kevin,

Yes, CPAN has a non-core module called 'Readonly.pm'. Because my tool is meant to be used in any newly configured servers, non-core modules are not portable. Now, I'm thinking to put those non-core ones in a local dir and tar them together with my tool so that they can be portable.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top