Access and Printing of an ARRAY of HASHES

download Access and Printing of an ARRAY of HASHES

of 18

Transcript of Access and Printing of an ARRAY of HASHES

  • 8/9/2019 Access and Printing of an ARRAY of HASHES

    1/18

    Generation of a HASH OF ARRAYS

    1. # reading from file2. # flintstones: fred barney wilma dino3. while ( ) {

    4. next unless s/^(.*?):\s*//;5. $HoA{$1} = [ split ];6. }7.8. # reading from file; more temps9. # flintstones: fred barney wilma dino10. while ( $line = ) {11. ($who, $rest) = split /:\s*/, $line, 2;12. @fields = split ' ', $rest;13. $HoA{$who} = [ @fields ];14. }

    15.16. # calling a function that returns a list17. for $group ( "simpsons", "jetsons", "flintstones" ) {18. $HoA{$group} = [ get_family($group) ];19. }20.21. # likewise, but using temps22. for $group ( "simpsons", "jetsons", "flintstones" ) {23. @members = get_family($group);24. $HoA{$group} = [ @members ];25. }26.27. # append new members to an existing family28. push @{ $HoA{"flintstones"} }, "wilma", "betty";

    Access and Printing of a HASH OF ARRAYS

    1. # one element2. $HoA{flintstones}[0] = "Fred";3.4. # another element

    5. $HoA{simpsons}[1] =~ s/(\w)/\u$1/;6.7. # print the whole thing8. foreach $family ( keys %HoA ) {9. print "$family: @{ $HoA{$family} }\n"10. }11.12. # print the whole thing with indices13. foreach $family ( keys %HoA ) {14. print "family: ";15. foreach $i ( 0 .. $#{ $HoA{$family} } ) {

    16. print " $i = $HoA{$family}[$i]";17. }18. print "\n";

  • 8/9/2019 Access and Printing of an ARRAY of HASHES

    2/18

    19. }20.21. # print the whole thing sorted by number of members22. foreach $family ( sort { @{$HoA{$b}} @{$HoA{$a}} } keys %HoA ) {23. print "$family: @{ $HoA{$family} }\n"24. }

    25.26. # print the whole thing sorted by number of members and name27. foreach $family ( sort {28. @{$HoA{$b}} @{$HoA{$a}}29. ||30. $a cmp $b31. } keys %HoA )32. {33. print "$family: ", join(", ", sort @{ $HoA{$family} }), "\n";34. }

    ARRAYS OF HASHESDeclaration of an ARRAY OF HASHES

    1. @AoH = (2. {3. Lead => "fred",4. Friend => "barney",5. },6. {7. Lead => "george",8. Wife => "jane",9. Son => "elroy",10. },11. {12. Lead => "homer",13. Wife => "marge",14. Son => "bart",15. }16. );

    Generation of an ARRAY OF HASHES

    1. # reading from file2. # format: LEAD=fred FRIEND=barney3. while ( ) {4. $rec = {};5. for $field ( split ) {6. ($key, $value) = split /=/, $field;7. $rec->{$key} = $value;8. }9. push @AoH, $rec;10. }

    11.12.13. # reading from file

  • 8/9/2019 Access and Printing of an ARRAY of HASHES

    3/18

    14. # format: LEAD=fred FRIEND=barney15. # no temp16. while ( ) {17. push @AoH, { split /[\s+=]/ };18. }19.

    20. # calling a function that returns a key/value pair list, like21. # "lead","fred","daughter","pebbles"22. while ( %fields = getnextpairset() ) {23. push @AoH, { %fields };24. }25.26. # likewise, but using no temp vars27. while () {28. push @AoH, { parsepairs($_) };29. }30.

    31. # add key/value to an element32. $AoH[0]{pet} = "dino";33. $AoH[2]{pet} = "santa's little helper";

    Access and Printing of an ARRAY OF HASHES

    1. # one element2. $AoH[0]{lead} = "fred";3.4. # another element5. $AoH[1]{lead} =~ s/(\w)/\u$1/;6.7. # print the whole thing with refs8. for $href ( @AoH ) {9. print "{ ";10. for $role ( keys %$href ) {11. print "$role=$href->{$role} ";12. }13. print "}\n";14. }15.

    16. # print the whole thing with indices17. for $i ( 0 .. $#AoH ) {18. print "$i is { ";19. for $role ( keys %{ $AoH[$i] } ) {20. print "$role=$AoH[$i]{$role} ";21. }22. print "}\n";23. }24.25. # print the whole thing one at a time26. for $i ( 0 .. $#AoH ) {

    27. for $role ( keys %{ $AoH[$i] } ) {28. print "elt $i $role is $AoH[$i]{$role}\n";29. }

  • 8/9/2019 Access and Printing of an ARRAY of HASHES

    4/18

    30. }

    HASHES OF HASHESDeclaration of a HASH OF HASHES

    1. %HoH = (

    2. flintstones => {3. lead => "fred",4. pal => "barney",5. },6. jetsons => {7. lead => "george",8. wife => "jane",9. "his boy" => "elroy",10. },11. simpsons => {12. lead => "homer",

    13. wife => "marge",14. kid => "bart",15. },16. );

    Generation of a HASH OF HASHES

    1. # reading from file2. # flintstones: lead=fred pal=barney wife=wilma pet=dino3. while ( ) {4. next unless s/^(.*?):\s*//;5. $who = $1;6. for $field ( split ) {7. ($key, $value) = split /=/, $field;8. $HoH{$who}{$key} = $value;9. }10.11.12. # reading from file; more temps13. while ( ) {14. next unless s/^(.*?):\s*//;

    15. $who = $1;16. $rec = {};17. $HoH{$who} = $rec;18. for $field ( split ) {19. ($key, $value) = split /=/, $field;20. $rec->{$key} = $value;21. }22. }23.24. # calling a function that returns a key,value hash25. for $group ( "simpsons", "jetsons", "flintstones" ) {

    26. $HoH{$group} = { get_family($group) };27. }28.

  • 8/9/2019 Access and Printing of an ARRAY of HASHES

    5/18

    29. # likewise, but using temps30. for $group ( "simpsons", "jetsons", "flintstones" ) {31. %members = get_family($group);32. $HoH{$group} = { %members };33. }34.

    35. # append new members to an existing family36. %new_folks = (37. wife => "wilma",38. pet => "dino",39. );40.41. for $what (keys %new_folks) {42. $HoH{flintstones}{$what} = $new_folks{$what};43. }

    Access and Printing of a HASH OF HASHES

    1. # one element2. $HoH{flintstones}{wife} = "wilma";3.4. # another element5. $HoH{simpsons}{lead} =~ s/(\w)/\u$1/;6.7. # print the whole thing8. foreach $family ( keys %HoH ) {9. print "$family: { ";10. for $role ( keys %{ $HoH{$family} } ) {11. print "$role=$HoH{$family}{$role} ";12. }13. print "}\n";14. }15.16. # print the whole thing somewhat sorted17. foreach $family ( sort keys %HoH ) {18. print "$family: { ";19. for $role ( sort keys %{ $HoH{$family} } ) {20. print "$role=$HoH{$family}{$role} ";

    21. }22. print "}\n";23. }24.25.26. # print the whole thing sorted by number of members27. foreach $family ( sort { keys %{$HoH{$b}} keys %{$HoH{$a}} } keys %HoH ) {28. print "$family: { ";29. for $role ( sort keys %{ $HoH{$family} } ) {30. print "$role=$HoH{$family}{$role} ";31. }

    32. print "}\n";33. }34.

  • 8/9/2019 Access and Printing of an ARRAY of HASHES

    6/18

    35. # establish a sort order (rank) for each role36. $i = 0;37. for ( qw(lead wife son daughter pal pet) ) { $rank{$_} = ++$i }38.39. # now print the whole thing sorted by number of members40. foreach $family ( sort { keys %{ $HoH{$b} } keys %{ $HoH{$a} } } keys %HoH ) {

    41. print "$family: { ";42. # and print these according to rank order43. for $role ( sort { $rank{$a} $rank{$b} } keys %{ $HoH{$family} } ) {44. print "$role=$HoH{$family}{$role} ";45. }46. print "}\n";47. }

    MORE ELABORATE RECORDSDeclaration of MORE ELABORATE RECORDS

    Here's a sample showing how to create and use a record whose fields are of many different sorts:

    1. $rec = {2. TEXT => $string,3. SEQUENCE => [ @old_values ],4. LOOKUP => { %some_table },5. THATCODE => \&some_function,6. THISCODE => sub { $_[0] ** $_[1] },7. HANDLE => \*STDOUT,8. };9.10. print $rec->{TEXT};11.12. print $rec->{SEQUENCE}[0];13. $last = pop @ { $rec->{SEQUENCE} };14.15. print $rec->{LOOKUP}{"key"};16. ($first_k, $first_v) = each %{ $rec->{LOOKUP} };17.18. $answer = $rec->{THATCODE}->($arg);19. $answer = $rec->{THISCODE}->($arg1, $arg2);

    20.21. # careful of extra block braces on fh ref22. print { $rec->{HANDLE} } "a string\n";23.24. use FileHandle;25. $rec->{HANDLE}->autoflush(1);26. $rec->{HANDLE}->print(" a string\n");

    Declaration of a HASH OF COMPLEX RECORDS

    1. %TV = (

    2. flintstones => {3. series => "flintstones",4. nights => [ qw(monday thursday friday) ],

  • 8/9/2019 Access and Printing of an ARRAY of HASHES

    7/18

    5. members => [6. { name => "fred", role => "lead", age => 36, },7. { name => "wilma", role => "wife", age => 31, },8. { name => "pebbles", role => "kid", age => 4, },9. ],10. },

    11.12. jetsons => {13. series => "jetsons",14. nights => [ qw(wednesday saturday) ],15. members => [16. { name => "george", role => "lead", age => 41, },17. { name => "jane", role => "wife", age => 39, },18. { name => "elroy", role => "kid", age => 9, },19. ],20. },21.

    22. simpsons => {23. series => "simpsons",24. nights => [ qw(monday) ],25. members => [26. { name => "homer", role => "lead", age => 34, },27. { name => "marge", role => "wife", age => 37, },28. { name => "bart", role => "kid", age => 11, },29. ],30. },31. );

    Generation of a HASH OF COMPLEX RECORDS

    1. # reading from file2. # this is most easily done by having the file itself be3. # in the raw data format as shown above. perl is happy4. # to parse complex data structures if declared as data, so5. # sometimes it's easiest to do that6.7. # here's a piece by piece build up8. $rec = {};

    9. $rec->{series} = "flintstones";10. $rec->{nights} = [ find_days() ];11.12. @members = ();13. # assume this file in field=value syntax14. while () {15. %fields = split /[\s=]+/;16. push @members, { %fields };17. }18. $rec->{members} = [ @members ];19.

    20. # now remember the whole thing21. $TV{ $rec->{series} } = $rec;22.

  • 8/9/2019 Access and Printing of an ARRAY of HASHES

    8/18

    23. ###########################################################24. # now, you might want to make interesting extra fields that25. # include pointers back into the same data structure so if26. # change one piece, it changes everywhere, like for example27. # if you wanted a {kids} field that was a reference28. # to an array of the kids' records without having duplicate

    29. # records and thus update problems.30. ###########################################################31. foreach $family (keys %TV) {32. $rec = $TV{$family}; # temp pointer33. @kids = ();34. for $person ( @{ $rec->{members} } ) {35. if ($person->{role} =~ /kid|son|daughter/) {36. push @kids, $person;37. }38. }39. # REMEMBER: $rec and $TV{$family} point to same data!!

    40. $rec->{kids} = [ @kids ];41. }42.43. # you copied the array, but the array itself contains pointers44. # to uncopied objects. this means that if you make bart get45. # older via46.47. $TV{simpsons}{kids}[0]{age}++;48.49. # then this would also change in50. print $TV{simpsons}{members}[2]{age};51.52. # because $TV{simpsons}{kids}[0] and $TV{simpsons}{members}[2]53. # both point to the same underlying anonymous hash table54.55. # print the whole thing56. foreach $family ( keys %TV ) {57. print "the $family";58. print " is on during @{ $TV{$family}{nights} }\n";59. print "its members are:\n";60. for $who ( @{ $TV{$family}{members} } ) {

    61. print " $who->{name} ($who->{role}), age $who->{age}\n";62. }63. print "it turns out that $TV{$family}{lead} has ";64. print scalar ( @{ $TV{$family}{kids} } ), " kids named ";65. print join (", ", map { $_->{name} } @{ $TV{$family}{kids} } );66. print "\n";67. }

    Database Ties

    You cannot easily tie a multilevel data structure (such as a hash of hashes) to a dbm file. The first

    problem is that all but GDBM and Berkeley DB have size limitations, but beyond that, you alsohave problems with how references are to be represented on disk. One experimental module thatdoes partially attempt to address this need is the MLDBM module. Check your nearest CPAN site

  • 8/9/2019 Access and Printing of an ARRAY of HASHES

    9/18

    as described in perlmodlib for source code to MLDBM.SEE ALSO

    perlref, perllol, perldata, perlobjAUTHOR

    Tom Christiansen

    Last update: Wed Oct 23 04:57:50 MET DST 1996Page index

    * NAME* DESCRIPTION* REFERENCES* COMMON MISTAKES* CAVEAT ON PRECEDENCE* WHY YOU SHOULD ALWAYS use strict

    * DEBUGGING* CODE EXAMPLES* ARRAYS OF ARRAYS

    o Declaration of an ARRAY OF ARRAYSo Generation of an ARRAY OF ARRAYSo Access and Printing of an ARRAY OF ARRAYS

    * HASHES OF ARRAYSo Declaration of a HASH OF ARRAYSo Generation of a HASH OF ARRAYSo Access and Printing of a HASH OF ARRAYS

    * ARRAYS OF HASHESo Declaration of an ARRAY OF HASHESo Generation of an ARRAY OF HASHESo Access and Printing of an ARRAY OF HASHES

    * HASHES OF HASHESo Declaration of a HASH OF HASHESo Generation of a HASH OF HASHESo Access and Printing of a HASH OF HASHES

    * MORE ELABORATE RECORDSo Declaration of MORE ELABORATE RECORDSo Declaration of a HASH OF COMPLEX RECORDS

    o Generation of a HASH OF COMPLEX RECORDS* Database Ties* SEE ALSO* AUTHOR

    perldoc.perl.org - Official documentation for the Perl programming language

    Contact details

    Site maintained by Jon Allen (JJ)

    See the project page for more details

    Documentation maintained by the Perl 5 Porters

  • 8/9/2019 Access and Printing of an ARRAY of HASHES

    10/18

    * Manualo Overviewo Tutorialso FAQso Changes

    * Referenceo Languageo Functionso Operatorso Variables

    * Moduleso Moduleso Pragmaso Utilities

    * Misco License

    o Internalso Platforms

  • 8/9/2019 Access and Printing of an ARRAY of HASHES

    11/18

  • 8/9/2019 Access and Printing of an ARRAY of HASHES

    12/18

  • 8/9/2019 Access and Printing of an ARRAY of HASHES

    13/18

  • 8/9/2019 Access and Printing of an ARRAY of HASHES

    14/18

  • 8/9/2019 Access and Printing of an ARRAY of HASHES

    15/18

    Anggi

  • 8/9/2019 Access and Printing of an ARRAY of HASHES

    16/18

  • 8/9/2019 Access and Printing of an ARRAY of HASHES

    17/18

    annggi

    jupri

  • 8/9/2019 Access and Printing of an ARRAY of HASHES

    18/18