SPUG: deferencing a hash in an array of hashes
Todd Wells
toddw at wrq.com
Mon Nov 6 19:02:23 CST 2000
Unfortunately, it doesn't seem to be working this way. Maybe you can point
out my error.
Here's the calling code:
my %attributes = get_xml_atts($resultfile, "variablemetadata", 1);
and the sub:
sub get_xml_atts
{
my ($file, $element, $occurrence) = @_;
if (not defined($occurrence)) {$occurrence = 1;}
undef @attribs;
$occurrence--; # for indexing
$sought_element = $element;
$need_attribs = 1;
my $parser = new XML::Parser(Handlers =>
{ Start => \&tag_start,
End => \&tag_end,
Char => \&xml_characters,
});
$parser->parsefile("$file");
undef $sought_element; undef $need_attribs;
return %{$attribs[$occurrence]};
}
And so you see where @attribs is created:
sub tag_start
{
my ($parser, $element, %attribs) = @_;
if ($element eq 'error') { $in_error = 1; }
if ($AppconnXML::sought_element &&
($element eq $AppconnXML::sought_element))
{
$in_sought = 1;
if ($AppconnXML::need_attribs)
{
push @AppconnXML::attribs, \%attribs;
}
}
}
and when all is said and done %attributes ends up just like this:
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'
-Todd
-----Original Message-----
From: ced at carios2.ca.boeing.com [mailto:ced at carios2.ca.boeing.com]
Sent: Monday, November 06, 2000 4:41 PM
To: spug-list at pm.org; toddw at wrq.com
Subject: Re: SPUG: deferencing a hash in an array of hashes
> 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'
> ...
> 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'
>...
> 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.
You're fine. The return statement just unrolls the hash
and returns the list of key-value pairs.
So, e.g. in sub 'foo' simply:
return %{$attribs[0]};
then in the call that invokes 'foo':
my %return_hash = foo();
and that automagically populates your hash.
Note because a list is being returned, you could
have just as well created a simple array:
my @return_array = foo();
hope this helps,
--
Charles DeRykus
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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