Help - Search - Member List - Calendar
Full Version: How do I get a Hash name after passing the ref?
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Tielman Koekemoer \
Hi all,

After I passed a hash ref to a subroutine, how can I see what the hash
name was? i.e.:

my %hash1 = ( one => 1, two => 2 );

check_limit(%hash1);

sub check_limit {
my $hname = $_[0];

my $num = 0; for (keys %{$hname}) { $num++ };
print "Hash: $hname has $num keysn";
}

Result:

Hash: HASH(0x297f8) has 2 keys

The reason I don't pass the whole hash is because these hash-puppies
can get quite large and I'd like to avoid copying them. This is just
an example of a problem I'm experiencing writing a larger program.

TIA!


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This e-mail and its contents are subject to the Telkom SA Limited
e-mail legal notice available at

http://www.telkom.co.za/TelkomEMailLegalNotice.PDF
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Edward WIJAYA
On Mon, 04 Jul 2005 17:31:03 +0800, Tielman Koekemoer (TNE)
<[Email Removed]> wrote:

QUOTE
After I passed a hash ref to a subroutine, how can I see what the hash
name was? i.e.:

I don't think that's possible. Why do you need that?

--
Regards,
Edward WIJAYA
SINGAPORE

Tielman Koekemoer \
QUOTE
After I passed a hash ref to a subroutine, how can I see

what the hash

name was? i.e.:


I don't think that's possible. Why do you need that?

Well in my case, the program builds some large hashes so I don't want
to copy them from sub to sub. So I pass the ref to a sub and some
repetative things happen with the hash's data. Inside the the sub I
would like to print the contents of the hash if a condition is met.
But I don't know which hash's data the sub is working with if I can't
reference the name. The only other way I can think of is to pass the
name of the hash: do_something_sub(%hash, "hash");

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This e-mail and its contents are subject to the Telkom SA Limited
e-mail legal notice available at

http://www.telkom.co.za/TelkomEMailLegalNotice.PDF
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Xavier Noria
On Jul 4, 2005, at 14:41, Tielman Koekemoer ((TNE)) wrote:

QUOTE
Well in my case, the program builds some large hashes so I don't want
to copy them from sub to sub. So I pass the ref to a sub and some
repetative things happen with the hash's data. Inside the the sub I
would like to print the contents of the hash if a condition is met.
But I don't know which hash's data the sub is working with if I can't
reference the name. The only other way I can think of is to pass the
name of the hash: do_something_sub(%hash, "hash");

Do you know this notation?

$hashref->{key}

Doy you know references can be dereferenced? For hashes the syntax is
to prepend a "%" to the hashref. For example,

while (my ($k, $v) = each %$hashref) {
# ...
}

or

foreach my $key (sort keys %$hashref) {
# ...
}

That is documented in perlref.

-- fxn

Tielman Koekemoer \
QUOTE
Do you know this notation?


$hashref->{key}



Yes I know how to dereference a ref. I wanted to know if you could get
the name of a hash (in my case, but this could be applied to any data
type) from a ref:

BEGIN
%hash = ( one => 1, two => 2 );

do_sometin(%hash);

sub do_sometin {
$href = $_[0];
for (keys %{$href}) {
print "The hash $href value is: ${$href}{$_}n";
}
}
END

Generates:
The hash HASH(0x29804) value is: 2
The hash HASH(0x29804) value is: 1

I would like to get the name for: HASH(0x29804)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This e-mail and its contents are subject to the Telkom SA Limited
e-mail legal notice available at

http://www.telkom.co.za/TelkomEMailLegalNotice.PDF
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Charles K. Clarkson
Tielman Koekemoer (TNE) <mailto:[Email Removed]> wrote:

: Generates:
: The hash HASH(0x29804) value is: 2
: The hash HASH(0x29804) value is: 1
:
: I would like to get the name for: HASH(0x29804)

Sounds like you need an object, not a hash. An object
knows its name.

HTH,

Charles K. Clarkson
--
Mobile Homes Specialist


John W. Krahn
Tielman Koekemoer (TNE) wrote:
QUOTE

Hi all,

Hello,

QUOTE
After I passed a hash ref to a subroutine, how can I see what the hash
name was? i.e.:

my %hash1 = ( one => 1, two => 2 );

check_limit(%hash1);

sub check_limit {
my $hname = $_[0];

my $num = 0; for (keys %{$hname}) { $num++ };
print "Hash: $hname has $num keysn";
}

Result:

Hash: HASH(0x297f8) has 2 keys

The reason I don't pass the whole hash is because these hash-puppies
can get quite large and I'd like to avoid copying them. This is just
an example of a problem I'm experiencing writing a larger program.

my %hash1 = ( one => 1, two => 2 );

check_limit( '%hash1', %hash1 );

sub check_limit {
my ( $hname, $href ) = @_;

my $num = keys %$href;
print "Hash: $hname has $num keysn";
}



John
--
use Perl;
program
fulfillment

Tielman Koekemoer \
QUOTE
This is just an example of a problem I'm experiencing writing a
larger program.


my %hash1 = ( one => 1, two => 2 );


check_limit( '%hash1', %hash1 );


sub check_limit {
my ( $hname, $href ) = @_;


my $num = keys %$href;
print "Hash: $hname has $num keysn"; }

Thanks everyone. I will explore objects, but in the mean time I will
have to pass the name as above - so much for being lazy :p

Thanks again.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This e-mail and its contents are subject to the Telkom SA Limited
e-mail legal notice available at

http://www.telkom.co.za/TelkomEMailLegalNotice.PDF
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.