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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hash question

Status
Not open for further replies.

PhoenixDown

Programmer
Joined
Jul 2, 2001
Messages
279
Location
CA
Can I make a hash like this:

%hash = (
'test' => 'test' => 'test' => 'test,
);
1;

If that's not currect, please show me the correct way to do this. Thanks. :-) - Go there!
 
A 'hash' is an associative array..... or, a collection
of key/value pairs. The pairs are separated by commas.
Each key must be unique.
Each key points (=>) to it's value.

%hash = ('key1'=>'value1','key2'=>'value2');
|___________| |____________|
first pair second pair

value1 is associated with key1
or, key1 points to value1

Sometimes, you might see another notation where that uses
just commas and not the '=>'.

%hash = ( 'key1','value1','key2','value2');
|__________| |_________|
first pair second pair

You appear to be using the same 'key' twice. Each key must be unique.
and
You need a comma between your key=>value pairs.
There are more examples in thread219-102565.

HTH



keep the rudder amid ship and beware the odd typo
 
It's common when creating a hash to put each key/value pair on a separate line too. It makes it much easier to read and maintain. It's also allowable to have a comma after the last key/value pair. I usually do that so I don't forget to add it if I add a new key/value pair after the last. It's exactly the same syntax as goBoating has, just formatted more clearly. Here's what the common format looks like:
Code:
%hash = (
   'key1' => 'value1',
   'key2' => 'value2',
);
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top