Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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.


Replies are listed 'Best First'.
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 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.


      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 *)
        If it's truly CString, then:
        CString sTest("This-has-dashes"); sTest.Replace('-','_');

        mr.nick ...

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


Re: Thinkin' Perl in C++
by GrandFather (Sage) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://957250]
Approved by tweetiepooh
[thezip]: So, we have a small deployment coming up soon for a project. What is the general name for the project management documentation that details the tasks and schedules/ sequences for the execution of the deployment?
[thezip]: Deployment checklist?
[Corion]: thezip: Yeah - we have "CAP", but that means "Change Ablauf Plan" (Change Sequence Plan). Likely there is a true ITIL name for this too.
[Corion]: While I'm not really a fan of ITIL, it certainly is better than nothing and it at least provides a common vocabulary, so if you have ITIL somewhere at your disposal, it might be worth a look

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2017-02-23 17:50 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (350 votes). Check out past polls.