Friday, September 30, 2011

Perl function

split, join
$words = "the cat is white";
($w1, $w2, $w3, $w4) = split(/ /,$words);

结果是: the cat is white;
split可以写成这个样: (@words) = split(/ /, $words);

Join
$joined = join(" ", ("A", "black", "dog"));
print "$joined\n";
结果: A black dog #有


lc, uc, lcfirst, ucfirst

$words = "nz";
$words = lc($words);
all lower case,都要小写;

lcfirst第一个字母小写
$words = "ADADSFS";
$words = lcfirst($words);
print $words; 结果是aDADSFS;

length, substr, index, rindex

length("thursday"); 结果是8;

$string = "It was hot on thursday.";
substr($string, 14, 6);
从第14个字母开始(从0数),6个字母
结果是thursd;

substr($string, 14, 8, "tuesday");
结果是It was hot on tuesday.

$string = "There, here & everywhere";
$index = index($string, "here");
print "First 'here', position $index\n";
结果是: First 'here', position 1

rindex是last position;
$string = "There, here & everywhere";


Perl Hashes - reverse, sort, merge,


$hash{$some_key} = "value";

eg. $hash{name} = Raymond;
    $hash{age}  = 29;
    $hash{city} = Hamilton;

%hash = ();     #"%" 百分号就是hash的意思

$hash{name} = Raymond;
 $hash{age}  = 29;
$hash{city} = Hamilton;


print "@{[%hash]}\n";
结果是: city Hamilton name Raymond age 29

单个hash值
print "$hash{city}\n";

如果key有多余一个words 就要用'', $hash{'my name'} = "Raymond nz";


另一种创建hash方法
%hash = (
   'name', 'Raymond',
   'age', '29',
   'city', 'Hamilton'
);

%hash = qw(
 name john
 age 28
 city Hamilton
);

looping hash
%hash = qw(
  food sandwich
  desert donut
  drink coke
);

while(($key, $value) = each(%hash)){
  print "$key --- $value\n";
}

foreach $key (keys %hash){
  pring "$key\n";
  $value = $hash{$key};
  print "$key => $value\n";
}

if(exists($hash{animal})){
  print "Key does exist!\n";
}else{
  print "key doesn't exist!\n";
}
结果:key不存在

defined 和 exists的区别
defined检查有没有值value, exists检查又没key

Reverse, sort, merge
reverse: 把key变成value,把value变成key

%hash = qw(
  food sandwich
  desert donut
  drink coke
);
%reversed = reverse %hash;
foreach $key (keys %reversed){
 print "$key - $reversed{$key};
}

sort:
foreach $key(sort key %hash){
print "key\n";
}

merge:
%hash2 = qw(
 drink coffee
 sugar 1
 milk yes
);

%hash3 = (%hash, %hash2);
print "$hash3{drink}\n";  结果是coffee;

Perl -- uc, grep, $_

@array = (1..10);
map {$_ = $_ * 2}(@array);
结果是@array里的每个元素都乘以2.

@array = qw(apple orange banana plum);
map {$_ = uc($_)} (@array);
#uc 是大写字母

@array = (1..10);
@array2 = grep{$_ > 5} @array;
取array里大于5的元素;

@array = qw(apple orange banana plum);
@array2 = grep{$_ =~ "n"}@array;
显示包含字母"n";

Perl -- Loop, foreach, if, unless, while

1. foreach

@array = qw(red orange blue green);

$count = 1;
foreach $color (@array)
{
     print "Color $count: $color \n";
    $count++;
}

@numbers = {1..10};

foreach $number (@numbers)
{
$number = $number + 10; //add 10 to $number
print "$number \n";
}

2. if

$number = 10;

if($number > 10)
{...}
elseif($number < 10)
{...}
else{...}

unless($number == 20)
{
 ...
}else{
 ...
}

3.

$number++; 后计算;
++$number; 先计算后赋值;

4.立方

$sum = 2 ** 3;

5. mod
$sum = 10 % 3;
the result is 1;

6.
use integer
$sum = 10 / 3;
print $sum;
the result is 3;

7. string equal use "eq" and number use "=="
if ($word eq "hel"){...}

not equal use "ne" and number use "!="

8. while


$count = 1;
while($count <= 10){
 ...
$count++;
}

9. until

$count = 0;
until(){

$count++;
}

Perl -- Array

Array

1. 输入array

@array(1,2,3);
@array("one", "two", "three");
简单的array建立
@array = qw(red blue organ white);
qw相当于“”,

简单方法建立数字array
@numbers = (1..5); 这个相当于@numbers(1,2,3,4,5);


2. 从右边添加或减少

@array("one", "two", "three");
push(@array, "one"); 添加
结果("one", "two", "three", "one");

@array("one", "two", "three");
pop(@array); 减少
结果("one", "two");

@array("one", "two", "three");
如果是 $popresult = pop(@array);
$popresult是"three";

3. 从左边添加或减少

@array("one", "two", "three");
unshift(@array, "zero");
结果("zero", "one", "two", "three")

@array("one", "two", "three");
shift(@array);
结果("two", "three");


4. 合并array
@array1("one", "two", "three");
@array2("four", "five", "six");
@array3(@array1, @array2);
结果:@array3 is ("one", "two", "three", "four", "five", "six")

5. 反向排列reverse:
reverse(@array3);
结果: @array3 is ("six", "five", .....,"one");

6. 排序 sort
sort(@array);

sort{$a <=> $b)(@array);

7. 反向排序
sort{$b <=> $a}(@array);

$a and $b 变量是perl默认的排序的变量.
如果是string排序 <=> 换成 cmb

sort{$b cmb $a}(@array);

8. 输出100次array
@array = (1) x 100;
print @array;

9. count array number
$#array

Friday, September 16, 2011

Thursday, September 1, 2011

Install memcache and APC on Mac OS X Server (snow leopard 10.6)

Looking to speed up your SocialEngine, Drupal, or other PHP/MySQL web site? The combination of APC and Memcache can really speed up sites based on these platforms. If you’re an admin of a server running Mac OS X Server (10.6), here’s how to install APC and memcache on Mac OS X server:
Download and install MacPorts from http://macports.org.
The following steps are performed in the Terminal:
Force MacPorts to update:
sudo port -v selfupdate
Now, install memcached:
sudo port install memcached
Set memcached to load on startup:
sudo port load memcached
Install the PHP5 module for memcache:
sudo port install php5-memcache
Copy the newly created shared object for memcache into Mac OS X’s default PHP5 extension directory:
sudo cp /opt/local/lib/php/extensions/no-debug-non-zts-20090626/memcache.so /usr/lib/php/extensions/no-debug-non-zts-20090626/
Install APC (Alternative PHP Cache):
sudo port install php5-apc
Copy the newly created shared object for APC into Mac OS X’s default PHP5 extension directory:
sudo cp /opt/local/lib/php/extensions/no-debug-non-zts-20090626/apc.so /usr/lib/php/extensions/no-debug-non-zts-20090626/
Next, you need to edit php.ini to add the extensions. Find the phrase Dynamic Extensions, and add:
extension=memcache.so
extension=apc.so

And finally, restart Apache:
sudo apachectl restart
To confirm installation, create a new PHP document called phpinfo.php with the following contents:
< ?php phpinfo(); ?>
Point your browser to that file. If all went well, you’ll see these blocks: from http://www.gigoblog.com/2011/01/23/install_memcache_and_apc_on_mac_os_x_server/