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 !

In reply to confused with Inline::C again by baxy77bax

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.