hi,
very simple task. I just want to have the same behavior in following code. in code A i pass an array of characters to the function and return back to perl an array of pointers (or array indexes) to my initial array so that i can print a list of suffixes. Code B illustrates my end result.
Code A
#!/usr/bin/perl
use strict;
my @string = qw(a a b h a g s j s z u u e);
my @a = Suff(@string);
print "@a\n"; #so here i should get numbers form 0..18 or something
use Inline C => <<'END_OF_C_CODE';
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void Suff(SV* array, ...){
int i,n;
Inline_Stack_Vars;
n = Inline_Stack_Items;
char **sarray;
sarray = (char **)malloc(n * sizeof(char *));
char *string[Inline_Stack_Items];
for (i = 0; i < n; i++){
string[i] = SvPV(Inline_Stack_Item(i), PL_na);
printf("%s",string[i]); // it prints characters instead of
+ strings
}
for(i=0;i<n;i++){
sarray[i]=string + i;
}
for(i=0;i<n;i++){
printf("%s - ",*(sarray+i)); // prints rubbish
}
return "How to return sarray?";
}
Code B - this is what i'm aiming to get as a final result, it depends
+on what can i return to perl from c but the list of suffixes should b
+e printed in the end
int test(){
char **sarray;
int i, n=15;
sarray = (char **)malloc(n* sizeof(char *));
char *string = "lhfsadgfbfsdaubsdkj";
for(i=0;i<n;i++){
sarray[i]=string + i;
}
for(i=0;i<n;i++){
printf("%s - ",sarray[i]);
}
}
can someone give a hand
baxy
Update so what i'm trying to achieve is , if the following code B when compiled and executed, for a string : "lhfsadgfbfsdaubsdkj" returns
lhfsadgfbfsdaubsdkj - hfsadgfbfsdaubsdkj - fsadgfbfsdaubsdkj - sadgfbf
+sdaubsdkj - adgfbfsdaubsdkj - dgfbfsdaubsdkj - gfbfsdaubsdkj - fbfsda
+ubsdkj - bfsdaubsdkj - fsdaubsdkj - sdaubsdkj - daubsdkj - aubsdkj -
+ubsdkj - bsdkj - sdkj - dkj - kj - j
for Code A where an input is an array like this :
my @string = qw(a a b h a g s j s z u u e);
the program should provide means to print in perl (not in c) the same thing:
aabhagsjszuue - abhagsjszuue - bhagsjszuue - hagsjszuue - agsjszuue -
+gsjszuue - sjszuue - jszuue - szuue - zuue - uue - ue - e -
hope this helps to clear whys and whats
UPDATE 2
ok with your help i got to this, so if anyone wants to use it, feel free
use strict;
my @string = qw(a a b h a g s j s z u u e);
my $n=@string;
my $str = join('',@string);
my @a = Suff($str,$n);
foreach (@a){
print "@string[$_..$n]\n";
}
use Inline C => <<'END_C';
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void Suff(char* array,int n){
int i;
char **sarray;
sarray = (char **)malloc(n * sizeof(char *));
char *string;
string = savepv(array);
for(i=0;i<n;i++){
sarray[i]=string + i;
}
Inline_Stack_Vars;
Inline_Stack_Reset;
for(i=0;i<n;i++){
Inline_Stack_Push( sv_2mortal( newSViv(n - (int)strlen(sar
+ray[i]) ) ) );
}
Inline_Stack_Done;
}
END_C
the reason why I am doing this in this awkward way which may be not so memory efficient an fast as BrowserUK and CountZero suggested is because there are at leas doesn functions that require sequential results and "by products" of this procedure. But thank you !