RE: adding hash reference into hash

2003-03-03 Thread Hanson, Rob
Your syntax is a little off...

$intHash1Ref = {};
$intHash2Ref = {};

# note use of parens, not curly braces
%containerHash = (hash1 = $intHash1Ref, hash2 = $intHash2Ref);

The parens store a list into %containerHash, the curly-braces were storing a
hash-ref.

Rob

-Original Message-
From: Yannick Warnier [mailto:[EMAIL PROTECTED]
Sent: Monday, March 03, 2003 11:40 AM
To: [EMAIL PROTECTED]
Subject: adding hash reference into hash


Hi,

I'm just trying to make a structure with a hash containing some
references to other (yet unused) hashes.

So what I wrote this:

  $intHash1Ref = {};
  $intHash2Ref = {};

  %containerHash = { hash1 = $intHash1Ref, hash2 = $intHash2Ref};

Then when I try to have a list of keys to that containerHash:

  print keys(%containerHash);

I get some hexadecimal values like:

  HASH(0x813f9b0)

How can I manage to do that cleanly? 
I'm searching in Programming Perl 3th Ed. for that but I don't get
it... yet.

Yannick

_
Envie de discuter en live avec vos amis ? Télécharger MSN Messenger
http://www.ifrance.com/_reloc/m la 1ère messagerie instantanée de France


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: adding hash reference into hash

2003-03-03 Thread Robin Norwood
Yannick Warnier [EMAIL PROTECTED] writes:

 Hi,
 
 I'm just trying to make a structure with a hash containing some
 references to other (yet unused) hashes.
 
 So what I wrote this:
 
   $intHash1Ref = {};
   $intHash2Ref = {};
 
   %containerHash = { hash1 = $intHash1Ref, hash2 = $intHash2Ref};
 
 Then when I try to have a list of keys to that containerHash:
 
   print keys(%containerHash);
 
 I get some hexadecimal values like:
 
   HASH(0x813f9b0)
 
 How can I manage to do that cleanly? 
 I'm searching in Programming Perl 3th Ed. for that but I don't get
 it... yet.

A couple of problems here:

First, always use 'use strict' and 'use warnings'.

Second, use 'my $intHash1Ref', etc.

Third, the curly braces '{ }' are for hash references (in this
context), not a hash, so:

   %containerHash = { hash1 = $intHash1Ref, hash2 = $intHash2Ref};

should be:

my %containerHash = ( hash1 = $intHash1Ref, hash2 = $intHash2Ref );

The above changes clean things up enough so that we can look at your
actual question.  The output of the script now is:

'hash1hash2'

Which are the keys of %containerHash, instead of HASH(0xblahblah).

To address your actual question, what you were printing was the hash
*reference* in $containerHash - the string representation of which
isn't very useful to you.  If you just want to look at the hash for
debugging purposes, use Data::Dumper:

use Data::Dumper;

[ your code ...]

print Data::Dumper-Dump([(%containerHash)]);

Which, with the changes I mentioned above, gives you:

$VAR1 = 'hash1';
$VAR2 = {};
$VAR3 = 'hash2';
$VAR4 = {};

Data::Dumper would have expanded $VAR2 and $VAR4 if the hashes
referenced by $intHash1Ref and $intHash2Ref had anything in them.
Data::Dumper isn't really user-friendly enough for actual output
though.  For that, you need to access the hashes directly:

#First, loop over the keys of %containerHash:
foreach my $hkey (keys %containerHash) { # 'hash1', then 'hash2'

#Now, loop over the keys in each of the hashes stored in %containerHash:
  foreach my $intkey (keys %{$containerHash{$hkey}}) {

# $containerHash{$hkey} gives us the hash reference which is the value
# of $containerHash for the given key.  Also known as $intHash1Ref and
# $intHash2Ref.  We dereference that hashref with '%{ }', and loop over
# the keys.

my $value = $containerHash{$hkey}-{$intkey};

# This time we're dereferencing the hashrefs in $containerHash with
# '-{$intkey}' and getting the value stored in $intHash1Ref (or 2)
# for that key.

print Hash '$hkey': key = '$intkey', value = '$value';
  }
}

Oh, and we usually don't capitalize variables in perl.  See: perldoc
perlstyle for recommendations.

Example code:


#!/usr/bin/perl -l

use strict;
use warnings;

use Data::Dumper;

my $intHash1Ref = { one = 1, two = 2, three = 3 };
my $intHash2Ref = { ten = 10, eleven = 11, twelve = 12 };

my %containerHash = ( hash1 = $intHash1Ref, hash2 = $intHash2Ref );

print Data::Dumper-Dump([(%containerHash)]);

foreach my $hkey (keys %containerHash) { # 'hash1', then 'hash2'
  foreach my $intkey (keys %{$containerHash{$hkey}}) { # the keys of $intHash1Ref and 
$intHash2Ref
my $value = $containerHash{$hkey}-{$intkey};
print $hkey: key = '$intkey', value = '$value';
  }
}


-RN

-- 

Robin Norwood
Red Hat, Inc.

The Sage does nothing, yet nothing remains undone.
-Lao Tzu, Te Tao Ching

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: adding hash reference into hash

2003-03-03 Thread Shishir K. Singh
You may want to handle them as 



$intHash1Ref = {'A' = 'B'};
$intHash2Ref = {'C' = 'D'};

%containerHash = ('hash1' = $intHash1Ref, 'hash2' = $intHash2Ref);

foreach my $hashRefKeys (keys %containerHash) {
   foreach my $hashKeys (keys %{$containerHash{$hashRefKeys}} ) {
  print $hashKeys,, $hashRef-{$hashKeys},\n;
}
}




Output is 

C   D
A   B

-Original Message-
From: Hanson, Rob [mailto:[EMAIL PROTECTED]
Sent: Monday, March 03, 2003 12:14 PM
To: 'Yannick Warnier'; [EMAIL PROTECTED]
Subject: RE: adding hash reference into hash


Your syntax is a little off...

$intHash1Ref = {};
$intHash2Ref = {};

# note use of parens, not curly braces
%containerHash = (hash1 = $intHash1Ref, hash2 = $intHash2Ref);

The parens store a list into %containerHash, the curly-braces were storing a
hash-ref.

Rob

-Original Message-
From: Yannick Warnier [mailto:[EMAIL PROTECTED]
Sent: Monday, March 03, 2003 11:40 AM
To: [EMAIL PROTECTED]
Subject: adding hash reference into hash


Hi,

I'm just trying to make a structure with a hash containing some
references to other (yet unused) hashes.

So what I wrote this:

  $intHash1Ref = {};
  $intHash2Ref = {};

  %containerHash = { hash1 = $intHash1Ref, hash2 = $intHash2Ref};

Then when I try to have a list of keys to that containerHash:

  print keys(%containerHash);

I get some hexadecimal values like:

  HASH(0x813f9b0)

How can I manage to do that cleanly? 
I'm searching in Programming Perl 3th Ed. for that but I don't get
it... yet.

Yannick

_
Envie de discuter en live avec vos amis ? Télécharger MSN Messenger
http://www.ifrance.com/_reloc/m la 1ère messagerie instantanée de France


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: adding hash reference into hash

2003-03-03 Thread Shishir K. Singh
oops..mistake!!

$intHash1Ref = {'A' = 'B'};
$intHash2Ref = {'C' = 'D'};

%containerHash = ('hash1' = $intHash1Ref, 'hash2' = $intHash2Ref);

foreach my $hashRefKeys (keys %containerHash) {

my $hashRef = $containerHash{$hashRefKeys};
foreach my $hashKeys (keys %{$hashRef} ) {

  print $hashKeys,, $hashRef-{$hashKeys},\n;
}


}




-Original Message-
From: Shishir K. Singh 
Sent: Monday, March 03, 2003 12:42 PM
To: 'Hanson, Rob'; 'Yannick Warnier'; [EMAIL PROTECTED]
Subject: RE: adding hash reference into hash


You may want to handle them as 



$intHash1Ref = {'A' = 'B'};
$intHash2Ref = {'C' = 'D'};

%containerHash = ('hash1' = $intHash1Ref, 'hash2' = $intHash2Ref);

foreach my $hashRefKeys (keys %containerHash) {
   foreach my $hashKeys (keys %{$containerHash{$hashRefKeys}} ) {
  print $hashKeys,, $hashRef-{$hashKeys},\n;
}
}




Output is 

C   D
A   B

-Original Message-
From: Hanson, Rob [mailto:[EMAIL PROTECTED]
Sent: Monday, March 03, 2003 12:14 PM
To: 'Yannick Warnier'; [EMAIL PROTECTED]
Subject: RE: adding hash reference into hash


Your syntax is a little off...

$intHash1Ref = {};
$intHash2Ref = {};

# note use of parens, not curly braces
%containerHash = (hash1 = $intHash1Ref, hash2 = $intHash2Ref);

The parens store a list into %containerHash, the curly-braces were storing a
hash-ref.

Rob

-Original Message-
From: Yannick Warnier [mailto:[EMAIL PROTECTED]
Sent: Monday, March 03, 2003 11:40 AM
To: [EMAIL PROTECTED]
Subject: adding hash reference into hash


Hi,

I'm just trying to make a structure with a hash containing some
references to other (yet unused) hashes.

So what I wrote this:

  $intHash1Ref = {};
  $intHash2Ref = {};

  %containerHash = { hash1 = $intHash1Ref, hash2 = $intHash2Ref};

Then when I try to have a list of keys to that containerHash:

  print keys(%containerHash);

I get some hexadecimal values like:

  HASH(0x813f9b0)

How can I manage to do that cleanly? 
I'm searching in Programming Perl 3th Ed. for that but I don't get
it... yet.

Yannick

_
Envie de discuter en live avec vos amis ? Télécharger MSN Messenger
http://www.ifrance.com/_reloc/m la 1ère messagerie instantanée de France


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: adding hash reference into hash

2003-03-03 Thread Dave K
Yannick,
$ perl -e '
 %ch = (h1 = {}, h2 = {});
 for (keys %ch) {
 print$_\n;
 }'
h1
h2
 Hi,
Hello,
 I'm just trying to make a structure with a hash containing some
 references to other (yet unused) hashes.
see above.

 So what I wrote this:

   $intHash1Ref = {};
   $intHash2Ref = {};

   %containerHash = { hash1 = $intHash1Ref, hash2 = $intHash2Ref};

Also see HOH (hash of hashes in the docs).

HTH



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]