Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

confused with Inline::C again

by baxy77bax (Deacon)
on Dec 01, 2011 at 13:54 UTC ( [id://941076]=perlquestion: print w/replies, xml ) Need Help??

baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

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 !

Replies are listed 'Best First'.
Re: confused with Inline::C again
by Eliya (Vicar) on Dec 01, 2011 at 15:08 UTC

    If you want to return a list of values (suffixes) that you can assign in a statement like

    my @a = Suff(@string);

    you'd need to return them on the stack (Perl's stack, not C's). For this, there are the Inline macros

    Inline_Stack_Vars Inline_Stack_Reset Inline_Stack_Push(sv) Inline_Stack_Done

    which should be used in that sequence (of course, push as many values as you need).

    As an alternative, you could create (and fill) an array (AV) on the XS side, and return a reference to it. This means, you'd assign it like this

    my $aref = Suff(@string); # print "@$aref"; # or whatever

    Other than that, it's hard to help (well, for me at least), because from your sample code I can't really make any inferences as to what exactly you're trying to do.

    Update: (in response to your update)

    #!/usr/bin/perl -wl use strict; use Inline C => <<'END_C'; void test() { Inline_Stack_Vars; Inline_Stack_Reset; char *string = "lhfsadgfbfsdaubsdkj"; int i; STRLEN n = strlen(string); for(i=0; i<n; i++){ Inline_Stack_Push( sv_2mortal( newSVpv(string + i, 0) ) ); } Inline_Stack_Done; } END_C my @a = test(); print for @a;

    Output:

    lhfsadgfbfsdaubsdkj hfsadgfbfsdaubsdkj fsadgfbfsdaubsdkj sadgfbfsdaubsdkj adgfbfsdaubsdkj dgfbfsdaubsdkj gfbfsdaubsdkj fbfsdaubsdkj bfsdaubsdkj fsdaubsdkj sdaubsdkj daubsdkj aubsdkj ubsdkj bsdkj sdkj dkj kj j
Re: confused with Inline::C again
by BrowserUk (Patriarch) on Dec 01, 2011 at 21:12 UTC

    You've named your array @string. Did it start out as a string?

    If so, in fact even if not, the simplest way of producing your list of suffices is:

    ## Skip this step (and the split) if the array started out as a string +! $string = join '', qw(a a b h a g s j s z u u e);; print substr( $string, $_ ) for 0 .. length( $string ) -1;; aabhagsjszuue abhagsjszuue bhagsjszuue hagsjszuue agsjszuue gsjszuue sjszuue jszuue szuue zuue uue ue e

    Without having tested it, I'd bet even with the join that'll run twice as quickly as any I::C implementation.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: confused with Inline::C again
by CountZero (Bishop) on Dec 01, 2011 at 20:26 UTC
    C is not my love, but in Perl this is trivially easy:
    use Modern::Perl; my @string = qw(a a b h a g s j s z u u e); for (1 .. @string) { say join '', @string; shift @string; }
    Output:
    aabhagsjszuue abhagsjszuue bhagsjszuue hagsjszuue agsjszuue gsjszuue sjszuue jszuue szuue zuue uue ue e
    Update: Of course this routine clears @string, if you need its original contents, copy it into another array.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: confused with Inline::C again
by girarde (Hermit) on Dec 05, 2011 at 22:44 UTC
    Please use the <more> tag with a post this long.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://941076]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-24 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found