in reply to Inline C : dereferencing array of hash references returned from function

Couldn't get your code, or ikegami's to work for me ... not sure why. (Update: Nothing wrong with ikegami's code, of course. Looks to me that $value in the op's code bears no relation to the hashref that has been returned and therefore is undef.)

The following *does* work for me, and should contain all you need to get your own code working:
use Inline C => Config => BUILD_NOISY => 1; use Inline C; $arref = get_data(); print $arref, "\n"; print $arref->[0]->{mickey}, "\n"; print $arref->[0]->{donkey}, "\n"; __END__ __C__ SV* get_data() { AV * results; HV * rh = newHV(); results = newAV(); hv_store(rh, "mickey", 6, newSVpv("mouse", 0), 0); hv_store(rh, "donkey", 6, newSVpv("kong", 0), 0); av_push(results,newRV_noinc((SV*)rh)); return newRV_noinc((SV*)results); }
Update: The 'BUILD_NOISY' config option that I've included in the above script ensures that you get the opportunity to see compiler warnings - which can be useful if a script does not behave as intended. It's a good idea to *always* have BUILD_NOISY switched on.

Cheers,
Rob
  • Comment on Re: Inline C : dereferencing array of hash references returned from function
  • Download Code

Replies are listed 'Best First'.
Re^2: Inline C : dereferencing array of hash references returned from function
by String62 (Novice) on Feb 16, 2011 at 16:38 UTC
    Changed a little your code to print out the keys as well as the values. De-referencing the hash is now ok, see code below. Thanks to you and ikegami for being so responsive, it helped me on my way. PS: indeed BUILD NOISY option is very informative.
    use Inline C => Config => BUILD_NOISY => 1; use Inline C; $arref = get_data(); print $arref, "\n"; $href = $arref->[0]; foreach my $key (sort keys %$href) { # print "v1 Key: ", $key, " => Value: ", $arref->[0]->{$key}, " +\n"; print "v2 Key: ", $key, " => Value: ", $href->{$key}, "\n"; } __END__ __C__ SV* get_data() { AV * results; HV * rh = newHV(); results = newAV(); hv_store(rh, "mickey", 6, newSVpv("mouse", 0), 0); hv_store(rh, "donkey", 6, newSVpv("kong", 0), 0); av_push(results,newRV_noinc((SV*)rh)); return newRV_noinc((SV*)results); }
Re^2: Inline C : dereferencing array of hash references returned from function
by String62 (Novice) on Feb 16, 2011 at 12:24 UTC
    Hi Rob, "Couldn't get your code", well here's the recipe, get a windows box (heheheh ;-)), with active state perl installed (mine is currently 5.10.0), ppm install mingw. At least this works for me. It also works for me with visual studio 2005. Made a copy of the vsvars32.bat file and added "perl.exe %1" as the last line in this file. Invoking perl scripts with this bat file works also fine, and uses MS's nmake instead of MinGW's dmake. Anyway, you can be sure I'll test out your code asap and come back to comment. Thank you very much for your reply ! Really appreciate, Bruno
      "Couldn't get your code to work", not "Couldn't get your code"