Hello,
I am returning from an inline C function an array filled with references to hashes (see my code below).
In this particular case, just for testing, I am returning 1 hash.
In the perl code I am walking the array, and subsequently walking the hash.
I am returning correctly the keys of the hash, but for whatever reason, getting at the key-value fails in the foreach loop of the hash.
Tried everything, but currently to no avail.
The uncommented line prints the keys.
Uncommenting the second one doesn't produce the values.
Tried hard and really wondering what's going on here.
Could somebody have a look at this.
Any help very much appreciated.
Cheers,
..........
foreach my $key (sort keys %$href) {
print "Key: ".$key."\n";
# print "Key: ".$key." => Value: ".$href->{$key}."\n";
}
..........
COMPLETE CODE:
-------------
use Inline C;
$array_ref = get_data();
print "Array ref: ".$array_ref."\n";
$href = $array_ref->[0];
print "Hash ref: ".$href."\n";
foreach my $key (sort keys %$href) {
print "Key: ".$key." => Value: ".$value."\n";
# print "Key: ".$key." => Value: ".$href->{$key}."\n";
}
__END__
__C__
SV* get_data() {
SV * x;
AV * results;
I32 nr = 0;
int i, n;
results = (AV *)sv_2mortal((SV *)newAV());
for (n = 0; n <= nr; n++) {
HV * rh;
STRLEN l;
rh = (HV *)sv_2mortal((SV *)newHV());
hv_store(rh, "mickey", 6, "mouse", 0);
hv_store(rh, "donkey", 6, "kong", 0);
av_push(results, newRV((SV * )rh));
}
return newRV((SV*)results);
}
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.