Day 4
.Conditional statement -條件陳述
.Iterative (Looping) Constructs - 迴圈
.File I/0 - 檔案的輸入與輸出
.Subroutine constructs - 如何寫副程式
4條件陳述、迴圈要檔案輸出/輸入與副程式撰寫
P64
什麼是真的(Truth)
.在Perl裡,truth一定以scalar的方式呈現
.除了" “ 及” 0"以外的字串都是True
.除了0以外的數字都是true
.任何參照(reference)都是true
.任何 undef()的值都是false
4-1什麼是true 以及false?
範例:
0 #會是一個字串,所以是false
1 #會是一個字串,所以是true
10-10 #10-10 = 0,然後再被轉換成字串"0",所以是false
0.00 #結果是0,然後再被轉換成字串"0",所以是false
“0” #會是一個字串,所以是false
“” #空的字串,所以是false
“0.00” #該字串並不是空的,也不完全是"0",因此是true
“0.00”+0 #計算結果後為0,所以是false
\$a #參照,所以是true
undef() #不被定義,所以是false
P68
.C style 的for迴圈
.接受三個expression
1、第一個:設字迴圈中使用變數的初始值
2、第二個:檢查迴圈變數值
3、第三個:改變迴圈變數值
for ( $sold = 0 ; $sold < 10000; $sold += $purchase )
{
$available = 10000 - $sold;
print “$available tickets are available. How many would you like: ”;
$purchase = ;
chomp($purchase);
}
4-2-2迴圈 for ()
執行迴圈的順序:
1、第一次迴圈先讀取迴圈變數初始值(第一個expression),並檢查第二個expression是否為真,如是,則執行大括號內的敘述。
2、第一次迴圈結束以前(敘述執行完),執行第三的expression。
3、先檢查第二個expression是否為真,如是,則進行第二次迴圈;如不是則停止迴圈。
4、第二次迴圈結束前執行第三個expression。
for ( $sold = 0 ; $sold < 10000; $sold += $purchase )
{
$available = 10000 - $sold;
print “$available tickets are available. How many would you like: ”;
$purchase = ;
chomp($purchase);
}
P69
迴卷 -for (2)
.簡單的遞增方式
for ( 1 .. 10000 )
{
print $_, “\n” ;
}
P70
迴圈 - foreach
.使用機率比for 迴圈大的多
.用來處理一連串的scalar
foreach $user ( @users )
{
if ( -f “/home/$user/.nexrc” -
{
print “$user is cool… they use a perl-aware vi!\n”;
}
}
p71
迴圈 - breaking out
.跳出迴圈的方式:next, last
.用以改變迴卷執行敘述的流程
.next: 跳離以下敘述,直接執行另一個迴圈
.last:跳離整個敘述區塊
foreach $user ( @users )
{
if ($user eq “root” or $user eq “lp” )
{
next;
}
if ($user eq “special”)
{
print “found the special account.”,\“;
last;
}
}
4-2-4跳出迴圈 - breaking out
foreach $user ( @users )
{
if ($user eq "root” or $user eq “lp” )
{
next;
}
if ($user eq “special”)
{
print “found the special account.”,\“;
last;
}
}
print "Enter string: ”;
While (($line = ) =~ /\d/) # 一般來講,代表鍵盤的輸入
{
print “$line\n”;
print “Enter string: ”;
}
print “Enter string: ”;
while (($line = ) ! ~ /\d/) # 只要$line沒有數字的話,程式繼續跑
{
print “$line\n”;
print “Enter string: ”;
}
print “Enter string: ”;
while (($line = ) !~ /\squit\s$/i)
{
print “$line\n”;
print “Enter string: ”;
}
P73
Perl 內建何留陣列變數 - @ARGV
.指令行各部份的名稱:
#exmaple.pl -d foo bar
#example.pl 程式或指令名稱,可以用保留變數$0代表
-d:參數
foo bar: 引數(argument)
.每個引數會成為@ARGV中的元件
.依照順序收入@ARGV中
.以上面例子而言:$ARGV [ 0 ] 是foo
4-2-5 Perl 內建的保留陳列變數- @ARGV
P74
檔案存取 - File Access
.File Handle - 可給任何名稱,通常給大寫
.開啟檔案 -
open (File Handle, 檔案所在位置)
.讀取檔案File Handle的內容 -
.把資料寫入新檔案的方式 - print FileHandle變數
.close (File Handle) - 關閉檔案
4-3 檔案存取File Access
p75
自訂副程式 - subroutine
.宣告副程式:
sub 函式名稱
{
…
}
.副程式內,接收輸入參數的預設陣列變數: @_
.區域(local)變數宣告:my
.函式回傳值的方式:return <值>
4-4自訂副程式
################檔案存取###########
die “Usage: $0 in file outfile\n” if @ARGV ! = 2;
open( INFILE, “$ARGV[0]”) || die “ Cannot open $ARGV [0] for reading. \n”;
open( OUTFILE, “$ARGV[1]”) || die “ Cannot open $ARGV [1] for writing. \n”;
@lines = ; #### 一次讀完整個檔案;每一行就是陳列的一個元件
上面的方式也可以寫成
push ( @line,$_) while ;
foreach $line ( @lines)
{
print OUTFILE $ line if $ line =~m/#/;
}
close ( INFILE );
######## 在開啟一次檔案前,無須關閉檔案
open ( OUTFILE, “$ARGV [1]” ) || die “Cannot open $ARGV [1] for reading. \n”;
print while (); #### 預設使用$_變數!
close ( OUTFILE );
################純量及陳列的函式參數################
@list = ( “foo”,“bar”,“snafu”,“ouch”,“opps”);
$oneline = run_together(@list);
print “\n$oneline\n”;
@address_parts = (“John Doe”, 123 Main Street", Nowhere, CA 90000");
print_envelope( @address_parts );
sub runtogether
{
my ( @list ) = <a href=“twitter:@”>@_;
my ( $str, $sting );
$string = “”;
foreach $str ( @list )
{
$string .= $str;
}
return $string;
}
sub printenvelope
{
my ($name, $street, $city_state_zip ) = <a href=“twitter:@”>@_;
print “$name\n”;
print “$street\n”;
print “$city_state_zip\n\n”;
}
sub hadprint
{
my ( @list1, @list2) = <a href=“twitter:@”>@_;
my $element;
foreach $element ( @list1 )
{
print $ element, “\n”; #### 看看輸出代表什麼意義
}
}
課堂實作:請分析access_log.16的內容,分別找出IP,時間及URL的統計,並寫出一個副程式把結果仙到標準輸出
#!/usr/bin/perl
$total _hit = 0;
$dir = “/tmp/logs”;
$filename = “access_log.16”;
open( FILE, “$dir/filename”) or die “Can't open file!\n”;
while ()
{
chomp;
$_ =~ /.*?\s.?[.?:(\d{2}).?“(.?)/;
$ip { $1 }++;
$hour_hits{ $2 }++;
$url{ $3 }++;
}
close ( FILE );
print_hash ( %ip );
sub printhash
{
my ( %hash ) = <a href="twitter:@”>@;
foreach ( sort { &hash { $a } $hash { $b }} keys %hash)
{
print “$ => $hash { $_ }\n”;
$total hits += $ hash { $ };
}
}
2012年11月5日 星期一
2012年10月14日 星期日
使用者介面設計
什麼是使用者介面設計?
李毓修說:”使用者介面設計包括了:版面視覺設計、功能/內容資訊架構、使用者操作流程和互動方式、文案設計。”而什麼是使用者經驗呢?李毓修說:”其實使用者介面設計(UI),只是使用者經驗(UX)的一各部份,那什麼是使用者經驗(UX),在於了解或是接收了一個系統或者是一個服務後,對於這個系統或是服務的認識或了解的反應”而為什麼這個東西這麼重要,李毓修接這說道:”這個反應多半都會反應在這個平台或是這個品牌,進而使用者才會回過頭來重覆地使用這個平台、品牌或是服務。”
李毓修說過一個有趣的例子來闡述,使用者介面(UI)及使用者經驗(UX)。以旅館為例:當進入旅館要入住時,會進行Check in的動作與櫃台小姐進行接觸。這個接觸點(touch point)就是使用者經驗的開始,櫃台小姐的外觀、服務態度、服務內容的一切就是使用者介面(UI)。
Perl 學習手冊第六版。
最近在看Perl 學習手冊第六版,
中英對照著看,
必竟我不是學理工的,對於程式語言相關的專有名詞有時候會看不懂,
先看英文有個七八成的底,在看中文確定真正的意思,以免留下錯誤的印象。
在看的過程中有時候會發現有先拼音,或是排版及其他問題。
在這記錄下來,
Perl 學習手冊第六版,中文版第2012.05刷。
P26
錯誤:乘冪(exponentiaion)
正確:乘冪(exponentiation)
2012年10月8日 星期一
Mac Perl update方式 perl 5.12.3 to perl 5.16.0
5年沒有使用過perl,perl的基本方式早就忘光光。
最近工作需求,重新開始使用。
寫下一遇到的問題,及解決法式。
MacBook Pro, Mac OS X (10.7.5), 2.4 Core 2 Duo Unibody
Language first prefer Englsih
perl 基本的尋找功能指令:
1、打開terminal (/Applications/Utilities/Terminal)
2、想要找split的功能
一般只要輸入
$ perldoc -f split
就可以。
“
MacPower:~ mac$ perldoc -f split
No documentation for perl function ‘split’ found
“
竟然出現這樣No documentation for perl function ‘split’ found一般只要輸入
$ perldoc -f split
就可以。
“
MacPower:~ mac$ perldoc -f split
No documentation for perl function ‘split’ found
“
我的媽啊!我難到我退化到連功能鍵都不記得了嗎?!
3、快來查查
“
$ man perldoc
“
出現結果
Mac mini, Mac OS X (10.8.1), 2.5 i5
Language first prefer Englsih
見鬼了!出現了,這下可好玩了!
5、查查版本吧。都是 perl 5. 12啊!
倒底拿裡出問題了,在網上找了很多文件都沒發現有這樣的問題。
正打算放棄,反正我可以在Mac mini上查找就好了。
這時才發現,原來鬼遮眼
細細比較一下
找出原因了,
這是本版的問題
Mac OS 10.7 底下的是
This is perl 5, version 12, subversion 3 (v5.12.3) built for darwin-thread-multi-2level
(with 2 registered patches, see perl -V for more detail)
Mac OS 10.8 底下的是
This is perl 5, version 12, subversion 4 (v5.12.4) built for darwin-thread-multi-2level
(with 2 registered patches, see perl -V for more detail)
差只差在 subversion
只差一版就差這麼多嗎?!(淚奔~!!)
那找到問題就好解決了,更新吧!
更新方法:
1、先下載Xcode (https://developer.apple.com/downloads/index.action)
檔案有 1.5G左右。
2、把Xcode放到Applications內
打開,就會自動安裝
3、進入Xcode的preference
點選Downloads
更新方法:
1、先下載Xcode (https://developer.apple.com/downloads/index.action)
檔案有 1.5G左右。
2、把Xcode放到Applications內
打開,就會自動安裝
3、進入Xcode的preference
點選Downloads
選擇Command line Tools 安裝 點選Install
5、來看看結果吧!
“
$ perl -v
“
喔耶~!!!!
2010年6月11日 星期五
2010年5月15日 星期六
Feedly 在Mac底下的設定
Feedly已是大家都知道的閱讀器,



他的UI介面比Google Reader來得容易閱讀。
若不清楚Feedly是什麼請參照:
如同Mr./Ms. Days說的:
"如果純論功能,很難有其他軟體及得上Google Reader,所以feedly也沒打算硬來,你只要用Google帳號即可登入,feedly會直接吃你的Google Reader設定,但是以「雜誌化」的封面呈現。同樣的功能,只差UI,feedly做出來的效果簡直好到「失明」,跟Google Reader真的是雲泥之別"
差多少,請看官自行評論,本篇文章不是要討論UI的好壞。
Feedly在Firefox及Chrome有很好外掛。但在Safari底下卻沒有。
但是在rudis dot net有篇文章有詳細的說明,
如何設定讓Feedly可以用Safari觀看。
本篇是寫給自己作個記錄用。
1.在Feedly for Safari下載 Feedly for Firefox package
2.解壓縮這個檔案後,可以看到底下的圖。

其實到這裡只要打開Safari,登錄Google,點還檔案內容的safari.htm就可以用Safari看Feedly。
但是Mac User怎麼可能就這樣作罷呢?!
接下來才是重頭戲。
3.在~/Lirary/Application Support底下開一個Feedly新的檔案夾

4.把上面解壓縮的檔案全部放到新開的Feedly內
5.用點選Feedly內的safari.htm,之後就會出現Safari開啟的Feedly
6.Copy上面的website address
7 .再開啟Fluid,在URL貼上Copy的address
在Name的空白處寫上:Feedly
用Icon處選~/Lirary/Application Support/Feedly/feedly-128.png

8.按下Create後,在應用程式內可找到Feedly。
9.按下Feedly,就可以看到美美介面的Google Reader
訂閱:
文章 (Atom)