|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
Perl Question:
I am writing a function that takes and array of numbers And will return an associative array of arrays i.e. num1->(value1, value2, value3, value4, value5, value6) num2->(value1, value2, value3, value4, value5, value7, value8) ... The problem is I found out I can implement an associative array of arrays. So how can I implement this function? Thanks in Advance. |
|
#2
|
|||
|
|||
|
You rad up on references. They are the key to Perl's complex data structures.
But just for the sake of it, an associative array (also called "the hash") of arrays could look something like this: Code:
%hash1 = { key1 => [1,2,3,4,5],
key2 => [6,7,8,9,10]};
or: Code:
$arrref = \@array1;
%hash1 = { key1 => $arrref};
|
|
#3
|
|||
|
|||
|
I don't really understand your question. You are saying you can implement a hash(associative array) of arrays, so then you want to know how to implement a function you wrote that implements a hash of arrays. I think I'm missing something, are you asking how you would return a hash of arrays from a function?
__________________
- dsb - ![]() Perl Guy |
|
#4
|
|||
|
|||
|
I think its a typo, that the man doesn't know how to implement hash-o-arrays. But, just in case, if he does want to know how to return the same structure from a subroutine, then the answer is pretty much the same. Use references. Thats all there is to it...
|
|
#5
|
|||
|
|||
|
How do I dereference the associated arrays
lets say I have stored values in the associative array, in a function called storeArray.
How do access elements in that associative array in another function called getValues? i.e. storeArray{ ... %hash1 = { key1 => [1,2,3,4,5], key2 => [6,7,8,9,10]}; ... return %hash1 } getValues{ my %hash1 = @_ ... } I have no clue what to do in the function getValues. Help!!! |
|
#6
|
|||
|
|||
|
First of all, in the sub where you create the HASH, you need to return a reference to that HASH, not the HASH itself. So that would be something like:
Code:
sub storeArray {
%hash = (
key1 => [1,2,3,4,5],
key2 => [6,7,8,9,0],
);
return \%hash; # returns a reference to the HASH
}
Also note that I used parentheses when creating the regular hash. You used curly braces. You could also create an anonymous hash and assign it to a scalar reference like so: Code:
sub storeArray {
$hash = {
key1 => [1,2,3,4,5],
key2 => [6,7,8,9,0],
};
return $hash; # don't return reference cause it already is
}
That is where you would use the curly braces to indicate that this is a HASH ref. As far accessing the values it would be something like: Code:
print $hash->{'key1'}->[0][2];
You should really do some research on references. Just using this code really won't help you unless you understand how it works. There are plenty of tutorials and such online for you to access. |
|
#7
|
|||
|
|||
|
I prefer to use nested foreach loops in getting data from hash of arrays or hash of hashes. It looks something like this:
Code:
foreach $key (keys %hash)
{
print "$key\n";
foreach $entry (@{$hash{$key}}) #uses anonymous array to get the data
{
print " $entry\n";
}
print "\n";
}
It's relatively simple once you're used to it. Good Luck, t |
|
#8
|
|||
|
|||
|
I think one of the best explanations about hashes (and about Perl) is here:
URL Happy coding! Jaime Sanmartin |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Associative array of arrays |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|