I'm teaching myself OOP by following examples. I can't figure out what this is doing for the life of me. I've read the explanation and still don't get it. Can anyone explain, plus is there a more obvious way to do this:
MY CONFUSION IS THIS STEP:BOOK'S EXPLANATION:push (@{$year_index {$year}}, $rlEntry); # By Year
Since we would like to retrieve entries by category or by year, we use a double indexing scheme. Notice that each push statement does a fair bit of work. It creates an entry in the index (if required), hangs an anonymous array off that entry (if required), and pushes the reference to the entry into that array.
DATA:CODE:1995:Actor:Nicholas Cage 1995:Picture:Braveheart 1995:Supporting Actor:Kevin Spacey 1994:Actor:Tom Hanks 1994:Picture:Forrest Gump 1928:Picture:WINGS
open (F, "oscar.txt") || die "Could not open database: $:"; %category_index = (); %year_index = (); while ($line = <F>) { chomp $line; ($year, $category, $name) = split (/:/, $line); create_entry($year, $category, $name) if $name; } sub create_entry { # create_entry (year, category, name) my($year, $category, $name) = @_; # Create an anonymous array for each entry $rlEntry = [$year, $category, $name]; # Add this to the two indices push (@{$year_index {$year}}, $rlEntry); # By Year push (@{$category_index{$category}}, $rlEntry); # By Category }
In reply to OOPs Beginner needs help w/ anonymous array / hash by ejaggers
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |