ejaggers has asked for the wisdom of the Perl Monks concerning the following question:

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:
push (@{$year_index {$year}}, $rlEntry); # By Year
BOOK'S EXPLANATION:

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:
1995:Actor:Nicholas Cage 1995:Picture:Braveheart 1995:Supporting Actor:Kevin Spacey 1994:Actor:Tom Hanks 1994:Picture:Forrest Gump 1928:Picture:WINGS
CODE:
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 }

Replies are listed 'Best First'.
Re: OOPs Beginner needs help w/ anonymous array / hash
by psini (Deacon) on Jul 14, 2008 at 21:03 UTC

    Hope I understood the problem, but your code really need the <code>...</code> tags!

    You don't understand this: push (@{$year_index {$year}}, $rlEntry); so let's analyze it:

    In your code is defined %year_index, the prefix % says it's a hash, $year is a scalar; so $year_index{$year} is the element of the hash with key==$year.

    This element is a reference to an array, @{...} dereferences it so @{$year_index{$year}} is an array.

    In the end the code means: push the scalar $rlEntry into the array referenced by the element of the hash %year_index with key $year.

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Re: OOPs Beginner needs help w/ anonymous array / hash
by CountZero (Bishop) on Jul 14, 2008 at 21:11 UTC
    This has nothing to do with OOP. It is all about data-structures in Perl (Chapter 2 "Implementing Complex Data Structures." of "Advanced Perl Programming")

    Do you understand how the datastructure is set-up? If not, I suggest that you run the program and use Data::Dumper to dump the structure. It will clearly show how it works.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: OOPs Beginner needs help w/ anonymous array / hash
by moritz (Cardinal) on Jul 14, 2008 at 20:52 UTC
    Please read the Writeup Formatting Tips and updated your post, it needs <code>...</code> tags very badly.

    I'll try an explanation:

    push (@{$year_index {$year}}, $rlEntry)

    %year_index is a hash, and you access the pair with key $year.

    If there's already an array reference stored in $year_index{$year}, it pushes the value $rlEntry to this array. If not, it creates a new array, stores $rlEntry there, and stores the new array in $year_index{$year}.

    This feature is called autovivification.

    See also perlreftut on how the nested data structures and their syntax work.

Re: OOPs Beginner needs help w/ anonymous array / hash
by FunkyMonk (Bishop) on Jul 14, 2008 at 22:50 UTC