By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,661 Members | 1,330 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,661 IT Pros & Developers. It's quick & easy.

OOP Beginner needs help w/ anonymous array / hash

P: 2
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:
Expand|Select|Wrap|Line Numbers
  1. push (@{$year_index {$year}}, $rlEntry);         # By Year
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. open (F, "oscar.txt") || die "Could not open database: $:";
  2. %category_index = (); %year_index = ();
  3. while ($line = <F>) {
  4.     chomp $line;
  5.     ($year, $category, $name) = split (/:/, $line);
  6.     create_entry($year, $category, $name) if $name;
  7. }
  8.  
  9. sub create_entry {             # create_entry (year, category, name)
  10.     my($year, $category, $name) = @_;
  11.     # Create an anonymous array for each entry
  12.     $rlEntry = [$year, $category, $name];
  13.     # Add this to the two indices
  14.     push (@{$year_index {$year}}, $rlEntry);         # By Year
  15.     push (@{$category_index{$category}}, $rlEntry);  # By Category
  16. }
  17.  
Jul 14 '08 #1
Share this Question
Share on Google+
3 Replies


KevinADC
Expert 2.5K+
P: 4,059
I already replied to this question on another forum, I think it was perlguru.
Jul 14 '08 #2

P: 2
Yes you replied: "push @array, $foo;

the reference does the same thing except the array is the value of a hash key instead of just an array. This is also called complex data and is discussed in detail in many perl books and also online"

I got this from a perl book but still can't figure it out.

And I understand :
push(@array,$foo) put the value of $foo into array @array. This is simple.

But my confusion is:
$rlEntry = [$year, $category, $name];
push (@{$year_index {$year}}, $rlEntry); is complex.

I know that:
1) $rlEntry = [$year, $category, $name]; referrences an anonymous array.
2) $year_index {$year}} is a hash named year_index with key $year.
3) push (????,$rlEntry); puts $rlEntry somewhere.
4) I don't understand: @{$year_index {$year}}, what is the array name? Can this be done simpler?
Jul 15 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
Yes you replied: "push @array, $foo;

the reference does the same thing except the array is the value of a hash key instead of just an array. This is also called complex data and is discussed in detail in many perl books and also online"

I got this from a perl book but still can't figure it out.

And I understand :
push(@array,$foo) put the value of $foo into array @array. This is simple.

But my confusion is:
$rlEntry = [$year, $category, $name];
push (@{$year_index {$year}}, $rlEntry); is complex.

I know that:
1) $rlEntry = [$year, $category, $name]; referrences an anonymous array.
2) $year_index {$year}} is a hash named year_index with key $year.
3) push (????,$rlEntry); puts $rlEntry somewhere.
4) I don't understand: @{$year_index {$year}}, what is the array name? Can this be done simpler?
This is an array:

@{$year_index {$year}}

just like:

@array

or just like:

$array = [1,2,3,4];

all of the above are arrays, they only differ in the syntax used to create them. Read the page I posted a link to for you on the other forum and it is discussed in detail.

To answer your question #4, the answer is no. That is the simplest way to do what the code is doing, you could do it other more convoluted ways. Once you get the hang of complex data types in perl, and it is easy, you will wonder how you ever wrote code without using them.
Jul 15 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.