Archive for July, 2010

STL in C++

Thursday, July 29th, 2010

STL stands for Standard Template Library in C++. It is very convenient, especially for people who use diverse languages. For example, I would assume there are functions like this:

a = “007”

as.numeric(a)==7 => should be true

In many latest languages, it is very friendly(saving finger muscle movements) that you can find such similar function doing the conversion. But back to C++, it is not the case at all. All the sudden we are back to stone age. No need to panic though. In STL, there are some functions for this purpose–converting string to number. Although still not very convenient, it is better than nothing unless you write your own (perfectly fine).  Suppose you want to pass input values for different variable at command line, there are up to 3 variables, “start”, “end”, “seed”, the STL string is quite useful for this purpose because it has strcmp and atoi to do string comparison and converting string to integers, respectively. Here is how I would do:

#include <string>

using namespace std;

int main(int argc, char *argv[])
{
int StartI, EndI, Seed;

if (argc ==  1){
cout << “Error: no command line inputs!” << endl;
exit(0);
}

for (int ai=1; ai < argc; ai++){
if (strcmp(argv[ai], “-start”)==0)
StartI = atoi(argv[++ai]);
else if (strcmp(argv[ai], “-end”)==0)
EndI = atoi(argv[++ai]);
else if (strcmp(argv[ai], “-seed”)==0)
Seed = atoi(argv[++ai]);
else
cout << “Unknown command line inputs!” << endl;
}
}

There are surely other excellent STLs, for example, vector, list,… But I personally haven’t used them yet since if possible I use other languages (PERL and R) to do such things instead.

Wonder…

Wednesday, July 28th, 2010

Sometimes I read very well-written sentences from famous people and have to wonder how they got those words. For example,

Until man duplicates a blade of grass, nature will laugh at his so-called scientific knowledge. –Thomas Edison

A quick answer is that Edison didn’t spend time on instant messaging, reading/writing blogs, checking new iPhone apps… Wow, how boring was his life? 🙂

Simple parallel codes in PERL

Friday, July 23rd, 2010

There are many ways to do parallel jobs in PERL, which way to go is pretty much dependent on the problem. There is one problem like I worked on, it is pretty much serial jobs, but need to be executed on multiple cores, there is no communication between jobs, so the straightforward solution other than repeat running serial jobs is the following:

For example, I need run 8 jobs:

use threads;
my( $t1 ) = threads->create( \&sub1 );
my( $t2 ) = threads->create( \&sub2 );
.. ..

my ($t8)= threads->create( \&sub8 );

sub1(),…sub8() could be up to 8 different functions.

Passing arguments from command line in R

Thursday, July 1st, 2010

Suppose need to pass three arguments to R from command line. In R script,

args <- commandArgs()

x1 <- args[3]

x2 <- args[4]

x3 <- argx[5]

Then run from command line: R –vanilla < script.R x1 x2 x3