CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
DataBase ==> Associative array



Jul 1, 2000, 4:12 PM

Post #1 of 4 (892 views)
DataBase ==> Associative array Can't Post

My question is how should a database containing the information for a associative array should look like?
a 8979087987
b 8902342344
and my second question is in your program how do you load the content of the associative array database in to a % associative array?

3) can the value for the keyword in the database be a adress such as

Simon N
Interaid Webhosting Services

User / Moderator

Jul 1, 2000, 6:00 PM

Post #2 of 4 (893 views)
Re: DataBase ==> Associative array [In reply to] Can't Post

3) Remember that the keys in a hash must be unique, but the key can be just about any string. So using URLs is fine.

1) Any consistent format will work, but placing one key value pair on each line has some real advantages when it comes time to read it back into a hash (as will be seen in the next section).

2) Here is some code that will do what you want...

intro.html<->Title Page
intro2.html<->Second Title

Subroutine to call...
sub Get_Hash
my ($file) = @_;
my @tohash;
open (SOURCE, $file);
while (<SOURCE> ) {
push (@tohash, split ('<->', $_));
close SOURCE;
return (@tohash);

How to call the routine...
my %hash = &Get_Hash ("hashdata.txt");

I chose to use '<->' as the delimiter, but you can change that to whatever you want as long as it is not in the data.

One word of warning. If you have a line that does not have exactly two things and one delimeter you will have problems when the array @tohash becomes %hash. If your data is not nice, then you will have to add some code to correct it. But that is left as an exercise for the reader. Smile

[This message has been edited by rGeoffrey (edited 07-01-2000).]


Jul 1, 2000, 8:17 PM

Post #3 of 4 (892 views)
Re: DataBase ==> Associative array [In reply to] Can't Post

Geoffery thank you very much for your post. I appracite it. But as you see i am a very begginer, and the main purpose of the post was so i could learn something out of it. I don't have a program that is missing those codes. So if you do not mind would you please comment out heavily what each line in the code does and etc. I want to learn and that was the goal of my previous mesg.
Thanks bud.

User / Moderator

Jul 3, 2000, 10:14 AM

Post #4 of 4 (892 views)
Re: DataBase ==> Associative array [In reply to] Can't Post

01 sub Get_Hash
02 {
03 my ($file) = @_;
04 my @tohash;
05 open (SOURCE, $file);
06 while (<SOURCE> ) {
07 chomp;
08 push (@tohash, split ('<->', $_));
09 }
10 close SOURCE;
11 return (@tohash);
12 }

Okay, here are some words about each part...

01 sub Get_Hash
02 {

Start the subroutine Get_Hash

03 my ($file) = @_;

In line 3 we get the one parameter passed to the subroutine. $file is a filename that will be opened on line 5.

Line 3 is just one of three common ways to get the parameter. You could also use either of these alternates versions if you feel more comfortable. Remember that shift will work on @_ by default so these work identically.

03a my $file = shift @_;
03b my $file = shift;

04 my @tohash;

The @tohash array is where we will store all the key/value pairs from the file.

05 open (SOURCE, $file);

Open the $file for reading.

06 while (<SOURCE> ) {

The while loop will run as long as there is another line in the file to read. Each time through the loop it will read in the next line and place it in the variable $_. Using my sample data, the first time through we will have...

($_ eq "intro.html<->Title Page\n")

Note that the newline "\n" is on the end of the line. This is the real reason that it is best to put one pair on each line, thus we can read it with this method and we are splitting the input file on "\n".

07 chomp;

Just as shift works by default on @_, chomp works by default on $_. So line 7 will removes the "\n" from the end of $_. If you want to be a little more straight forward about what is happening here you could use this alternate...

07a chomp $_;

08 push (@tohash, split ('<->', $_));

Line 8 is the most important line in Get_Hash. It takes the string that is now missing the newline from the end, and breaks it into pieces by looking for the first parameter to split, in my case that is '<->'. By the time the loop is done after the third time through my data we will have an array with these values...

'intro.html', 'Title Page', 'intro2.html', 'Second Title', 'abstract.html', 'Abstract'

09 }

End the loop in line 9

10 close SOURCE;

Close the file now that we are done reading from it.

11 return (@tohash);

Return the list of all the values in @tohash to the place that called Get_Hash.

12 }

Line 12 ends the subroutine.

Any time you pass an array or a hash into a subroutine or return it from one, it is flattened. That is the other tricky thing happening here. The line where we call Get_Hash is asking for a hash to be returned...

my %hash = &Get_Hash ("hashtext.txt");

But Get_Hash is actually returning an array. This works as long as the array has an even number of items that alternate key, value, key, value. And is why I included the warning that each line should give us exactly 2 items, a key and a value.

What %hash sees being sent to it with this data is...

%hash = ('intro.html', 'Title Page', 'intro2.html', 'Second Title', 'abstract.html', 'Abstract');

This is the same as...

%hash = ('intro.html' => 'Title Page',
'intro2.html' => 'Second Title',
'abstract.html'=> 'Abstract');

When you are creating and filling your own hashes either form will work, but the syntactic sugar of the second version makes it the one that is usually used.

So by the time we are done we have a %hash with all the key/value pairs that were in $file with one pair on each line.

I hope that answers most of the questions. If not, let me know which parts are still confusing and I will say a few more words on just those parts.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives