SPUG: deferencing a hash in an array of hashes

Todd Wells toddw at wrq.com
Mon Nov 6 18:22:28 CST 2000


I'm not sure why I'm having a problem with this, but here's the situation:

I have a subroutine which builds up an array of hashes in @attributes, and
then I want to use one of those hashes as it's returned value.

	return $attributes[0]; # returns the hash reference in the array.

How can I return the de-referenced hash?  I seem to be having difficulty.

Here's @attributes:

  DB<5> x @attribs
0  HASH(0x227a8d0)
   'defaultvalue' => 'junkchar'
   'initialization' => 'atconnection'
   'isencrypted' => 'false'
   'ishidden' => 'false'
   'isreadable' => 'false'
   'iswriteable' => 'false'
   'name' => 'userID'
   'variabletype' => 'cursor'

I just figured I'll de-reference it into %hash and return it as a hash
rather than a reference...

	my %hash = %{$attribs[0]}; # this must be wrong
	return %hash;

Alas!  This doesn't do what I want, now:

  DB<6> x %hash
0  'ishidden'
1  'false'
2  'isreadable'
3  'false'
4  'isencrypted'
5  'false'
6  'iswriteable'
7  'false'
8  'defaultvalue'
9  'junkchar'
10  'initialization'
11  'atconnection'
12  'name'
13  'userID'
14  'variabletype'
15  'cursor'

Can someone please show me the error of my ways?

Ideally, I'd skip the "my %hash = " part altogether and just do something
like 

	return %{$attribs[0]}; # or whatever the proper syntax is.

Thanks,

-Todd

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list