perl資料操作
一次給多個變數賦值
($a, $b, $c) = ("aaa", "bbb", "ccc");
print "$a $b $c\n";
print "$a $b $c\n";
或者
@ls = ("aaa", "bbb", "ccc");
($a, $b, $c) = @ls;
print "$a $b $c\n";
($a, $b, $c) = @ls;
print "$a $b $c\n";
%longday =
(
"Sun" => "Sunday",
"Mon" => "Monday",
"Tue" => "Tuesday",
"Wed" => "Wednesday",
"Thu" => "Thursday",
"Fri" => "Friday",
"Sat" => "Saturday",
"All" => ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
);
print $longday{"Sun"}."\n";
print $longday{"All"}[1]."\n";
(
"Sun" => "Sunday",
"Mon" => "Monday",
"Tue" => "Tuesday",
"Wed" => "Wednesday",
"Thu" => "Thursday",
"Fri" => "Friday",
"Sat" => "Saturday",
"All" => ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
);
print $longday{"Sun"}."\n";
print $longday{"All"}[1]."\n";
遍歷雜湊(如環境變數)
foreach $key (keys %ENV) {
print "$key = $ENV{$key}\n";
}
print "$key = $ENV{$key}\n";
}
或
while(($key, $value) = each(%ENV)) {
print $key." = ".$ENV{$key}." = ". $value ."\n";
print $key." = ".$ENV{$key}." = ". $value ."\n";
}
陣列
給陣列賦值
@ls = "a" .. "z";
for(@ls){print $_, "\n";}
for(@ls){print $_, "\n";}
利用qw定義陣列,可以省去引號和都好
@ls = qw(aaa bbb ccc ddd);
for (@ls){print $_, "\n";}
for (@ls){print $_, "\n";}
qw允許用其它符號代替括號,甚至是註釋符號
@l = qw#aaa bbb ccc ddd#;
陣列擴容
@a = ("a", "b");
@a = (@a, "x");
print $a[2]; 輸出x
@a = (@a, "x");
print $a[2]; 輸出x
push和pop
push從下標加1開始追加,
pop從下標最大開始減
@ls = qw(a b c);
for(@ls){print $_, " ";}
print "\n";
push @ls, "l";
push @ls, "m";
push @ls, "n";
for(@ls){print $_, " ";}
print "\n";
@xyz = qw(x y z);
push @ls, @xyz;
for(@ls){print $_, " ";}
print "\n";
print " POP:", pop @ls;
print " POP:", pop @ls;
print " POP:", pop @ls;
print "\n";
for(@ls){print $_, " ";}
print "\n";
for(@ls){print $_, " ";}
print "\n";
push @ls, "l";
push @ls, "m";
push @ls, "n";
for(@ls){print $_, " ";}
print "\n";
@xyz = qw(x y z);
push @ls, @xyz;
for(@ls){print $_, " ";}
print "\n";
print " POP:", pop @ls;
print " POP:", pop @ls;
print " POP:", pop @ls;
print "\n";
for(@ls){print $_, " ";}
print "\n";
輸出:
a b c
a b c l m n
a b c l m n x y z
POP:z POP:y POP:x
a b c l m n
a b c l m n
a b c l m n x y z
POP:z POP:y POP:x
a b c l m n
排序和翻轉
@ls = sort qw(b a c); 得到 a b c
@ls = reverse qw(a b c); 得到 c b a
@ls = reverse qw(a b c); 得到 c b a
迴圈的時候
@a = ("asd", "bsd");
@a = (@a, "csd");
foreach (@a)
{
next if /b/; next相當於continue,這裡指跳過“bsd”
print; 迴圈體內,$_代表當前的迴圈內容,命令中如果沒有運算元,則預設是對$_操作
@a = (@a, "csd");
foreach (@a)
{
next if /b/; next相當於continue,這裡指跳過“bsd”
print; 迴圈體內,$_代表當前的迴圈內容,命令中如果沒有運算元,則預設是對$_操作
switch
case /^a/ {...}
case /^b/ {...}
case /^c/ {...}
}
}
幾種switch(last相當於break)
foreach (@a)
{
{
switch:
{
if(/^a/) {...; last switch}
if(/^b/) {...; last switch}
if(/^c/) {...; last switch}
}
或者
switch:
{
/^a/ && do {...; last switch};
/^b/ && do {...; last switch};
/^c/ && do {...; last switch};
}
}
使用者輸入
$line = <STDIN>;
print $line;
print $line;
透過STDIN的方式,獲取的輸入內容是帶有換行符的,因此,在實際使用中,需要用chomp,獲取不帶換行符的使用者輸入。
$line = <STDIN>;
chomp $line;
print $line;
chomp $line;
print $line;
或
chomp ($line = <STDIN>);
print $line;
print $line;
判斷變數是否被定義
if (defined($a)) {print "defined", "\n";} else {print "undefined", "\n";}
$a=123;
if (defined($a)) {print "defined", "\n";} else {print "undefined", "\n";}
$a=123;
if (defined($a)) {print "defined", "\n";} else {print "undefined", "\n";}
輸出
[root@server2 pl]# ./a.pl
undefined
defined
undefined
defined
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26239116/viewspace-2125607/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Perl讀寫檔案&字串操作字串
- Perl資料型別安全研究【翻譯】資料型別
- perl
- 資料庫操作資料庫
- 資料夾操作
- 資料庫操作·資料庫
- Perl語言學習(四)Perl控制結構
- perl的logwrapperAPP
- Perl&LWP
- perl指令碼指令碼
- 使用perl cpan
- perl 安裝
- 值得白嫖的資料庫常用操作語句彙總(資料庫、資料表、資料操作)資料庫
- Redis資料基本操作Redis
- MySQL 資料表操作MySql
- MySQL 資料庫操作MySql資料庫
- mongodb資料庫操作MongoDB資料庫
- 資料表相關操作
- 資料庫基本操作資料庫
- MySQL資料基本操作MySql
- MongoDB 資料庫操作MongoDB資料庫
- laravel 資料庫操作Laravel資料庫
- perl+xml+iniXML
- Perl Catalyst 倒出 ExcelExcel
- Perl常用語法
- 【Falsk 使用資料庫】---- 資料庫基本操作資料庫
- 前端資料操作總結前端
- Python 操作 SQLite 資料庫PythonSQLite資料庫
- 資料庫操作語句資料庫
- django操作多資料庫Django資料庫
- elasticsearch(二)---基本資料操作Elasticsearch
- SQLAIchemy對資料基本操作SQLAI
- postgresql 資料庫基本操作SQL資料庫
- MySQL之json資料操作MySqlJSON
- Mysql資料庫操作命令MySql資料庫
- 使用tensorflow操作MNIST資料
- PHP操作MySQL資料庫PHPMySql資料庫
- Python操作SQLite資料庫PythonSQLite資料庫
- python操作mongodb資料庫PythonMongoDB資料庫