Click here to Skip to main content
11,483,586 members (68,306 online)
Click here to Skip to main content

CPerlString - A Class to Utilise Perl String Functions

, 31 Aug 2002 134.7K 1.3K 24
Rate this:
Please Sign up or sign in to vote.
A Class to Utilise Perl String Functions
Sample Image - CPerlString.gif  

Introduction

String manipulation and regular expressions has always been a strength of Perl. The C language is powerful but to do string manipulation and regular expressions is difficult. Although there are libraries like PCRE which simulate Perl regular expressions in C, I have found them difficult to use.  So I thought, the best way is to embel Perl inside C.

With the help of Perl's documentation, I have successfully created two classes to encapsulate some of Perl's most useful functions. One class is for MFC users (CPerlString.h) and one class is for non-MFC users (PerlString.h). The functions encapsulated are:

  1. Pattern matching
  2. String substitution
  3. Joining of an array into a string
  4. Splitting of a string into an array
  5. Sort (forward or reverse)
  6. Chop
  7. Chomp

I will discuss the usage of the MFC class. The non-MFC class is similar but it uses STL string and vector<string> instead of CString and CStringArray.

I32 CPerlString::Match (CString inputString, CString pattern)

I32 CPerlString::Matches (CString inputString, CString pattern, CStringArray &matchList)

Match and Matches are two function which makes use of Perl's pattern matching ability. Basically, Match sends a Perl's statement: inputString =~ pattern and Matches sends a Perl's statement: matchList = (inputString =~ pattern). Match will return 1 if pattern is found and 0 if pattern is not found. Matches will return the number of matches.

CPerlString perl;
CString inputString = "Hello World!";
CString pattern1 = "/Hello/";
CString pattern2 = "/(.o)/g";
CStringArray matchList;

if (perl.Match(inputString, pattern1))
  printf("Pattern found\n");
else
  printf("Pattern not found\n");

int num_matches = perl.Matches(inputString, pattern2, matchList);

printf("%d matches\n", num_matches);
if (num_matches > 0)
{
  for (int i = 0; i < matchList.GetSize(); i++)
    printf("Match %d: %s\n", i+1, matchList.GetAt(i));

I32 Substitute(CString &inputOutputString, CString pattern)

Substitute is a function which makes use of Perl's string substitute ability. Basically it sends: inputOutputString =~ pattern. Substitute will return 1 if substitution is done and 0 if it is not done.

CPerlString perl;
CString inputOutputString = "Hello World!";
CString pattern1 = "s/Hello/Hello Happy/";

perl.Substitute(inputOutputString, pattern1);
printf("%s\n", inputOutputString);

void Join(CStringArray &inputStringList, CString pattern, CString &outputString)

Join is a function which makes use of Perl's joining ability. It sends: outputString = join (pattern, inputStringList). Join does not return any value.

CPerlString perl;
CString outputString;
CString pattern1 = " ";
CStringArray inputStringList;
inputStringList.Add("Hello");
inputStringList.Add("Happy");
inputStringList.Add("World");

perl.Join(inputStringList, pattern1, outputString);
printf ("%s\n", outputString);

I32 Split(CString inputString, CString pattern, CStringArray &splitList)

Split performs the Perl's statement: splitlist = split (pattern, inputString). It returns the number of split items.

CPerlString perl;
CString inputString = "Hello Happy World!";
CString pattern1 = "/\\s/";
CStringArray splitList;

int num_split = perl.Split(inputString, pattern1, splitList);

printf("%d split\n", num_split);
if (num_split > 0)
{
  for (int i = 0; i < splitList.GetSize(); i++)
    printf("Split %d: %s\n", i+1, splitList.GetAt(i));

void Sort(CStringArray &inputStringList, CStringArray &outputStringList, int Direction = 0)

Sort performs sorting on an array by sending: outputStringList = sort (inputStringList) if direction is 0 and outputStringList = reverse sort (inputStringList) if direction is not 0 (e.g. 1). Sort does not return any value.

CPerlString perl;
CStringArray inputStringList, outputStringList;
inputStringList.Add("Hello");
inputStringList.Add("Happy");
inputStringList.Add("World");

perl.Sort(inputStringList, outputStringList); // Forward sort

for (int i = 0; i < outputStringList.GetSize(); i++)
  printf("%s\n", outputStringList.GetAt(i));

perl.Sort(inputStringList, outputStringList, 1); // Reverse sort

for (int i = 0; i < outputStringList.GetSize(); i++)
  printf("%s\n", outputStringList.GetAt(i));

void Chomp(CString &inputOutputString)

void Chomp(CStringArray &inputOutputStringList)

void Chop(CString &inputOutputString)

void Chop(CStringArray &inputOutputStringList)

Chop and Chomp are two functions which performs Perl's chop and chomp. They are overloaded to handle a string and a string array. The corresponding Perl's statement is: chomp (inputOutputString) or chomp (inputOutputStringList) or chop (inputOutputString) or chop (inputOutputStringList).

CPerlString perl;
CString inputOutputString = "Hello World!";
CStringArray inputOutputStringList;
inputOutputStringList.Add("Hello");
inputOutputStringList.Add("Happy");
inputOutputStringList.Add("World");

perl.Chop(inputOutputString);
printf("%s\n", inputOutputString);

perl.Chop(inputOutputStringList);
for (int i = 0; i < inputOutputStringList.GetSize(); i++)
  printf("%s\n", inputOutputStringList.GetAt(i));

Usage

To use the two classes, you will need to include them in your project by adding the statement

#include "CPerlString.h"

or 

#include "PerlString.h"

You will also need to have the Perl's core libraries. This will be found at the directory where you install Perl. For example, if you install Perl in C:\Perl, the core libraries will be found at C:\Perl\lib\CORE. You will need to set this directory as one of the default include and library directory for Visual C++. This can be done by Tools->Options->Directories and adding C:\Perl\lib\CORE in the include files and library files section.

Demo

The demo program demonstrate the use of the above-mentioned functions except chomp and chop. It can be used to help test your regular expression patterns.

History

6 Mar 2002 - Updated source files.
1 Sep 2002 - Plug memory leak. Updated source files

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Yap Chun Wei
Researcher
Singapore Singapore
No Biography provided

Comments and Discussions

 
GeneralBoost.regex Pin
Stephen Hewitt27-Aug-06 22:37
memberStephen Hewitt27-Aug-06 22:37 
QuestionMatches works, Match doesn't? Pin
C Morell4-Sep-03 9:53
memberC Morell4-Sep-03 9:53 
AnswerRe: Matches works, Match doesn't? Pin
Yap Chun Wei4-Sep-03 16:45
memberYap Chun Wei4-Sep-03 16:45 
GeneralRe: Matches works, Match doesn't? Pin
C Morell5-Sep-03 6:04
memberC Morell5-Sep-03 6:04 
GeneralUnresolved External Symbol Pin
jrlambs14-Jun-03 8:50
memberjrlambs14-Jun-03 8:50 
GeneralRe: Unresolved External Symbol Pin
Yap Chun Wei15-Jun-03 16:57
memberYap Chun Wei15-Jun-03 16:57 
GeneralRe: Unresolved External Symbol Pin
jrlambs24-Jun-03 17:14
memberjrlambs24-Jun-03 17:14 
GeneralRe: Unresolved External Symbol Pin
Yap Chun Wei25-Jun-03 16:45
memberYap Chun Wei25-Jun-03 16:45 
Generalcompile error Pin
Anonymous2-Nov-02 5:15
sussAnonymous2-Nov-02 5:15 
GeneralRe: compile error Pin
Yap Chun Wei2-Nov-02 17:11
memberYap Chun Wei2-Nov-02 17:11 
GeneralInfo Pin
Simonla200110-Oct-02 6:27
memberSimonla200110-Oct-02 6:27 
GeneralRe: Info Pin
Yap Chun Wei10-Oct-02 16:45
memberYap Chun Wei10-Oct-02 16:45 
GeneralRe: Info Pin
Simonla200110-Oct-02 22:04
memberSimonla200110-Oct-02 22:04 
GeneralRe: Info Pin
Simonla200111-Oct-02 7:57
memberSimonla200111-Oct-02 7:57 
GeneralRe: Info Pin
Simonla200111-Oct-02 8:00
memberSimonla200111-Oct-02 8:00 
GeneralI'm not shure... Pin
Julian (TSL)7-Oct-02 7:41
sussJulian (TSL)7-Oct-02 7:41 
I've already created something like that, it's easy because it is documented in Perl docs, but there is *only* one problem.
YOU NEED Perp56.dll to RUN application.
So my question is how you can link Perl56.lib staticly to executible?!?

Regards
GeneralCPerlString and Multithreading Pin
Anonymous15-Apr-02 8:57
memberAnonymous15-Apr-02 8:57 
QuestionCan I incorporate perl lib into it? Pin
Anonymous7-Mar-02 12:25
memberAnonymous7-Mar-02 12:25 
QuestionPerl Implementation ?? Pin
Garth J Lancaster3-Mar-02 19:03
memberGarth J Lancaster3-Mar-02 19:03 
AnswerRe: Perl Implementation ?? Pin
Yap Chun Wei3-Mar-02 19:40
memberYap Chun Wei3-Mar-02 19:40 
Generalmemory leak Pin
Elvis28-Feb-02 22:39
memberElvis28-Feb-02 22:39 
GeneralRe: memory leak Pin
Yap Chun Wei28-Feb-02 23:00
memberYap Chun Wei28-Feb-02 23:00 
GeneralRe: memory leak Pin
Anonymous28-Feb-02 23:57
memberAnonymous28-Feb-02 23:57 
GeneralRe: memory leak Pin
varadii30-Aug-02 2:25
membervaradii30-Aug-02 2:25 
GeneralRe: memory leak Pin
Yap Chun Wei1-Sep-02 7:01
memberYap Chun Wei1-Sep-02 7:01 
GeneralNeed More! Pin
Zhuofei Wang31-Dec-01 7:56
memberZhuofei Wang31-Dec-01 7:56 
GeneralRe: Need More! Pin
Yap Chun Wei31-Dec-01 17:42
memberYap Chun Wei31-Dec-01 17:42 
GeneralRe: Need More! Pin
Zhuofei Wang1-Jan-02 15:02
memberZhuofei Wang1-Jan-02 15:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 1 Sep 2002
Article Copyright 2001 by Yap Chun Wei
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid