📄 databaseaccess.mht
字号:
color=3D#98fb98>cmp</FONT> <FONT color=3D#00cd00>"\L$key2"</FONT> <FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>};</FONT>
<FONT color=3D#ffa500>my</FONT> <FONT color=3D#cdcd00>%hash</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$filename</FONT> =
=3D <FONT color=3D#00cd00>'/tmp/sorthash.db'</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>tie</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>%hash</FONT>, <FONT color=3D#00cd00>"DB_File"</FONT>, =
$filename, O_RDWR|O_CREAT, <FONT color=3D#cdcd00>0666</FONT>, =
$DB_BTREE<FONT color=3D#00ffff>)</FONT>
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"can't tie $filename: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$i</FONT> =3D =
<FONT color=3D#cdcd00>0</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>for</FONT> <FONT color=3D#ffa500>my</FONT> $word =
<FONT color=3D#00ffff>(</FONT><FONT color=3D#ff7f50>qw</FONT><FONT =
color=3D#00ffff>(</FONT>Can<FONT color=3D#00cd00>'t you go camp down by =
Gibraltar)) {
</FONT><FONT color=3D#cdcd00>$hash</FONT><FONT =
color=3D#00cd00>{$word} =3D ++$i;
}
while (my($word, $number) =3D each </FONT><FONT =
color=3D#cdcd00>%hash</FONT><FONT color=3D#00cd00>) {
printf "%-12s </FONT><FONT color=3D#cdcd00>%d</FONT><FONT =
color=3D#00cd00>\n", $word, $number;
}
#-----------------------------
#by 6
#
#camp 4
#
#Can'</FONT>t <FONT color=3D#cdcd00>1</FONT>
<FONT color=3D#bebebe>#
#down 5
#
#Gibraltar 7
#
#go 3
#
#you 2
#-----------------------------
</FONT><FONT color=3D#ff7f50>tie</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdcd00>%hash</FONT>, <FONT =
color=3D#00cd00>"DB_File"</FONT>, <FONT color=3D#ff7f50>undef</FONT>, =
O_RDWR|O_CREAT, <FONT color=3D#cdcd00>0666</FONT>, $DB_BTREE<FONT =
color=3D#00ffff>)</FONT>
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"can't tie: $!"</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=3DAEN775>Treating a Text File as a Database =
Array</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>use</FONT> <FONT =
color=3D#b2dfee>DB_File</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>tie</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>@array</FONT>, <FONT color=3D#00cd00>"DB_File"</FONT>, =
<FONT color=3D#00cd00>"/tmp/textfile"</FONT>, O_RDWR|O_CREAT, <FONT =
color=3D#cdcd00>0666</FONT>, $DB_RECNO<FONT color=3D#00ffff>)</FONT>
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Cannot open file 'text': $!\n"</FONT> <FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#cdad00>$array</FONT><FONT color=3D#00ffff>[</FONT><FONT =
color=3D#cdcd00>4</FONT><FONT color=3D#00ffff>]</FONT> =3D <FONT =
color=3D#00cd00>"a new line"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>untie</FONT> <FONT =
color=3D#cdad00>@array</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch14/recno_demo">downlo=
ad the following standalone program</A></FONT>
#!/usr/bin/perl -w
# recno_demo - show how to use the raw API on recno bindings
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>strict</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>use</FONT> <FONT color=3D#b2dfee>vars</FONT> <FONT =
color=3D#ff7f50>qw</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>@lines</FONT> $dbobj $file $i<FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>DB_File</FONT><FONT color=3D#00ffff>;</FONT>
$file =3D <FONT color=3D#00cd00>"/tmp/textfile"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>unlink</FONT> $file<FONT color=3D#00ffff>;</FONT> =
<FONT color=3D#bebebe># just in case
</FONT>
$dbobj =3D <FONT color=3D#ff7f50>tie</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdad00>@lines</FONT>, <FONT =
color=3D#00cd00>"DB_File"</FONT>, $file, O_RDWR|O_CREAT, <FONT =
color=3D#cdcd00>0666</FONT>, $DB_RECNO<FONT color=3D#00ffff>)</FONT>
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Cannot open file $file: $!\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># first create a text file to play with
</FONT><FONT color=3D#cdad00>$lines</FONT><FONT =
color=3D#00ffff>[</FONT><FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>]</FONT> =3D <FONT color=3D#00cd00>"zero"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#cdad00>$lines</FONT><FONT color=3D#00ffff>[</FONT><FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>]</FONT> =3D <FONT =
color=3D#00cd00>"one"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#cdad00>$lines</FONT><FONT color=3D#00ffff>[</FONT><FONT =
color=3D#cdcd00>2</FONT><FONT color=3D#00ffff>]</FONT> =3D <FONT =
color=3D#00cd00>"two"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#cdad00>$lines</FONT><FONT color=3D#00ffff>[</FONT><FONT =
color=3D#cdcd00>3</FONT><FONT color=3D#00ffff>]</FONT> =3D <FONT =
color=3D#00cd00>"three"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#cdad00>$lines</FONT><FONT color=3D#00ffff>[</FONT><FONT =
color=3D#cdcd00>4</FONT><FONT color=3D#00ffff>]</FONT> =3D <FONT =
color=3D#00cd00>"four"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># Print the records in order.
#
# The length method is needed here because evaluating a tied
# array in a scalar context does not return the number of
# elements in the array.
</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"\nORIGINAL\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>foreach</FONT> <FONT color=3D#9ac0cd>$i</FONT> =
<FONT color=3D#00ffff>(</FONT><FONT color=3D#cdcd00>0</FONT> .. =
$dbobj-><FONT color=3D#98fb98>length</FONT> - <FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"$i: =
</FONT><FONT color=3D#cdad00>$lines</FONT><FONT =
color=3D#00cd00>[$i]\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe># use the push & pop methods
</FONT>$a =3D $dbobj-><FONT color=3D#ff7f50>pop</FONT><FONT =
color=3D#00ffff>;</FONT>
$dbobj-><FONT color=3D#ff7f50>push</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"last"</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"\nThe last =
record was [$a]\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># and the shift & unshift methods
</FONT>$a =3D $dbobj-><FONT color=3D#ff7f50>shift</FONT><FONT =
color=3D#00ffff>;</FONT>
$dbobj-><FONT color=3D#ff7f50>unshift</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"first"</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"The first =
record was [$a]\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># Use the API to add a new record after record 2.
</FONT>$i =3D <FONT color=3D#cdcd00>2</FONT><FONT =
color=3D#00ffff>;</FONT>
$dbobj->put<FONT color=3D#00ffff>(</FONT>$i, <FONT =
color=3D#00cd00>"Newbie"</FONT>, R_IAFTER<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#43cd80> </FONT>
<FONT color=3D#bebebe># and a new record before record 1.
</FONT>$i =3D <FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>;</FONT>
$dbobj->put<FONT color=3D#00ffff>(</FONT>$i, <FONT =
color=3D#00cd00>"New One"</FONT>, R_IBEFORE<FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe># delete record 3
</FONT>$dbobj->del<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>3</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe># now print the records in reverse order
</FONT><FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"\nREVERSE\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>for</FONT> <FONT color=3D#00ffff>(</FONT>$i =3D =
$dbobj-><FONT color=3D#98fb98>length</FONT> - <FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>;</FONT> $i >=3D <FONT =
color=3D#cdcd00>0</FONT><FONT color=3D#00ffff>;</FONT> -- $i<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"$i: =
</FONT><FONT color=3D#cdad00>$lines</FONT><FONT =
color=3D#00cd00>[$i]\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe># same again, but use the API functions instead
</FONT><FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"\nREVERSE again\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>my</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#9ac0cd>$s</FONT>, <FONT color=3D#9ac0cd>$k</FONT>, <FONT =
color=3D#9ac0cd>$v</FONT><FONT color=3D#00ffff>)</FONT> =3D <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdcd00>0</FONT>, <FONT =
color=3D#cdcd00>0</FONT>, <FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#ffa500>for</FONT> <FONT color=3D#00ffff>(</FONT>$s =3D =
$dbobj->seq<FONT color=3D#00ffff>(</FONT>$k, $v, R_LAST<FONT =
color=3D#00ffff>);</FONT>
$s =3D=3D <FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>;</FONT>
$s =3D $dbobj->seq<FONT color=3D#00ffff>(</FONT>$k, $v, =
R_PREV<FONT color=3D#00ffff>))</FONT>
<FONT color=3D#00ffff>{</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"$k: =
$v\n"</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#ff7f50>undef</FONT> $dbobj<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>untie</FONT> <FONT =
color=3D#cdad00>@lines</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
#ORIGINAL
#
#0: zero
#
#1: one
#
#2: two
#
#3: three
#
#4: four
#
#
#The last record was [four]
#
#The first record was [zero]
#
#
#REVERSE
#
#5: last
#
#4: three
#
#3: Newbie
#
#2: one
#
#1: New One
#
#0: first
#
#
#REVERSE again
#
#5: last
#
#4: three
#
#3: Newbie
#
#2: one
#
#1: New One
#
#0: first
#-----------------------------
</FONT> <FONT color=3D#ffa500>foreach</FONT> <FONT =
color=3D#9ac0cd>$item</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>@lines</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>foreach</FONT> <FONT =
color=3D#9ac0cd>$i</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -