📄 perldsc.1
字号:
\&\&\& # reading from file\& # format: LEAD=fred FRIEND=barney\& # no temp\& while ( <> ) {\& push @AoH, { split /[\es+=]/ };\& }\&\& # calling a function that returns a key/value pair list, like\& # "lead","fred","daughter","pebbles"\& while ( %fields = getnextpairset() ) {\& push @AoH, { %fields };\& }\&\& # likewise, but using no temp vars\& while (<>) {\& push @AoH, { parsepairs($_) };\& }\&\& # add key/value to an element\& $AoH[0]{pet} = "dino";\& $AoH[2]{pet} = "santa\*(Aqs little helper";.Ve.Sh "Access and Printing of an \s-1ARRAY\s0 \s-1OF\s0 \s-1HASHES\s0".IX Subsection "Access and Printing of an ARRAY OF HASHES".Vb 2\& # one element\& $AoH[0]{lead} = "fred";\&\& # another element\& $AoH[1]{lead} =~ s/(\ew)/\eu$1/;\&\& # print the whole thing with refs\& for $href ( @AoH ) {\& print "{ ";\& for $role ( keys %$href ) {\& print "$role=$href\->{$role} ";\& }\& print "}\en";\& }\&\& # print the whole thing with indices\& for $i ( 0 .. $#AoH ) {\& print "$i is { ";\& for $role ( keys %{ $AoH[$i] } ) {\& print "$role=$AoH[$i]{$role} ";\& }\& print "}\en";\& }\&\& # print the whole thing one at a time\& for $i ( 0 .. $#AoH ) {\& for $role ( keys %{ $AoH[$i] } ) {\& print "elt $i $role is $AoH[$i]{$role}\en";\& }\& }.Ve.SH "HASHES OF HASHES".IX Xref "hass of hashes HoH".IX Header "HASHES OF HASHES".Sh "Declaration of a \s-1HASH\s0 \s-1OF\s0 \s-1HASHES\s0".IX Subsection "Declaration of a HASH OF HASHES".Vb 10\& %HoH = (\& flintstones => {\& lead => "fred",\& pal => "barney",\& },\& jetsons => {\& lead => "george",\& wife => "jane",\& "his boy" => "elroy",\& },\& simpsons => {\& lead => "homer",\& wife => "marge",\& kid => "bart",\& },\& );.Ve.Sh "Generation of a \s-1HASH\s0 \s-1OF\s0 \s-1HASHES\s0".IX Subsection "Generation of a HASH OF HASHES".Vb 9\& # reading from file\& # flintstones: lead=fred pal=barney wife=wilma pet=dino\& while ( <> ) {\& next unless s/^(.*?):\es*//;\& $who = $1;\& for $field ( split ) {\& ($key, $value) = split /=/, $field;\& $HoH{$who}{$key} = $value;\& }\&\&\& # reading from file; more temps\& while ( <> ) {\& next unless s/^(.*?):\es*//;\& $who = $1;\& $rec = {};\& $HoH{$who} = $rec;\& for $field ( split ) {\& ($key, $value) = split /=/, $field;\& $rec\->{$key} = $value;\& }\& }\&\& # calling a function that returns a key,value hash\& for $group ( "simpsons", "jetsons", "flintstones" ) {\& $HoH{$group} = { get_family($group) };\& }\&\& # likewise, but using temps\& for $group ( "simpsons", "jetsons", "flintstones" ) {\& %members = get_family($group);\& $HoH{$group} = { %members };\& }\&\& # append new members to an existing family\& %new_folks = (\& wife => "wilma",\& pet => "dino",\& );\&\& for $what (keys %new_folks) {\& $HoH{flintstones}{$what} = $new_folks{$what};\& }.Ve.Sh "Access and Printing of a \s-1HASH\s0 \s-1OF\s0 \s-1HASHES\s0".IX Subsection "Access and Printing of a HASH OF HASHES".Vb 2\& # one element\& $HoH{flintstones}{wife} = "wilma";\&\& # another element\& $HoH{simpsons}{lead} =~ s/(\ew)/\eu$1/;\&\& # print the whole thing\& foreach $family ( keys %HoH ) {\& print "$family: { ";\& for $role ( keys %{ $HoH{$family} } ) {\& print "$role=$HoH{$family}{$role} ";\& }\& print "}\en";\& }\&\& # print the whole thing somewhat sorted\& foreach $family ( sort keys %HoH ) {\& print "$family: { ";\& for $role ( sort keys %{ $HoH{$family} } ) {\& print "$role=$HoH{$family}{$role} ";\& }\& print "}\en";\& }\&\&\& # print the whole thing sorted by number of members\& foreach $family ( sort { keys %{$HoH{$b}} <=> keys %{$HoH{$a}} } keys %HoH ) {\& print "$family: { ";\& for $role ( sort keys %{ $HoH{$family} } ) {\& print "$role=$HoH{$family}{$role} ";\& }\& print "}\en";\& }\&\& # establish a sort order (rank) for each role\& $i = 0;\& for ( qw(lead wife son daughter pal pet) ) { $rank{$_} = ++$i }\&\& # now print the whole thing sorted by number of members\& foreach $family ( sort { keys %{ $HoH{$b} } <=> keys %{ $HoH{$a} } } keys %HoH ) {\& print "$family: { ";\& # and print these according to rank order\& for $role ( sort { $rank{$a} <=> $rank{$b} } keys %{ $HoH{$family} } ) {\& print "$role=$HoH{$family}{$role} ";\& }\& print "}\en";\& }.Ve.SH "MORE ELABORATE RECORDS".IX Xref "record structure struct".IX Header "MORE ELABORATE RECORDS".Sh "Declaration of \s-1MORE\s0 \s-1ELABORATE\s0 \s-1RECORDS\s0".IX Subsection "Declaration of MORE ELABORATE RECORDS"Here's a sample showing how to create and use a record whose fields are ofmany different sorts:.PP.Vb 8\& $rec = {\& TEXT => $string,\& SEQUENCE => [ @old_values ],\& LOOKUP => { %some_table },\& THATCODE => \e&some_function,\& THISCODE => sub { $_[0] ** $_[1] },\& HANDLE => \e*STDOUT,\& };\&\& print $rec\->{TEXT};\&\& print $rec\->{SEQUENCE}[0];\& $last = pop @ { $rec\->{SEQUENCE} };\&\& print $rec\->{LOOKUP}{"key"};\& ($first_k, $first_v) = each %{ $rec\->{LOOKUP} };\&\& $answer = $rec\->{THATCODE}\->($arg);\& $answer = $rec\->{THISCODE}\->($arg1, $arg2);\&\& # careful of extra block braces on fh ref\& print { $rec\->{HANDLE} } "a string\en";\&\& use FileHandle;\& $rec\->{HANDLE}\->autoflush(1);\& $rec\->{HANDLE}\->print(" a string\en");.Ve.Sh "Declaration of a \s-1HASH\s0 \s-1OF\s0 \s-1COMPLEX\s0 \s-1RECORDS\s0".IX Subsection "Declaration of a HASH OF COMPLEX RECORDS".Vb 10\& %TV = (\& flintstones => {\& series => "flintstones",\& nights => [ qw(monday thursday friday) ],\& members => [\& { name => "fred", role => "lead", age => 36, },\& { name => "wilma", role => "wife", age => 31, },\& { name => "pebbles", role => "kid", age => 4, },\& ],\& },\&\& jetsons => {\& series => "jetsons",\& nights => [ qw(wednesday saturday) ],\& members => [\& { name => "george", role => "lead", age => 41, },\& { name => "jane", role => "wife", age => 39, },\& { name => "elroy", role => "kid", age => 9, },\& ],\& },\&\& simpsons => {\& series => "simpsons",\& nights => [ qw(monday) ],\& members => [\& { name => "homer", role => "lead", age => 34, },\& { name => "marge", role => "wife", age => 37, },\& { name => "bart", role => "kid", age => 11, },\& ],\& },\& );.Ve.Sh "Generation of a \s-1HASH\s0 \s-1OF\s0 \s-1COMPLEX\s0 \s-1RECORDS\s0".IX Subsection "Generation of a HASH OF COMPLEX RECORDS".Vb 5\& # reading from file\& # this is most easily done by having the file itself be\& # in the raw data format as shown above. perl is happy\& # to parse complex data structures if declared as data, so\& # sometimes it\*(Aqs easiest to do that\&\& # here\*(Aqs a piece by piece build up\& $rec = {};\& $rec\->{series} = "flintstones";\& $rec\->{nights} = [ find_days() ];\&\& @members = ();\& # assume this file in field=value syntax\& while (<>) {\& %fields = split /[\es=]+/;\& push @members, { %fields };\& }\& $rec\->{members} = [ @members ];\&\& # now remember the whole thing\& $TV{ $rec\->{series} } = $rec;\&\& ###########################################################\& # now, you might want to make interesting extra fields that\& # include pointers back into the same data structure so if\& # change one piece, it changes everywhere, like for example\& # if you wanted a {kids} field that was a reference\& # to an array of the kids\*(Aq records without having duplicate\& # records and thus update problems.\& ###########################################################\& foreach $family (keys %TV) {\& $rec = $TV{$family}; # temp pointer\& @kids = ();\& for $person ( @{ $rec\->{members} } ) {\& if ($person\->{role} =~ /kid|son|daughter/) {\& push @kids, $person;\& }\& }\& # REMEMBER: $rec and $TV{$family} point to same data!!\& $rec\->{kids} = [ @kids ];\& }\&\& # you copied the array, but the array itself contains pointers\& # to uncopied objects. this means that if you make bart get\& # older via\&\& $TV{simpsons}{kids}[0]{age}++;\&\& # then this would also change in\& print $TV{simpsons}{members}[2]{age};\&\& # because $TV{simpsons}{kids}[0] and $TV{simpsons}{members}[2]\& # both point to the same underlying anonymous hash table\&\& # print the whole thing\& foreach $family ( keys %TV ) {\& print "the $family";\& print " is on during @{ $TV{$family}{nights} }\en";\& print "its members are:\en";\& for $who ( @{ $TV{$family}{members} } ) {\& print " $who\->{name} ($who\->{role}), age $who\->{age}\en";\& }\& print "it turns out that $TV{$family}{lead} has ";\& print scalar ( @{ $TV{$family}{kids} } ), " kids named ";\& print join (", ", map { $_\->{name} } @{ $TV{$family}{kids} } );\& print "\en";\& }.Ve.SH "Database Ties".IX Header "Database Ties"You cannot easily tie a multilevel data structure (such as a hash ofhashes) to a dbm file. The first problem is that all but \s-1GDBM\s0 andBerkeley \s-1DB\s0 have size limitations, but beyond that, you also have problemswith how references are to be represented on disk. One experimentalmodule that does partially attempt to address this need is the \s-1MLDBM\s0module. Check your nearest \s-1CPAN\s0 site as described in perlmodlib forsource code to \s-1MLDBM\s0..SH "SEE ALSO".IX Header "SEE ALSO"\&\fIperlref\fR\|(1), \fIperllol\fR\|(1), \fIperldata\fR\|(1), \fIperlobj\fR\|(1).SH "AUTHOR".IX Header "AUTHOR"Tom Christiansen <\fItchrist@perl.com\fR>.PPLast update:Wed Oct 23 04:57:50 \s-1MET\s0 \s-1DST\s0 1996
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -