⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arrays.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
old phrases'</FONT>,<FONT color=3D#43cd80> </FONT>
      <FONT color=3D#00cd00>'ponder big, happy thoughts'</FONT>,<FONT =
color=3D#43cd80> </FONT>
      <FONT color=3D#00cd00>'sleep and dream peacefully'</FONT> <FONT =
color=3D#00ffff>]</FONT>,
    <FONT color=3D#00ffff>);</FONT>

<FONT color=3D#ffa500>foreach</FONT> <FONT color=3D#9ac0cd>$aref</FONT> =
<FONT color=3D#00ffff>(</FONT><FONT color=3D#cdad00>@lists</FONT><FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"The list =
is: "</FONT> . commify_series<FONT color=3D#00ffff>(</FONT>@$aref<FONT =
color=3D#00ffff>)</FONT> . <FONT color=3D#00cd00>".\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>

<FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>commify_series</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#9ac0cd>$sepchar</FONT> =3D <FONT =
color=3D#ff7f50>grep</FONT><FONT color=3D#00ffff>(</FONT>/,/ =3D&gt; =
<FONT color=3D#cdad00>@_</FONT><FONT color=3D#00ffff>)</FONT> ? <FONT =
color=3D#00cd00>";"</FONT> : <FONT color=3D#00cd00>","</FONT><FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#00ffff>(</FONT><FONT color=3D#cdad00>@_</FONT> =3D=3D =
<FONT color=3D#cdcd00>0</FONT><FONT color=3D#00ffff>)</FONT> ? <FONT =
color=3D#00cd00>''</FONT>                                      :
    <FONT color=3D#00ffff>(</FONT><FONT color=3D#cdad00>@_</FONT> =3D=3D =
<FONT color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>)</FONT> ? <FONT =
color=3D#cdad00>$_</FONT><FONT color=3D#00ffff>[</FONT><FONT =
color=3D#cdcd00>0</FONT><FONT color=3D#00ffff>]</FONT>                   =
                :
    <FONT color=3D#00ffff>(</FONT><FONT color=3D#cdad00>@_</FONT> =3D=3D =
<FONT color=3D#cdcd00>2</FONT><FONT color=3D#00ffff>)</FONT> ? <FONT =
color=3D#98fb98>join</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>" and "</FONT>, <FONT color=3D#cdad00>@_</FONT><FONT =
color=3D#00ffff>)</FONT>                       :
                <FONT color=3D#98fb98>join</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"$sepchar "</FONT>, <FONT =
color=3D#cdad00>@_</FONT><FONT color=3D#00ffff>[</FONT><FONT =
color=3D#cdcd00>0</FONT> .. <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>$#_</FONT><FONT color=3D#cdcd00>-1</FONT><FONT =
color=3D#00ffff>)]</FONT>, <FONT color=3D#00cd00>"and </FONT><FONT =
color=3D#cdad00>$_</FONT><FONT color=3D#00cd00>[-1]"</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#00ffff>}</FONT>

<FONT color=3D#bebebe>#-----------------------------
#The list is: just one thing.
#
#The list is: Mutt and Jeff.
#
#The list is: Peter, Paul, and Mary.
#
#The list is: To our parents, Mother Theresa, and God.
#
#The list is: pastrami, ham and cheese, peanut butter and jelly, and =
tuna.
#
#The list is: recycle tired, old phrases and ponder big, happy thoughts.
#
#The list is: recycle tired, old phrases; ponder</FONT><FONT =
color=3D#43cd80> </FONT><FONT color=3D#bebebe>
#
#   big, happy thoughts; and sleep and dream peacefully.
#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN179>Changing Array Size</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
# grow or shrink </FONT><FONT color=3D#cdad00>@ARRAY</FONT><FONT =
color=3D#bebebe>
</FONT><FONT color=3D#cdad00>$#ARRAY</FONT> =3D =
$NEW_LAST_ELEMENT_INDEX_NUMBER<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#cdad00>$ARRAY</FONT><FONT =
color=3D#00ffff>[</FONT>$NEW_LAST_ELEMENT_INDEX_NUMBER<FONT =
color=3D#00ffff>]</FONT> =3D $VALUE<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>what_about_that_array</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"The array =
now has "</FONT>, <FONT color=3D#ff7f50>scalar</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdad00>@people</FONT><FONT =
color=3D#00ffff>)</FONT>, <FONT color=3D#00cd00>" =
elements.\n"</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"The index =
of the last element is </FONT><FONT color=3D#cdad00>$#people</FONT><FONT =
color=3D#00cd00>.\n"</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Element #3 =
is `</FONT><FONT color=3D#cdad00>$people</FONT><FONT =
color=3D#00cd00>[3]'.\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>

<FONT color=3D#cdad00>@people</FONT> =3D <FONT =
color=3D#ff7f50>qw</FONT><FONT color=3D#00ffff>(</FONT>Crosby Stills =
Nash Young<FONT color=3D#00ffff>);</FONT>
what_about_that_array<FONT color=3D#00ffff>();</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>The array now has <FONT color=3D#cdcd00>4</FONT> elements.

The <FONT color=3D#98fb98>index</FONT> of the <FONT =
color=3D#ffa500>last</FONT> <FONT color=3D#ff7f50>element</FONT> is =
<FONT color=3D#cdcd00>3</FONT>.

Element <FONT color=3D#bebebe>#3 is `Young'.
#-----------------------------
</FONT><FONT color=3D#cdad00>$#people</FONT>--<FONT =
color=3D#00ffff>;</FONT>
what_about_that_array<FONT color=3D#00ffff>();</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>The array now has <FONT color=3D#cdcd00>3</FONT> elements.

The <FONT color=3D#98fb98>index</FONT> of the <FONT =
color=3D#ffa500>last</FONT> <FONT color=3D#ff7f50>element</FONT> is =
<FONT color=3D#cdcd00>2</FONT>.

Element <FONT color=3D#bebebe>#3 is `'.
#-----------------------------
</FONT><FONT color=3D#cdad00>$#people</FONT> =3D <FONT =
color=3D#cdcd00>10</FONT>_<FONT color=3D#cdcd00>000</FONT><FONT =
color=3D#00ffff>;</FONT>
what_about_that_array<FONT color=3D#00ffff>();</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>The array now has <FONT color=3D#cdcd00>10001</FONT> elements.

The <FONT color=3D#98fb98>index</FONT> of the <FONT =
color=3D#ffa500>last</FONT> <FONT color=3D#ff7f50>element</FONT> is =
<FONT color=3D#cdcd00>10000</FONT>.

Element <FONT color=3D#bebebe>#3 is `'.
#-----------------------------
</FONT><FONT color=3D#cdad00>$people</FONT><FONT =
color=3D#00ffff>[</FONT><FONT color=3D#cdcd00>10</FONT>_<FONT =
color=3D#cdcd00>000</FONT><FONT color=3D#00ffff>]</FONT> =3D <FONT =
color=3D#ff7f50>undef</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN182>Doing Something with Every Element in =
a=20
List</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>foreach</FONT> <FONT =
color=3D#9ac0cd>$item</FONT> <FONT color=3D#00ffff>(</FONT>LIST<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#bebebe># do something with $item
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>foreach</FONT> <FONT =
color=3D#9ac0cd>$user</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>@bad_users</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
        complain<FONT color=3D#00ffff>(</FONT>$user<FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>foreach</FONT> <FONT =
color=3D#9ac0cd>$var</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#ff7f50>sort</FONT> <FONT color=3D#ff7f50>keys</FONT> <FONT =
color=3D#cdcd00>%ENV</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"$var=3D</FONT><FONT color=3D#cdcd00>$ENV</FONT><FONT =
color=3D#00cd00>{$var}\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>foreach</FONT> <FONT =
color=3D#9ac0cd>$user</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>@all_users</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    $disk_space =3D get_usage<FONT color=3D#00ffff>(</FONT>$user<FONT =
color=3D#00ffff>);</FONT>     <FONT color=3D#bebebe># find out how much =
disk space in use
</FONT>    <FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT>$disk_space &gt; $MAX_QUOTA<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>     <FONT =
color=3D#bebebe># if it's more than we want ...
</FONT>        complain<FONT color=3D#00ffff>(</FONT>$user<FONT =
color=3D#00ffff>);</FONT>                <FONT color=3D#bebebe># ... =
then object vociferously
</FONT>    <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>foreach</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>`who`</FONT><FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT>/tchrist/<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
        <FONT color=3D#ff7f50>print</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;FH&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>              <FONT color=3D#bebebe># $_ is set =
to the line just read
</FONT>    <FONT color=3D#ff7f50>chomp</FONT><FONT =
color=3D#00ffff>;</FONT>                  <FONT color=3D#bebebe># $_ has =
a trailing \n removed, if it had one
</FONT>    <FONT color=3D#ffa500>foreach</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#ff7f50>split</FONT><FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>       <FONT =
color=3D#bebebe># $_ is split on whitespace, into </FONT><FONT =
color=3D#cdad00>@_</FONT><FONT color=3D#bebebe>
</FONT>                            <FONT color=3D#bebebe># then $_ is =
set to each chunk in turn
</FONT>        $_ =3D <FONT color=3D#98fb98>reverse</FONT><FONT =
color=3D#00ffff>;</FONT>       <FONT color=3D#bebebe># the characters in =
$_ are reversed
</FONT>        <FONT color=3D#ff7f50>print</FONT><FONT =
color=3D#00ffff>;</FONT>              <FONT color=3D#bebebe># $_ is =
printed
</FONT>    <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>foreach</FONT> <FONT =
color=3D#ffa500>my</FONT> $item <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>@array</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"i =3D =
$item\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#cdad00>@array</FONT> =3D <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdcd00>1</FONT>,<FONT =
color=3D#cdcd00>2</FONT>,<FONT color=3D#cdcd00>3</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#ffa500>foreach</FONT> <FONT color=3D#9ac0cd>$item</FONT> =
<FONT color=3D#00ffff>(</FONT><FONT color=3D#cdad00>@array</FONT><FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
    $item--<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"</FONT><FONT =
color=3D#cdad00>@array</FONT><FONT color=3D#00cd00>\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#cdcd00>0</FONT> <FONT color=3D#cdcd00>1</FONT> <FONT =
color=3D#cdcd00>2</FONT>


<FONT color=3D#bebebe># multiply everything in </FONT><FONT =

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -