Neil Watkiss wrote:
>
> On Sun, 4 Feb 2001, Jack H. Stanley wrote:
>
> > I applaud the latest release of Inline. I have integrated a complex API
> > into Perl with ease. I would appreciate a reference to a method of
> > returning an array of hashes.
> >
>
> Try this:
>
> ----8<----
> use Inline 'C';
> use Data::Dumper;
>
> my @array = get_hashes();
That example returns a _list_ of hashes. To return an array (ref) of
hashes (refs) use this example adapted from my HOL one in the
C-Cookbook:
----8<----
use Inline C;
use Data::Dumper;
$array_ref = load_data("./people.txt");
print Dumper $array_ref;
__END__
__C__
static int next_word(char**, char*);
SV* load_data(char* file_name) {
char buffer[100], word[100], * pos;
HV* hash;
AV* array = newAV();
FILE* fh = fopen(file_name, "r");
while (fgets(pos = buffer, sizeof(buffer), fh)) {
av_push(array, newRV_noinc((SV*)hash = newHV()));
next_word(&pos, word);
hv_store(hash, "First Name", 10, newSVpvf("%s", word), 0);
next_word(&pos, word);
hv_store(hash, "Last Name", 9, newSVpvf("%s", word), 0);
next_word(&pos, word);
hv_store(hash, "Nick Name", 9, newSVpvf("%s", word), 0);
}
fclose(fh);
return newRV_noinc((SV*) array);
}
static int next_word(char** text_ptr, char* word) {
char* text = *text_ptr;
while(*text != '\0' &&
*text <= ' ')
text++;
if (*text <= ' ')
return 0;
while(*text != '\0' &&
*text > ' ') {
*word++ = *text++;
}
*word = '\0';
*text_ptr = text;
return 1;
}
----8<----
people.txt:
----8<----
Brian Ingerson INGY
Neil Watkiss NEILW
Jack Stanley JHS
----8<----
Cheers, Brian
--
perl -le 'use Inline C=>q{SV*JAxH(char*x){return newSVpvf
("Just Another %s Hacker",x);}};print JAxH+Perl'