Friday, October 7, 2011

@"" in C#

1.
string file = "C:\\MyFolder\\MySubFolder\\MyFile.txt";
if use @
string file = @"C:\MyFolder\MySubFolder\MyFile.txt";

2.
<%= Html.TextBox("Title", Model.Title, new { size = 30, @class="myclass" } )%>
using a @ character because class is a reserved keyword in C#

Tuesday, October 4, 2011

Perl -- sent email

1) sent email

($message = $message) =~ s/<[^]*>//gs;  ##remove Html text 为了防止被放到spam文件夹

@rudewords = qw(rudeword1 rudeword2);
$rude = "no";
foreach $word (@rudewords)
{
  if($message =~ m/$word/gi or $subject =~ m/$word/gi)
  {
     $rude = "yes";
  }
}

unless ($rude eq "yes"){
   open(MAIL, '| /usr/lib/sendmail -t -oi'); ##sometimes not use lib and use sbin
   print MAIL <
   To: you\@yourserver.com
   From: them\@theirserver.com
   Subject: $subject

   $message

   EOF
   close MAIL;

   print "Thanks for your email!";
}

Perl -- print a block text; file test; chmod

1)
print <
test line one
test line two
...
EOF

2)-e 测试文件或文件夹是否存在
print -e "directory1/file.txt"; ##true while return 1;

if(-e "directory1/file.txt"){
print "file exists. \n";
}else{
print "file doesn't exists. \n";
}

-z 测试文件是不是零size true返回1
-s 测试none 零size 返回文件的尺寸数
-d 测试是不是文件夹 true返回1

3)chmod set file permissions
chmod 0644, 'directory1/file.txt';
chmod 0755, 'directory1';

@files = ("directory1/file1.txt", "directory1/file2.txt");
chmod 0644, @files; ##成批文件更改

#files 0644, 0666
#directorys 0755, 0777
#perl scripts 0755

Perl -- Text File

1)
open (FILE, "file.txt");
print ;
close(FILE);

open(handle名,"文件名");

2)

open (FILE, "file.txt") or die("Error");
print ;
close(FILE);

or die("Error")如果文件不存在,显示Error message。

3)
open (FILE, "file.txt");
@array = ;
close(FILE);

print @array;
foreach $line (@array)
{
chomp($line);
$line = uc($line);
print "$line";
}

chomp 去掉结尾的\n;
chop 是去掉最后一个字符;
uc Upper Case;

4)Write to Text file
open(FILE, ">file.txt");
flock(FILE,2);
print FILE "Hello\n";
close(FILE);

">" 是准备写入的开始
flock 是将文件锁上,以防止其他人操作同一个文件

5)append to text file
open(FILE, ">>file.txt");
print FILE "Hello\n";
close(FILE);

">>" 是准备添加到文件


is associated with the keyboard.Use this handle when you wish to read from the keyboard.
"<>" is operator directs Perl to read a line from the file handle specified within the angled brackets. 

6)Rename file
rename "file.txt", "file2.txt";

7)Copy file (use perl module)
use File::Copy;
copy("file.txt", "copied.txt");

8)delete file
unlink "file.txt";

9)
$file = "copied.txt";
$size = (stat $file)[7];

stat function不同的参数用于不用的作用,比如7代表的是文件的字节数。

10)Open Directory
opendir(DIR, 'directory1');
@files = readdir(DIR);
closedir(DIR);

readdir 读取文件夹里有什么文件和文件夹。

11)Change Directory
chdir 'directory2';  ##开始使用另一个文件夹
mkdir 'newdirectory'; ##文件夹改名
chdir '..';  ##返回上一级文件夹

12)Ddelete Directory
rmdir 'newdirectory';

13)Glob function for Directory
@files = glob('directory1/*');
print "@files\n";

结果打印出包括 文件夹名/文件名 directory1/file1.txt


@files = glob('directory1/*.txt'); ##only .txt 文件 

push (@files2, glob('directory1/*.bmp");
print "@files2\n";

push的作用是是上边直接付给@files一样的结果

直接print出来用join
print join("\n", glob('directory1/*.cgi'));

or

while ()
{
print "$_\n";
}



Perl -- Regular Expressions

A是检查是不是在首位,$是检查是不是在末位, "."点是替代一个字符,"+"代表one or more; "?"问号是代表0个,1个或多个
\w Match a "word" character (alphanumberic plus "_")
\W Match a non-word character
\s Match a whitespace character
\S Match a non-whitespace character
\d Match a digit character
\D Match a non-digit character

1)m match operator
$string = "hErE Is a STriNg";
if($string =~ m/here is a string/i)
{
 print "It's a match!\n";
}

m//  ##match 匹配
i    ##忽略大小写
可以写成 /.../; m!...!; m#...#;

2)s subsitution
$string =~ s/here/There/i;
结果是There Is a STriNg

s/ / /i ##substution用后一个字符串取代前一个字符串

3)
$string = "I see the road.";
$string =~ tr/r/t/;

结果是"I see the toad";
tr/r/t/; 用后一个字符取代前一个字符。

4)
$string = "Perl is coooooooooool!";
$string =~ s/o+/oo/g;
结果是"Perl is cool!";
s/o+/oo/g

5) number
$number = 123;
if($number =~ /^\d+$/){
print "it is a number!\n";
}
/^\d+$/检查是不是number

6)
$letters = "abc";
if($letters =~ /^[A-Za-z]+$/)
{print "Letter only!";}

7)去掉空格
$stirng1 = "     Leading whitespace.";
$string2 = "Trailing whitespace.    ";

$string1 =~ s/^\s+//;
$string2 =~ s/\s+$//;

Saturday, October 1, 2011

Perl

$random = rand(100);

sprintf "%.3f", $random;  ##小数点后面保留3位。

time

localtime(time); ##Thu Dec 23 13:25:02 2011
gmtime(time); ##Thu Dec 23 00:25:02 2011
time; ##1196347834

($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);


Subroutines
sub doit
{
......
}

$value = 10;
if($value<10){
doit;
}