logoruby


关与匹配的问题

Posted in ruby by wanguan2000 on the 07月 28th, 2008

a=”(R)-(+)-3-氯-1-苯基-1-丙醇”
b=”(R)-(+)-3-氯-1-苯基-1-丙醇”
puts “ok” if b =~ /#{a.to_s}/

/(R)-(+)-3-\346\260\257-1-\350\213\257\345\237\272-1-\344\270\231\351\206\207/ (RegexpError)

我怎么才能用匹配呢?里面有些需要\(\) 我怎么可以让他自己识别自动加\?

perl 里面

双引号字符串中的转义符

符号

含义

\n

换行

\r

回车

\t

制表符

\f

formfeed

\b

退格

\a

响铃

\e

escapeASCII中的escape 字符)

07

任何八进制值(这里是,007=bell(响铃)

\x7f

任何十六进制值(这里是,007=bell

\cC

一个控制符(这里是,ctrl+c

\\

反斜线

\”

双引号

\l

下个字符小写

\L

接着的字符均小写直到\E

\u

下个字符大写

\U

接着的字符均大写直到\E

\Q

non-word字符前加上\(自动加转义符号),直到\E

\E

结束\L,\E\Q

这里是一个\Q,\E的例子:

$word=”[box]“;

foreach (qw(in[bon] out[bom] whit[box])){

   if (/\Q$word\E/){

      print “$_ matched!\n”;

       }

}

这里只有第三个whit[box]匹配,如果不加\Q \E改为 if (/$word/){  三个都匹配,因为[]中括号被当成类别选择符号了,这样只要b,o,x三个字母有一个就可以,而不是要三个连续的box.

加了\Q \E的效果此处相当于  if (/\[box\]/){. “\”转义符的意思其实有两种相反用法,一种是后面的普通字符要转义成特殊意义,一种是将有特殊符号当成普通字符看待,要看具体上下文环境。

评论关闭

rails 1.2.6 undefined method `each’ for

Posted in rails by wanguan2000 on the 07月 28th, 2008

在差数据库中如果返回的结果是1个的话:   @username = Company.find(params[:q]) 这个@username就不是数组

在显示的页面中:for username in @username,就不会报错: undefined method `each’ for 。

我是这样处理的:

@username = Array.new
@username2 = Company.find(:conditions => ["id=?", params[:q]])
@username[0,0] = @username2

就是把@username2插入到 @username[0,0]这个空数组里面。如果@username2是数组的话结果还是数组。如果@username2不是数组的话,就插入变成一个数组。

这样@username就总是数组了。

find(:all)方法返回的就是一个数组,不管是一个还是两个,还是空,都是数组。所以就用:all 参数好了。

白白忙了一个晚上哈哈

评论关闭

rails 图书

Posted in rails by wanguan2000 on the 07月 18th, 2008

Web开发敏捷之道–应用Rails进行敏捷Web开发(第2版)
http://www.china-pub.com/34800

Ruby for Rails中文版
http://www.china-pub.com/34807

Rails Cookbook中文版
http://www.china-pub.com/36886

评论关闭

mysql 数据导入 utf8 第二版

Posted in mysql by wanguan2000 on the 07月 18th, 2008

utf8:

use chem_development;
delete from companies;
set names ‘utf8′;

load data local infile ‘/home/wanguan2000/companynot.txt’ into table companies
character set utf8
fields terminated by ‘\t’
(nation,
province,
area,
zip_code,
company_number,
credit_grade,
chinese_name,
chinese_short_name,
english_name,
english_short_name,
address,
contact_man,
phone,
fax,
email,
home_page,
attachment,
company_summary);

不设定字符:

load data local infile ‘/home/wanguan2000/companynot.txt’ into table companies
fields terminated by ‘\t’
(nation,
province,
area,
zip_code,
company_number,
credit_grade,
chinese_name,
chinese_short_name,
english_name,
english_short_name,
address,
contact_man,
phone,
fax,
email,
home_page,
attachment,
company_summary);

mysql> use mysql;
Database changed

运行该脚本文件:

mysql> source hi.sql
Query OK, 0 rows affected (0.08 sec)

评论关闭

mysql 数据导入

Posted in mysql by wanguan2000 on the 07月 17th, 2008

mysql -uroot -ppassword < path_to_import_file –default-character-set=utf8

shell>mysql -hxx -uxx -pxx database
mysql>set names utf8;
mysql>load data infile ‘/path_to_file/xx.csv’ into table xx ;
做类似上面的步骤即可

SELECT * FROM store_development.items i;

mysql -u root -ppassword < path_to_import_file –default-character-set=utf8

load data infile ‘D:\2.csv’ into table items;

load data infile ‘D:\2.csv’ into table items
fields terminated by ‘,’
lines terminated by ‘\n’;

评论关闭

mysql utf8

Posted in mysql by wanguan2000 on the 07月 17th, 2008

show variables like ‘character_set\_%’;
set names ‘utf8′;

create table bb(

id int not null auto_increment,
address varchar(100),
company_summary text,

primary key(id)
)
ENGINE = MyISAM
CHARACTER SET utf8 COLLATE utf8_general_ci;

insert into bb(id,address,company_summary)
values(’1′,’撒旦’,'东方’),(”,’很快’,'文艺’),(’3′,’把’,'北大’);

评论关闭

mysql 常用命令

Posted in mysql by wanguan2000 on the 07月 17th, 2008

数据录入
insert into titles(title,publID,langID,year)
values(’My SQL’,1,2,2008),(’C++’,2,1,2001),(’Visual C’,3,3,2003),(’Oracle’,1,5,1987),
(’SQL’,2,4,2005),(’VB’,1,2,2003);

insert into publishers (publID,publName)
values(1,’chunfeng’),(2,’wenyi’),(3,’renmin’);

insert into authors(authID,authName)
values(1,’Jack’),(2,’Jackson’),(3,’Lucy’),(4,’Mary’),(5,’Rebecca’);

insert into language(langID,langName)
values(1,’Chinese’),(2,’English’),(3,’German’),(4,’Japanese’),(5,’Spanish’);

insert into rel_title_author(authID,titleID)
values(1,1),(4,2),(5,3),(3,4),(2,5),(5,6);

补入新纪录(包括一名新作者和一本新书)
insert into authors(authName)
values(’Isza’);
select last_insert_id();
insert into titles(title,publID,langID,year)
values(’pearl’,1,3,2003);
select last_insert_id();
insert into rel_title_author(titleID,authID)
values(8,6),(8,7);

显示结果
select publID from publishers where publName=’chunfeng’;

SELECT title,year,authName,publName,langName
from titles,publishers,authors,language,rel_title_author
where titles.publID=publishers.publID
and titles.langID=language.langID
and rel_title_author.titleID=titles.titleID
and rel_title_author.authID=authors.authID
order by title;

select authNAme from authors order by authName desc;(desc 表示倒序)

select * from titles; (*表示全部)

改变数据值
update titles set langID=2 where titleID=7;
update titles set title=’php’ where publID=4;

删除记录
delete titles from titles where year=0;(注意限定条件不要忘了)

禁止、打开外键约束条件
set foreign_key_checks=0;
set foreign_key_checks=1;

录入中文数据
set names ‘utf8′;
show variables like ‘character_set\_%’;
再建表 改变table options 为utf8
录入数据

待确定
alter table titles add  title unique;

数据库中数据表导出
mysqldump -u root -p mylibrary > c:\testdumpfile2.sql(在dos 命令下,具体见onenote)
mysqldump -u username -p db_name >存盘路径和命名

数据库中数据表导入
mysql -uroot -hlocalhost -p mylibrarycpy < testdumpfile2.sql
mysql -u username -hlocalhost -p target_database_name < 文件名.sql

评论关闭

ruby rails 全文搜索引擎 Ferret

Posted in rails by wanguan2000 on the 07月 15th, 2008

Ferret 来自著名的java搜索引擎 Apache Lucene

安装:

sudo gem install ferret

安装插件:acts_as_ferret

rails script/plugin install svn://projects.jkraemer.net/acts_as_ferret/trunk/plugin/acts_as_ferret

命令:

acts_as_ferret :fields => [:title,  :author_names]

评论关闭

ruby 关于 gets 和=~匹配的问题

Posted in ruby by wanguan2000 on the 07月 14th, 2008

假设一个文件: 只有一行:

1:CDK9

当你用file.gets它会在后面自动加上一个换行符号的。所以要用chomp!

puts line if line =~ /#{line2}/

line = “CDK9    verb:targets    cyclin T1    Pubmed:11689688″
#line2 = “CDK9″
file = File.open(”up1.txt”)
while line2 = file.gets
print line2
print line2
line2.chomp!
print line2
print line2
puts line if line =~ /#{line2}/

end

关于匹配,一般文件下都会有最后一行,空白行,chomp之后会与任何匹配。可以手动删除或加方法判定。 && line2 =~ /./就可以了。

puts line2 if line =~ /#{line2}/ && line2 =~ /./

评论关闭

rails lesson 1

Posted in rails by wanguan2000 on the 07月 8th, 2008

windows下: action = “\look\at”

linux下: action = “/look/at”

<form action = “/look/at” method=”post” >
Please enter your name.
<br>
<input type=”text” name=”text1″>
<br>
<br>
<input type=”submit”>
</form>

html 模板:

<html>
<head>
<title>Reading data from text fields</title>
</head>
<body>
<br>

</body>
</html>

评论关闭
下一页 »