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:Nich olas Cage
1995:Picture:Br aveheart
1995:Supporting Actor:Kevin Spacey
1994:Actor:Tom Hanks
1994:Picture:Fo rrest Gump
1928:Picture:WI NGS
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
-
}
-
3 1500 KevinADC 4,059
Recognized Expert Specialist
I already replied to this question on another forum, I think it was perlguru.
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,$fo o) 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?
KevinADC 4,059
Recognized Expert Specialist
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,$fo o) 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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: laurie |
last post by:
Hi all,
I'm trying to help out a friend who has inherited a client with a PHP
shopping cart application. Neither of us know PHP, but I've been
muddling my way through, trying to get these old scripts working on a
new server with the most recent version of PHP. I've pretty much
taken care of all the various errors that were popping up. Most only
pointed out out non-fatal undefined or assumed variables. I've been
able to cure most of...
|
by: Anders Eriksson |
last post by:
Hello!
I'm a beginner at PHP and I wonder how I would go about to read a textfile
into an array(hash).
The textfile looks like this:
A/S=age/sex?
A/S/L=age/sex/location?
AA=alcoholics anonymous
|
by: Casey |
last post by:
Hi, I haven't being using perl for too long. Can someone explain the
correct way to get the sort function to recognize an anonymous function
declared as a hash value? Look at my sample code for clarification:
#!/usr/bin/perl
@my_array = qw( g a z f u q m i e b );
$hash{my_sort_sub} = sub { $a cmp $b };
$hash{test_routine} = sub { print "test_routine works\n" };
&{$hash{test_routine}};
|
by: R. Rajesh Jeba Anbiah |
last post by:
I could see that it is possible to have hash array using objects like
var hash = {"a" : "1", "b" : "2"};
Couldn't still findout how to declare hash array in Array.
var arr = new Array("a" : "1", "b" : "2"); doesn't work. Any hints? TIA
--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/
|
by: VK |
last post by:
Or why I just did myArray = "Computers" but myArray.length is
showing 0. What a hey?
There is a new trend to treat arrays and hashes as they were some
variations of the same thing. But they are not at all.
If you are doing *array", then you have to use only integer values for
array index, as it was since ALGOL.
| |
by: Stijn van Dongen |
last post by:
A question about void*. I have a hash library where the hash create
function accepts functions
unsigned (*hash)(const void *a)
int (*cmp) (const void *a, const void *b)
The insert function accepts a void* key argument, and uses the
functions above to store this argument. It returns something (linked to
the key) that the caller can store a value in. The actual key argument
is always of the same pointer type (as seen in the caller,...
|
by: anonymous |
last post by:
Hi Folks,
I have a form with two Dropdown list boxes, which get loaded with data from
Database.
DropDownList1 gets data from Table1
and
DropDownList2 gets data from Table2
Table1 has a parent child relationship with Table2 ( has a foreign key to
Table1)
|
by: sathyashrayan |
last post by:
Dear group,
pls go through the following function definition:
function at_show_aux(parent, child)
{
var p = document.getElementById(parent);
var c = document.getElementById(child);
var top = (c == "y") ? p.offsetHeight+2 : 0;
var left = (c == "x") ? p.offsetWidth +2 : 0;
|
by: nodrogbrown |
last post by:
hi
i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.
eg: in
char * mynames
mynames="mathew"
mynames="gordon_brown"
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |