Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old February 6th, 2001, 10:50 PM
underwarez underwarez is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Location: toronto, ontario, canada
Posts: 10 underwarez User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old February 7th, 2001, 01:08 AM
dwarf dwarf is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 300 dwarf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
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};


Reply With Quote
  #3  
Old February 7th, 2001, 08:25 AM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Wink

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

Reply With Quote
  #4  
Old February 7th, 2001, 12:34 PM
dwarf dwarf is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 300 dwarf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Wink

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...

Reply With Quote
  #5  
Old February 12th, 2001, 12:40 AM
underwarez underwarez is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Location: toronto, ontario, canada
Posts: 10 underwarez User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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!!!

Reply With Quote
  #6  
Old February 12th, 2001, 10:15 AM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Wink

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.

Reply With Quote
  #7  
Old February 15th, 2001, 12:13 PM
timbuckthree timbuckthree is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Posts: 4 timbuckthree User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #8  
Old February 16th, 2001, 03:56 PM
jasavi jasavi is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Location: Caracas, Venezuela
Posts: 1 jasavi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to jasavi
Talking Perl hashes

I think one of the best explanations about hashes (and about Perl) is here:

URL

Happy coding!

Jaime Sanmartin

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Associative array of arrays


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway