最近在意识到,c++中文件流的输入方式打开文档,例如
fstream filestr("filename",ios::in);
将意味着读文件filename中的内容
fstream filestr("filename",ios::out);
将意味着写文件filename,如果filename已存在,则清空文件内容从新开始写。
这与我已前对它的认识有出入,正好相反:我认为ios::in意味着写文件,ios::out
为读文件。
备忘。
posted @ 2012-02-20 09:11 ewre 阅读(459) | 评论 (0)编辑 收藏
最近在ubuntu上写c++的peason corr操作,用code::blocks做IDE
,用了一个开源的科学计算库alglib,开始进行测试的时候
gnu gcc 老是报错:undefined reference to XXXX
google未果,最后才发现原来是没有把header和cpp添加
到工程中去。记得在用kdevelop时犯过这种错误,备忘....
posted @ 2012-02-18 16:05 ewre 阅读(1135) | 评论 (0)编辑 收藏
这几天想在自己的wm手机上写个小程序,用ms自己的东西觉得太复杂也太丑,就想到了qt,环境是
vs2008 sp1,qt-win-opensource-4.8.0-vs2008.exe addin1.1.0
qt-everywhere用的4.7.3,用vs2008命令行弄得。
configure用的open source版
结果有些小问题,我是想编译出wm6.5.3的平台用,我发现用了
setcepath.bat脚本后,我自己添加的6.5的sdk在nmake时根本没用,而是用的6.0的sdk库和include文件,
而6.0的include中缺少手势相关的头文件,就会报gesture.h no such file or directory 的错误,让编译目录从
我自己手动设置的wm6.5变为6.0的动作是setcepath.bat脚本完成的,尽管不知道为什么要这样。
留下备忘,下次再仔细研究研究。
posted @ 2012-02-17 18:46 ewre 阅读(1069) | 评论 (0)编辑 收藏
在perl中,字母比较运算符(eq,ne,lt,gt,le,ge)将做字符比较。
数学符号比较运算符将做数字比较。
posted @ 2012-01-31 16:31 ewre 阅读(207) | 评论 (0)编辑 收藏
linux shell 在作为linux系统的shell程序参与“用户内核交互”的过程中逐渐发展出了前大的文本处理能力。
流处理能力:

shell的大多数字符处理应用其实指的是流处理应用,gnu在对sed介绍时的题目是:sed,a stream editor。
流的概念用shell写脚本的人都会很熟悉,管道符,cut,split,awk,sed等等的处理程序的处理对象是流对象,
而非字符串。这也是linux shell在字符串处理方面和其它脚本语言的较大差别。
仔细看过win cmd和linux bash 脚本的人都会上注意到脚本中有很多echo语句,echo一个很重要的作用是把
脚本中的变量转换成流对象,这样上述工具才可以起作用。

下面就分两类对shell字符处理做下总结:
(测试环境cygwin,gnu bash 4.1.10)

1,流的方式。
这一类的处理模式是:
echo $var|whaterver ur stream editor command
首先把你要处理的变量echo一下变成一个流对象,然后用管道符传递它给后续的处理函数,这些函数以sed和
awk较常见。先说awk,awk的命令形式是:
awk -'ur delimitor' '{ur command for the awk output}'
-F指定域分隔符,awk以指定的分隔符为界,分隔输入流,将分割结果存储在 $1,$2,$3等shell系统变量中.
例:
= "ur_apple"
echo $a 
| awk -F'_' '{print $1}' #以下划线分隔ur_apple
将输出
ur

再来sed。sed来源于stream editor。他确实是一个stream editor,而且非常强大,这里只做简单介绍。
sed命令形式:
sed OPTIONS [SCRIPT] [INPUTFILE]
options 一般设为 -e,及脚本执行,区别于脚本文件执行。如
echo $a|sed --'p'
-n选项指定pattern空间的内容不输出到屏幕,关于sed如何工作请参看http://ss64.com/bash/sed.html
其实sed更多的用法适合正则表达相结合。

下面说下非流方式的字符串处理,这是一种命令替换方式。
= "ur_apple"
echo ${a
%%_apple}
这种方式的形式是:
${var%%}
${var%}
${var##}
${var#}
百分号形式去尾,井号形式去头,另有最长匹配和最短匹配的区别。如
b="ur_appleur_apple"
echo ${b
%_a*ple}
echo ${b
%%_a*ple}
echo ${b#u*r}
echo ${b##u*r}
输出
ur_appleur
ur
_appleur_apple
_apple
posted @ 2012-01-13 18:15 ewre 阅读(1255) | 评论 (0)编辑 收藏
最近在用ftp从ncbi倒腾东西,东西很多,自动化好一些。

Sometimes I want to FTP a file from one machine to another. Usually, I can do the transfer interactively, but every so often, I would like to have a shell script do the file transfer. This task has eluded me in the past, but I finally figured it out. I've not seen this particular trick documented in the past, so I submit it for your approval.

The Problem

The problem I always encountered in scripting ftp transfers involved getting a password to the ftp server. Typical ftp client programs under Unix, Linux, Solaris and NetBSD all read the ftp password from /dev/tty.

Example (non-working) script

#!/bin/sh 
HOST
='ftp.users.qwest.net' 
USER
='yourid' 
PASSWD
='yourpw' 
FILE
='file.txt'  
ftp $HOST 
<<END_SCRIPT 
user $USER $PASSWD 
put $FILE 
quit 
END_SCRIPT 
exit 
0 
  

The above script will just hang if run in the foreground (in an xterm), or if run in the background (from a cron job), it will fail to perform the work of transferring file.txt.

/dev/tty names a strange, magic device. Each process (more strictly each process group) has a different /dev/tty, and you can not naively make ftp clients read the password from some non-magic, yet convenient source, like a "here document". When run in an xterm, the script above appears to hang because it reads the password from /dev/tty. The xterm constitutes the script's /dev/tty, so the script waits for keyboard input.

Example Working Script

#!/bin/sh 
HOST
='ftp.users.qwest.net' 
USER
='yourid' 
PASSWD
='yourpw' 
FILE
='file.txt'  
ftp 
-n $HOST <<END_SCRIPT 
quote USER $USER 
quote PASS $PASSWD 
put $FILE 
quit 
END_SCRIPT 
exit 
0 
The Tricks Getting the password to the ftp server without having the ftp client program read the password from /dev/tty requires two tricks: 
  1. Using the -n option on the ftp client program to prevent the ftp client from trying to log in immediately. That way, the ftp client does not ask for a user ID and password. No use of /dev/tty.
  2. Use the ftp client program command quote to send user ID and password to the ftp server.

Further Refinements

The above sh script will spew lots of ftp client output to standard output. Even if everything works perfectly, the user running the above script will see lots of incomprehensible text scrolling by quite rapidly. One refinement would send output to different places:
ftp -n $HOST > /tmp/ftp.worked 2> /tmp/ftp.failed <<END_SCRIPT 
One could further refine error handling by acting on the ftp client program's exit status:
ftp -n $HOST > /tmp/ftp.worked 2> /tmp/ftp.failed <<END_SCRIPT 
blah blah 
END_SCRIPT 
EXITSTATUS
=$?  
if [ $EXITSTATUS != "0" ] 
then     
# handle the error fi  
 

Except that the above doesn't always work - most FTP clients always exit with a status of 0. This leads to ugly "false negatives": the file transfer fails, but the script doesn't detect the problem.

One way to verify that a file transfer took place - transfer it back:

 
#!/bin/sh  ftp -<< END_SCRIPT 
open $
1 user $2 $3 
put $
4 
get $4 
retrieval.$$ 
bye 
END_SCRIPT  
if [ -f retrieval.$$ ] 
then     
echo 
"FTP of $4 to $1 worked"     
rm 
-f retrieval.$$ 
else     
echo 
"FTP of $4 did not work" 
fi

Regular FTPs there and back of large files can consume a lot of time.

Control of ftp by a shell script

One obvious improvement would have the ftp client program controlled by the shell script. I don't think that would comprise an impossible task, but I also don't think that it would have much value. Scripting ftp transfer using expect might cause you less pain.


Alternative #1

I saw a second way of doing this in a usenet article:

#!/bin/sh 
USER
=userid 
PASSWD
=userpw 
ftp 
-n f2dev <<SCRIPT 
user $USER $PASSWD 
binary 
get some.file 
quit 
SCRIPT 
 

It still uses the "-n" trick, but it sends user ID and password in the same "user" command.


Alternative #2

Use a .netrc file

Linux, Unix and BSD users have the alternative of using a .netrc file. The ftp man page documents the format of .netrc. To accomplish the task of using ftp in a shell script you would have to fill out a .netrc file something like this:

 machine something.else.com login myid password mypassword  

ftp demands that .netrc not have group or world read or write permissions:

 $ ls -l .netrc -rw-------    1 bediger  users          51 Dec 16 13:30 .netrc  

Using a .netrc file has a few problems that may or may not prevent you from using it.

  • A shell scripkt that does FTP using .netrc is no longer self-contained. You have to keep track of two files, which means that bugs can be less than obvious.
  • ftp reads it's user ID's .netrc. If you develop your script under a given user ID, then put it in production under a second user ID, you have to coordinate .netrc file contents between those two user IDs.

Alternative #3

Apparently, the Ckermit program from Columbia University understands FTP. You could use Ckermit to script FTP transfers. This looks to have advantages and disadvantages. On the "pro" side, it appears that Ckermit can exit on various errors, like unknown user IDs, or bad passwords. On the "con" side, you have to have Ckermit. I don't recall that it had a too onerous install, but it doesn't come with many Linux distros these days, and it probably doesn't come with any vendor Unix.


需要注意的一点是,如果在脚本中使用mget命令,需要在ftp时加上-i 选项,这样可以关闭 interactive prompt,不然悲剧.

阻止连接超时: 可以修改服务器端ssh配置文件;修改客户端ssh配置文件(/etc/ssh/ssh_config serveraliveinterval 和 clientalivecountmax);直接ssh -o ServerAliveInterval=x user@ip
这是找到的一个参考,转帖注明出处
posted @ 2012-01-12 11:10 ewre 阅读(344) | 评论 (0)编辑 收藏
R的data frame本质是一种列表。
df的引用,尤其是列引用和列表存在很大相似之处:
1 采用df[["colname"]]的方式引用
2 采用df$colname的方式引用
R的data frame又有二维数组的形式,所以也可以这样引用:
df[,1:2]
posted @ 2012-01-08 14:24 ewre 阅读(566) | 评论 (0)编辑 收藏
参见wiki.
特别的是:
若A,B 相互独立

P(AB)=P(A)*P(B)
另外,由条件概率知:
P(A∩B)=P(A|B)*P(B)=P(B|A)*P(A)
所以
P(A)=P(A|B),P(B)=P(B|A)
在处理加法原理 乘法原理 事件相容 独立一类的问题时,
应当特别小心,因为具体的问题很容易迷惑我们对其相应
的模型的判断与认定。
posted @ 2012-01-05 10:36 ewre 阅读(197) | 评论 (0)编辑 收藏
代码如下:
char* str = "std";
puts(str);
以前老觉得,既然c里面关于字符串的机制是:字符数组,且数组名便是数组起始地址的指针名。那么如果我
puts(*str);
应该输出一个s字符才对。试了很多次都不对,今天猛然发现是我错了,错在用错了函数
putchar(*str);
会如我所想。所以,有时候,我们会按照自己的意愿去关注一个问题的一个方面,但是问题却得
不到解决这是因为我们一开始不经意就自己加了一些自认为正确但是错误的限定条件,puts就是
用来输出字符串的,你给他一个字符它肯定不买你的帐。
posted @ 2012-01-03 17:18 ewre 阅读(193) | 评论 (2)编辑 收藏
R中data.frame和matrix是有区别的。data.frame在存储时默认stringAsFactors=T,
这时,当你用rbind去bind多个df成为一个时,如果存在字符列且值不唯一,则R会warning
因子水平不唯一,并且将你的不唯一的字符强制转为NA(默认),所以操作df时注意先设置
stringAsFactors选项。
posted @ 2011-12-26 19:21 ewre 阅读(1450) | 评论 (0)编辑 收藏
假设实验设计有2个factor,每个factor分别有2,3个level,要将这些可能的level的组合一一列举。
也可以这样考虑:存在一个2位数,这个两位数每个位的进制分别为相应factor的level数,枚举这个两位数。
R脚本实现:
进位函数
carry<-function(max.num.vec,start.vec){
    #A
<-matrix()
    A
<-start.vec
    
while(T){
        #cat(start.vec,
"\n")
        start.vec[length(start.vec)]
<- start.vec[length(start.vec)]+1
        i
<-length(start.vec)
        
while(i>1){
            
if(start.vec[i] > max.num.vec[i]){
                start.vec[(i
-1)]<-start.vec[(i-1)]+1
                start.vec[i]
<-start.vec[i]-max.num.vec[i]
            }
            i
<-i-1
        }
        rbind(A,start.vec)
->A
        #cat(start.vec,
"\n")
        
if(length(max.num.vec[max.num.vec == start.vec]) == length(max.num.vec)){
            #cat(start.vec,
"\n")
            #colnames(A)
<-seq(1:length(start.vec))
            #rownames(A)
<-seq(1:prod(max.num.vec))
            
return(A)
        }
    }
}
进制(分别为3进位和2进位):
max.num.vec<-c(2,3)
起始值设为111
start.vec<-c(1,1)
运行函数
carry(max.num.vec,start.vec)
          [,1] [,2]
A            1    1
start.vec    1    2
start.vec    1    3
start.vec    2    1
start.vec    2    2
start.vec    2    3


得到因子组合情况。
posted @ 2011-12-23 18:13 ewre 阅读(350) | 评论 (0)编辑 收藏
最近在看GSEA的1.0源码,对order仔细的看了一下,觉得应该写下来。
order的官方说它返回一个能将参数排序的permutation。
order的行为是:(A)返回新有序列对应位置的元素在原序列中的索引号,也就
是位置;有些情况下我们会认为order(B)返回原序列对应位置的元素在新序列
中的位置:秩(rank),这是不对的,两者不等价。
例如
> a<-sample(seq(1:10),5)
> a
[
11 4 9 8 3
> a.order<-order(a)
> a.order  
[1] 1 5 2 4 3   #可以看到返回值是(A)所述的情况,(B)所述的情况为 1 3 5 4 2
(B)所述的情况可以用下面的两种操作得到
> a.sort<-sort(a)
> match(a,a.sort)
[
11 3 5 4 2
> rank(a)
[1] 1 3 5 4 2
同时order函数等价于
> match(a.sort,a)
[1] 1 5 2 4 3
不要搞混
有趣的是:1,不论一个vector A有序还是无序,
               执行A[order(A)]将得到sort(A)的结果。
               也就是说,在操作上,vec[order(vec)]与
               sort(vec)等价。
            2,order运算性质:
               order(a) ∈(1,length(a))
               奇数次order运算的结果相同,偶数次order运算结果相同
> a<-sample(seq(1:10),5,replace=F)
> sort(a)
[
11 3 4 5 8
> a[order(a)]
[
11 3 4 5 8
 order(a)
[1] 4 1 2 5 3
> order(order(a))
[1] 2 3 5 1 4
> order(order(order(a)))
[1] 4 1 2 5 3
> order(order(order(order(a))))
[1] 2 3 5 1 4

总结:
order <==> match(a.sort,a)
rank  <==> match(a,a.sort)
posted @ 2011-12-08 11:34 ewre 阅读(200) | 评论 (0)编辑 收藏
R里面数字取整相关的操作有一组函数:
ceiling ;floor;trunc; round; signif
ceiling返回对应数字的'天花板'值,就是不小于该数字的最小整数
a<-(1,2.5,3.2,3.5,3.6)
> ceiling(a)
[
11 3 4 4 4
floor与ceiling相对,返回'地板'值,即不大于该数字的最大值
floor(a)
[
11 2 3 3 3
由于一个整数不大于且不小于它本身,所以,对于整数来说,ceiling和floor都将原样返回。
> b<-c(3,4,2,5,3,2,4,5,23,23,2,45,23)
> ceiling(b)
 [
1]  3  4  2  5  3  2  4  5 23 23  2 45 23
> floor(b)
 [
1]  3  4  2  5  3  2  4  5 23 23  2 45 23
trunc的特性是'向零截取', 也就是说对于一个数字a,它将数轴分成两侧,
trunc(a)将返回数轴上包含数字0的那一侧离a最近的那个整数,例如:
> a
[
11.0 2.5 3.2 3.5 3.6
> trunc(a)
[
11 2 3 3 3
> b<-c(-1,-2.5,-3.2,-3.5,-3.6)
> trunc(b)
[1] -1 -2 -3 -3 -3
round是R里的'四舍五入'函数,具体的规则采用banker's rounding,即四舍六入五留双规则(wiki)。
round的原型是round(x, digits = 0), digits设定小数点位置,默认为零即小数点后零位(取整)。
> c<-c(1.4,1.6,1.5,2.5,2.51)
> round(c)
[1] 1 2 2 2 3
signif是保留有效数字的函数。常用于科学计数。
posted @ 2011-12-07 11:41 ewre 阅读(2147) | 评论 (0)编辑 收藏
bash的大括号(braces)在用法上共有三大类:
parameter subsitution,也叫parameter expansion-最广泛的一类
brace expansion-也是常用用法
code block-不是很常用
另外,还有一些其他用法。

parameter substitution是我们经常使用的功能,当然可能是无意的。
具体的内容比较多,参见此处
http://tldp.org/LDP/abs/html/parameter-substitution.html#PARAMSUBREF
参数替换,字面理解“用参数替换(原来的变量)”,主要用在参数的输入场合,根据参数是否定义采用不同的值。

第一类形式:
${param:-default};${param-default}
若param未定义则令整个表达式的值为default。
${param:-default};${param-default}
若param未定义则令param的值为default。
${param:+default};${param+default}
若param已定义则令整个表达式的值为default。
${param:?-errormsg};${param?errormsg}
若param未定义,print errormsg,退出当前shell脚本,返回值为1。

注意,以上操作:*与*的区别在于它们怎么解释“param定义与否”:
:*认为"已定义"必须是“定义了并且已经赋值,该值不是null”
*认为"定义"就是“至少有前向声明",已经声明但未赋值(此时值为null)也被认为"已定义”

例如:
a=subs
b
= #b被声明,但未赋值,这时,两种形式的变量替换行为是不同的
echo{b
-$a}

echo{b:
-$a}
subs

需要指出的是:最后一项在脚本的错误控制中用很大的用途。

第二类:
${#var}:将获得(字符串)变量长度, ${#},${@}将获得位置参数的个数,位置参数是命令行启动脚本是传递给脚本的参数
表。
${var#Pattern},${var##Pattern}
${var%Pattern},${var%%Pattern}
这两组就是俗称的变量的”掐头去尾“,非常强大,可以做正则匹配(将Pattern存为一个变量,然后${var%%$pattern_var}就好了),掐头去尾具体规则参看
http://www.cppblog.com/ewre/archive/2012/01/13/164132.html

另外一种比较有用的处理方式就是substring replacement-子串替换。
形式:
${var/pattern/replacement}
${var//pattern/replacement}
区别在于上面的做first match替换,下面的做全局替换。例:
str="abcdabcd"
${str
/abc/m}
${str
//abc/m}
将输出
mdabcd
mdmd
与之相似的是prefix与suffix替换,形式结合了掐头去尾和子串替换:
${var/#pattern/replacement}
${var/%pattern/replacement}

另外,有一点要说的是,为什么bash大部分场合令‘#’与‘开头‘关联,‘%’与‘结尾’关联,我看过一篇帖子说
这是因为普通键盘上#位于$(你可以理解为bash的变量)的前面也就是头部,%位于$的后面也就是尾部,所以
他们分别成了头尾的代替符号。
以上提到的就是parameter substitution的大部分内容,它们大部分用在参数处理的场合。

brace expasion比较有意思,感觉与perl正则表达式里的[]有些共通的地方。具体形式:
echo var_{a,b,c,d}
var_a var_b var_c var_d


code block和c是相似的,相当于一个匿名函数块,不过该函数块内部的变量
外部是可见见此处

后话:前面提到的大括号用法其实是shell在命令处理流程(shell operation)中的一些步骤。shell命令处理流程是这样的:
1,读入输入部分(从脚本或者字符串中)
2,把输入断成words和operators,切断是根据元字符(meta-character)进行的。
3,把2得到的结果进行解析,解析成简单和复合命令(simple and compound commands)
4,做shell expansion(上面说的一些用法就是属于这里的一些操作,shell expansion 包括        {}expansion,~expansion,variable expansion,command expansion等等)
5,重定向解析
6,命令执行
7,收集命令执行结果返回值并退出。
以上提到的words operators meta-character simple and compound commands 是shell的定义
术语,具体请参看此处
特别要指出的是meta-character在shell中有明确的定义:在unquoting的情况下用于分隔words,这和我们
在学习c语言时的ascii码里的那些称为元字符的控制字符以及正则表达式中提到的元字符不完全等同,shell中的
meta-characters是:
space tab  |  &  < > 这9个。
posted @ 2011-12-05 11:22 ewre 阅读(974) | 评论 (0)编辑 收藏
最近写程序时希望loop中出现error时next一下继续执行而不是退出loop,搜了一下tryCatch可以实现。
tryCatch(my.process.func,error = function() next)即可实现。不过貌似error handleing是个比较复杂的
系统,一时半会儿还参不透。
posted @ 2011-11-30 14:44 ewre 阅读(453) | 评论 (0)编辑 收藏
仅列出标题
共2页: 1 2 

导航

<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

留言簿(2)

文章分类

文章档案

最新评论

阅读排行榜