Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Thinkin' Perl in C++

by gibsonca (Beadle)
on Mar 01, 2012 at 16:20 UTC ( #957250=perlquestion: print w/ replies, xml ) Need Help??
gibsonca has asked for the wisdom of the Perl Monks concerning the following question:

I have been using Perl for too long.

In a legacy C++ program, all I need to do is convert strings with a "-" to a "_". 1) Ideally I'd like to find a C++ Perl class I could import into MS IDE 6.0 to make string handling trivial 2) Right now I am trying to recall the CString functions to do this - seems sooo clunky... And yes I think this is a dumb question too - but since I have not finished this is 30 minutes I thought I would ask.

Thx

Comment on Thinkin' Perl in C++
Re: Thinkin' Perl in C++
by davido (Archbishop) on Mar 01, 2012 at 16:50 UTC

    A C-String is an array of char's where the last element is a null character ( '\0' ) which evaluates to false in boolean context. Walk the array with a while loop and an index variable that you will increment++, looking for a '-'. When you find a '-', assign that element a '_'. When you reach a '\0', you're done.

    size_t i = 0; while( cstring[i] ) { if( cstring[i] == '-' ) cstring[i] = '_'; i++; }

    Or without an index, but with pointer arithmatic...

    char * temp = cstring; while( *temp ) { if( *temp == '-' ) *temp = '_'; temp++; }

    You did mention C strings. If you're using the string class, it's a little different.


    Dave

      I guess that when the OP talks about CString he is actually referring to the Windows C++ class CString, not to the C language strings (aka char *)

        Well, then he's on his own. ;) I don't know anything about the Windows CString class. :)


        Dave

        If it's truly CString, then:
        CString sTest("This-has-dashes"); sTest.Replace('-','_');

        mr.nick ...

Re: Thinkin' Perl in C++
by BrowserUk (Pope) on Mar 01, 2012 at 16:53 UTC
    I'd like to find a C++ Perl class I could import ... I am trying to recall the CString functions to do this

    Sounds like a sledgehammer to crack a peanut given all you need is: while( p = strchr( s, '-' ) ) *p = '_';

    I sympathise with the sentiment of wanting Perl's string handling from C, but the power of Perl's string comes not directly from it's built-in functions -- there's only substr & length of note which are nothing particularly powerful or special -- but more from the regex engine.

    Perhaps this is what you are looking for?


    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?

      there's only substr & length of note which are nothing particularly powerful or special
      I'm quite found of the power of concatenation compared on how to do that in C.
Re: Thinkin' Perl in C++
by GrandFather (Cardinal) on Mar 01, 2012 at 20:09 UTC

    If that rather old version of MS's C++ provided ATL with CStringT accessed using #include <atlstr.h> you can use CStringT::Replace to replace instances of a substring with another substring in a string. This seems not to be available with MFC's CString however.

    True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://957250]
Approved by tweetiepooh
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2014-09-02 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (18 votes), past polls