-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
2,365 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# The URL of the site. It is used in the atom feed, the sitemap, the Open Graph meta field of each page, and the robots.txt | ||
# No trailing slashes! | ||
url: https://es.code-maven.com | ||
|
||
# site_name is used in the atom feed and in the Open Graph meta field of each page | ||
site_name: Code Maven en Español con errores gramaticales | ||
|
||
# The source of the https://rust.code-maven.com site is in a public git repository | ||
# using the 'main' branch. | ||
# We wanted each page on the site to link directly to the source of that page. | ||
# The following 3 parameters help do this. | ||
# You can disable this feature by keeping `link_to_source` as `false`. | ||
# You can enable this feature by putting the correct repo link and correct branch name | ||
# and setting `link_to_source` to `true` | ||
repo: https://github.com/szabgab/es.code-maven.com | ||
branch: main | ||
link_to_source: false | ||
|
||
# Each page will have a footer as defined by the `footer` field that can contain markdown. | ||
footer: "[Code Maven](https://code-maven.com/) en ingles" | ||
|
||
# Each site has a /tags page generated from the tags in the individual pages. | ||
# The following parameters set the `title` of the page and the content of the `description` meta-tag. | ||
tags: | ||
title: Tags | ||
description: Articles about Foo Bar | ||
|
||
# Each site has a /archive page generated from all the pages, except the main page (the root index.md file). | ||
# The following parameters set the `title` of the page and the content of the `description` meta-tag. | ||
archive: | ||
title: Archivo | ||
description: Archivo de este sitio | ||
|
||
# If you have setup Google Analytics add your code here and uncomment the following field | ||
# google_analytics: G-BLABLA | ||
|
||
|
||
# Fields used by the email sender | ||
# TODO: Explain the whole email sending via `sendgrid` | ||
from: | ||
name: Foo Bar | ||
email: [email protected] | ||
|
||
# On each page show the list of pages that link to the current page. | ||
# Define the title of the section when the related pages are shown | ||
show_related: true | ||
related_pages_title: Otras páginas | ||
|
||
# Each page can have an `author` field in the front-matter containing the nickname of the author. | ||
# Here we list all the authors of the site. | ||
# The name will be displayed on the page | ||
# In addition there can be a folder called `authors` with makrdown files using the nicknames (so in the example it would be authors/foobar.md) | ||
# Those file can contain information about the author that will be displayed at the bottom of each page. | ||
# TODO: where is the image located | ||
authors: | ||
- name: Gabor Szabo | ||
nickname: szabgab | ||
picture: szabgab.png | ||
- name: Jeffrey Thalhammer | ||
nickname: thalhammer | ||
picture: thalhammer.png | ||
|
||
# The following section defines the menu. The "start" will be left-aligned the "end" will be right aligned. | ||
# TODO: Once we implement rtl site this will probably be the other direction too | ||
# TODO: Support also drop-down menus | ||
navbar: | ||
start: | ||
- path: / | ||
title: Casa | ||
- path: /tags/ | ||
title: Tags | ||
end: | ||
- path: /archive | ||
title: Archivo | ||
- path: /about | ||
title: About | ||
|
||
# search: Resultsos de buscada |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
title: "About Perl Maven" | ||
timestamp: 2012-05-02T00:01:41 | ||
published: true | ||
original: about | ||
archive: false | ||
--- | ||
|
||
|
||
Placeholder. Check out the [English version](https://perlmaven.com/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
--- | ||
title: "Perl 的布林值" | ||
timestamp: 2013-04-16T19:47:56 | ||
tags: | ||
- undef | ||
- true | ||
- false | ||
- boolean | ||
published: true | ||
original: boolean-values-in-perl | ||
books: | ||
- beginner | ||
author: szabgab | ||
translator: shelling | ||
--- | ||
|
||
|
||
Perl 沒有特定的布林值,但在 Perl 的文件中你經常會看到函數回傳「布林」值。 | ||
有時候這文件會說函式回傳真值或是假值。 | ||
|
||
哪個才是對的? | ||
|
||
|
||
Perl 確實沒有特定的布林型別,但凡純量皆是 — 如果用 if 來確認,非真值即假值。所以你可以寫 | ||
|
||
```perl | ||
if ($x eq "foo") { | ||
} | ||
``` | ||
|
||
你也可以寫 | ||
|
||
```perl | ||
if ($x) { | ||
} | ||
``` | ||
|
||
前者會確認 $x 變數的內容是否和 "foo" 字串一致 | ||
後者則會確認 $x 本身是真值或否。 | ||
|
||
## 哪些值在 Perl 裡是真值,哪些是假值? | ||
|
||
這相當簡單。讓我引述一下文件: | ||
|
||
<pre> | ||
數字 0,字串 '0',和 '',空串列 (),和 undef 在布林語境都是假值。其他值都是真值。 | ||
用 ! 和 not 來否定一個真值會回傳一個特別的假值。 | ||
當被估算為字串時它被當作 '',估算為數字時,,被當作 0。 | ||
|
||
引自 perlsyn 的 "Truth and Falsehood" 一節。 | ||
</pre> | ||
|
||
所以下列純量被認為是假值。 | ||
|
||
* undef — 未定義值 | ||
* 0 — 數字 0,無論你寫成 000 或是 0.0 | ||
* '' — 空字串 | ||
* '0' — 只包含一個羅馬數字 0 的字串 | ||
|
||
所有其他純量,包含下列,都是真值: | ||
|
||
* 1 和任何非零數 | ||
* ' ' — 包含一個空白的字串 | ||
* '00' — 兩個或許多數字零組成的字串 | ||
* "0\n" — 一個數字零接上一個換行字元 | ||
* 'true' | ||
* 'false' — 是的,即使字串 'false' 也是真值 | ||
|
||
我想這是因為 Perl 之父,[Larry Wall](http://www.wall.org/~larry/), | ||
有相當正向的世界觀。 | ||
他也許覺得世界上只有很少事情是很糟糕錯誤的。 | ||
大部分事情都是對的。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
--- | ||
title: "使用 grep 來過濾想要的值" | ||
timestamp: 2013-05-17T09:45:56 | ||
tags: | ||
- grep | ||
- filter | ||
- any | ||
- List::MoreUtils | ||
- <> | ||
- glob | ||
published: true | ||
original: filtering-values-with-perl-grep | ||
author: szabgab | ||
translator: h2chang | ||
--- | ||
|
||
|
||
Perl 內建的 <b>grep</b> 函式就像<b>過濾器</b>一樣。你可以給它一個串列以及一個要過濾的條件,它會回傳一個符回過濾條件的串列。 | ||
它是 UNIX和 Linux 指令中 grep 或是 egrep 的通式,不過我們不必真的去了解這些指令。 | ||
|
||
|
||
`grep` 函式需要兩個參數:一個程式區塊和一個串列。 | ||
|
||
串列中的每個值都會被傳到 `$_`裡,[ Perl 的預設變數](/the-default-variable-of-perl), | ||
然後這個程式區塊就會被執行。如果程式區塊回傳值為 `false`,那麼這個串列值就會被丟棄。如果程式區塊回傳值為 `true`, | ||
那麼這個串列值就會被保留並且當作回傳值。 | ||
|
||
請注意,在程式區塊和串列之間並沒有逗號。 | ||
|
||
讓我們來看看幾個例子: | ||
|
||
## 過濾數字 | ||
|
||
```perl | ||
my @numbers = qw(8 2 5 3 1 7); | ||
my @big_numbers = grep { $_ > 4 } @numbers; | ||
print "@big_numbers\n"; # (8, 5, 7) | ||
``` | ||
|
||
這裡的 grep 函式回傳大於 4 的值,並且把小於 4 的值給丟棄。 | ||
|
||
|
||
## 過濾檔案 | ||
|
||
```perl | ||
my @files = glob "*.log"; | ||
my @old_files = grep { -M $_ > 365 } @files; | ||
print join "\n", @old_files; | ||
``` | ||
|
||
`glob "*.log"` 會回傳所有在現在目錄中的 .log 檔案。 | ||
|
||
`-M $path_to_file` 會回傳這個檔案最後被變動的時間跟現在時間相差的天數。 | ||
|
||
這個例子就是要找出最後變動時間超過一年以上的檔案。 | ||
|
||
## 找出某個元素是否位在陣列中? | ||
|
||
`grep` 也可以使用在找出某個元素是否位在陣列中。比如,你有一組串列的名字,妳想要知道裏面是否有某個名字? | ||
|
||
```perl | ||
use strict; | ||
use warnings; | ||
|
||
my @names = qw(Foo Bar Baz); | ||
my $visitor = <STDIN>; | ||
chomp $visitor; | ||
if (grep { $visitor eq $_ } @names) { | ||
print "Visitor $visitor is in the guest list\n"; | ||
} else { | ||
print "Visitor $visitor is NOT in the guest list\n"; | ||
} | ||
``` | ||
|
||
這個例子中,我們把 grep 放在 [SCALAR 意境](https://perlmaven.com/scalar-and-list-context-in-perl)中。 | ||
在 SCALAR 意境中, `grep` 會傳回符合元素字串的數目。如果回傳 0, 代表這個表示式為 false。如果回傳正值,代表這個表示式為 true。 | ||
|
||
這個方法可以運作是因為它利用了意境這個方式,但是有人可能對這個比較陌生。讓我們看看利用 `any` 函式,位在 | ||
[List::MoreUtils](https://metacpan.org/pod/List::MoreUtils) 模組中,也可以解決這個問題。 | ||
|
||
## 是否有任何元素配對成功? | ||
|
||
`any` 這個函式跟 `grep` 有同樣的語法: 一個程式區塊和一個串列,但是它只回傳 true 或是 false。如果程式區塊配對成功就回傳 true,反之則回傳 false。 | ||
而且它有短捷徑,所以在大量串列時,它能夠運算的很快。 | ||
|
||
```perl | ||
use List::MoreUtils qw(any); | ||
if (any { $visitor eq $_ } @names) { | ||
print "Visitor $visitor is in the guest list\n"; | ||
} else { | ||
print "Visitor $visitor is NOT in the guest list\n"; | ||
} | ||
``` | ||
|
||
|
||
## UNIX 和 Linux grep? | ||
|
||
解釋如下: | ||
|
||
我曾經提過 Perl 的 grep 是 UNIX和 Linux 指令中 grep 或是 egrep 的通式。 | ||
|
||
<b>UNIX grep</b> 是基於正規表達式來過濾檔案中的每一列。 | ||
|
||
<b>Perl's grep</b> 則是可以用在任何表達式來過濾任何串列的值。 | ||
|
||
下面這個程式可以代表 UNIX grep 的基本功能: | ||
|
||
```perl | ||
my $regex = shift; | ||
print grep { $_ =~ /$regex/ } <>; | ||
``` | ||
|
||
第一行從命令列得到第一個參數來當作正規表達式。剩下的命令列參數則應該是檔案名稱。 | ||
|
||
鑽石符號 `<>` 可以讀取命令列中檔案名稱的每一列。接下來 grep 根據正規表達式來過濾每一列。每一列只要能成功配對的話,就會被印出來。 | ||
|
||
## Windows 上的 grep | ||
|
||
Windows 上並沒有 grep 這樣的工具,不過你可以安裝一個或是你可以使用上面的 perl 程式腳本。 | ||
|
Oops, something went wrong.