baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:
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?"; }
can someone give a handCode 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]); } }
baxy
Update so what i'm trying to achieve is , if the following code B when compiled and executed, for a string : "lhfsadgfbfsdaubsdkj" returns
for Code A where an input is an array like this :lhfsadgfbfsdaubsdkj - hfsadgfbfsdaubsdkj - fsadgfbfsdaubsdkj - sadgfbf +sdaubsdkj - adgfbfsdaubsdkj - dgfbfsdaubsdkj - gfbfsdaubsdkj - fbfsda +ubsdkj - bfsdaubsdkj - fsdaubsdkj - sdaubsdkj - daubsdkj - aubsdkj - +ubsdkj - bsdkj - sdkj - dkj - kj - j
the program should provide means to print in perl (not in c) the same thing:my @string = qw(a a b h a g s j s z u u e);
hope this helps to clear whys and whatsaabhagsjszuue - abhagsjszuue - bhagsjszuue - hagsjszuue - agsjszuue - +gsjszuue - sjszuue - jszuue - szuue - zuue - uue - ue - e -
UPDATE 2
ok with your help i got to this, so if anyone wants to use it, feel free
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 !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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: confused with Inline::C again
by Eliya (Vicar) on Dec 01, 2011 at 15:08 UTC | |
|
Re: confused with Inline::C again
by BrowserUk (Patriarch) on Dec 01, 2011 at 21:12 UTC | |
|
Re: confused with Inline::C again
by CountZero (Bishop) on Dec 01, 2011 at 20:26 UTC | |
|
Re: confused with Inline::C again
by girarde (Hermit) on Dec 05, 2011 at 22:44 UTC | |
by Anonymous Monk on Dec 05, 2011 at 22:53 UTC |