Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP WITH ASSOCIATIVE ARRAYS (newbie to hashes)

Status
Not open for further replies.

jez

Programmer
Joined
Apr 24, 2001
Messages
370
Location
VN
Hey there,

I am trying to get to grips with Hashes and they're causing me alot of problems.
I have the camel book, but it doesn't elaborate on how to populate a hash, other than hard coding it.

I want to populate a hash with key=>value pairs read in from a file (to an array) and split into pairs.
I am trying to loop through the array and populate the hash.
CODE:

for($i=2;$i<@indat;$i++) {
($key, $value) = (split '&', $indat[$i]);
$hash{$key} => $value;}

I have tried to use a temporary array to print the contents of the hash at this point, but it doesn't work.

Later in the program, I am trying to tie the values of an array (which are the same as the keys in the hash) to their value from the hash.
The reason for this is that the array is file names
(e.g.- abc.pdf), and the hash has these file names and also their titles. I want to create links to the files, but have the text of the link as the title of the file (from the hash).
CODE:

foreach $cuz(@cfiles) {
print &quot;<tr><td><a href='$relcurfile$cuz' target='_blank'>$hash{'$cuz'}</a></td></tr>&quot;;}

but again this doesn't work.
I think I getting the problem at the populating stage, but I can't find any decent info on hash population on the fly.

Any help or pointers to info on the web would be really helpful.

Thanks

Jez

:-)
 
Jez,

I am not sure exactly what your problem is but you may try the following. Make your array in the form of a key->value pair. For instance:

my $array = (&quot;name&quot;,&quot;jez&quot;,&quot;color&quot;,&quot;blue&quot;,&quot;language&quot;,&quot;perl&quot;);

Then you can assign this array to a hash. The hash will be assinged with appropriate key->value pairs.

my %hash = @array;


This may help you in the short term if you can't get the hash to store properly. Try finding a good tutorial on Perl. I have enclosed one that I like but I don't how much detail it goes into with hashes. You may also want to get a book called Learning Perl, by O'Reilly. This has a lot of good examples.


Brandon
 
At least one of the problems, is the following line:

$hash{$key} => $value;}

This line should be:

$hash{$key} = $value;}


 
Specifically - this
$hash{'$cuz'}
needs to loose the single quotes. They are preventing the intrepretation of the var, $cuz.

MORE GENERALLY,

#!/usr/local/bin/perl
# a mini hash faq

%general_hash_form = ('key1','value1',
'key2','value2',
'key3','value3');

# more specifically, build a hash...car colors
%car_colors = ('porche','red','vw','blue','ford','green');

# what color is the vw
$car = 'vw';
$color = $car_colors{$car};
print &quot;The $car is $color.\n&quot;;

# change the color of the vw
$car_colors{'vw'} = 'black';
print &quot;The vw is now $car_colors{vw}.\n&quot;;

# add another car and color
$car_colors{'bugatti'} = 'hunter green with tan leather';
# ok , so it's more that just the color, sounds nice though

print &quot;\nQuestion: what color is the bugatti?\n&quot;;
print &quot;Answer: $car_colors{'bugatti'}\n\n&quot;;

# output an HTML table of cars and colors
print '<table>';
# you could do
# @cars = keys(%car_colors);
# foreach $car (@cars) { print table stuff; }
# but, a little more concisely,

foreach $car (keys(%car_colors))
{
print &quot;<tr><td>$car</td><td>$car_colors{$car}</td><tr>\n&quot;;
}
print '</table>';

# ps - check out the bugatti # 1001 hp! from a vw company?

HTH


keep the rudder amid ship and beware the odd typo
 
Thankyou, all of you for your help.
I have managed to get it working now, using a few other things to tidy up my data first;-

populating the hash;-
## first to get rid of the first 2 lines in the @indat array
## I did this (twice), instead of starting the loop at 2
push @first_lines, shift(@indat);

## then
for(@indat) {
my @temp = split(/\&/);
$hash{$temp[0]}=$temp[1]; ## thanks raider2001
}

## to print it I used the same loop through the array

print &quot;<tr><td><a href='$relcurfile$cuz' target='_blank'>$hash{$cuz}</a></td></tr>&quot;;

## and there I got rid of the single quotes (cheers goBoating).


I also found errors in the array contents I was reading in from the text files, which threw me aswell for a while.

Although I have been using perl for about 3 or 4 months now, this was my first real exploration of %hash 's and getting them to work makes things much easier.

Thanks again

Jez

:-):-):-):-):-):-):-):-):-):-);-)
 
Hashes can be incredibly handy once you learn how to use them. Now that you got the basics down, you can try a hash of hashes, or a hash of arrays, or an array of hashes. :-)
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Jez, my preference, rather than
-----------------------

for(@indat) {
my @temp = split(/\&/);
$hash{$temp[0]}=$temp[1]; ## thanks raider2001
}


Change to:
--------

for(@indat) {
my ($key, $value) = split(/\&/);
$hash{$key}=$value;
}

To me, it's clearer to use $key and $value, rather than using [0] and [1], but really it's just personal preference. As you know now, both ways work fine.

Then to print out your hash, you can do:

foreach my $key (keys %hash) {
print &quot;key = $key, value = $hash{$key}\n&quot;;
}

OR
---

while ( my($key,$value) = each %inc_fields ) {
print &quot;key = $key, value = $value\n&quot;;
}

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Yeah, RE: Hashes of Hashes or Arrays of Hashes (etc complex data structures)....

I have got to grips alot more with hashes from solving this problem, and as far as more complex data structures are concerned, that was why this particular bit of code had me stumped for a few days, because it was calling on the hash from a loop through a completely seperate array.
Also this array had to be the index of the loop to preserve the order of the data.

Thanks hmerrill, while trying to get this to work I did try out alot of variations on the code, y'know how you change things that really shouldn't be causing a problem, but you change them anyway to something you know can't cause a problem...yeah well that happenned there, and now the working version of this does it the way you suggested (which was the way it started..[see first posting on this thread]).

Anyway, as with all tricky bits of a programming language, it takes having to solve one problem, and get help and trawl the internet, and so now I think I can say i understand how to use hashes alot more than I did.

Cheers!!!


Many thanks

Jez

:-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top