I am unable to retrieve the value of a key in a hash h1, if that value is another hash reference h2, and then recognize keys in h2. What am I doing wrong and how can I fix it? I have a structure stored in Perl as a multi-dimensional hash like the following:
my $h1;
my $h2;
$h2->{'bar'} = 1;
$h1->{'foo'} = $h2;
I can test the existence of 'foo' in $h1 with XS as follows:
void
mhash1(key1, h1)
char* key1
HV* h1
CODE:
I32 klen1 = strlen(key1);
if ( hv_exists(h1, key1, klen1) ) {
}
I cannot test for 'bar' because when I get the value of 'foo' (which is another hash reference) it appears not to be the right kind of structure in the following code:
void
mhash2(key1, h1, key2)
char* key1
char* key2
HV* h1
CODE:
I32 klen1 = strlen(key1);
I32 klen2 = strlen(key2);
if ( hv_exists(h1, key1, klen1) ) {
SV** h2r = hv_fetch(h1, key1, klen1, 0);
HV* h2 = (HV*)*h2r;
if ( hv_exists(h2, key2, klen2) ) {
printf("%s\n", "found it!");
}
}
The following Perl code results in a segmentation fault:
#!/usr/bin/perl
use strict;
use warnings;
use MyTest;
my $h1;
my $h2;
$h2->{'bar'} = 1;
$h1->{'foo'} = $h2;
MyTest::mhash2('foo', $h1, 'bar');
Thank you!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.