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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Accessing a array of arrays within a hash

 



awilson
Deleted

Jan 15, 2001, 9:35 AM

Post #1 of 9 (3222 views)
Accessing a array of arrays within a hash Can't Post

I am creating a "Excel spreadsheet -like" environment with a perl script. I would like to have a array of arrays inside a hash (like have a worksheet inside of a workbook). This allows me to identify each array of array and populate it.

I have been able to create everything and even get row counts and the names of each array of arrays. Where I get lost is how to access the individual members of the arrays. I can accessing them one by one but I cannot get the whole thing to print (it give back array references). I need this so that I can make the array a table in a HTML page.

Any ideas... it would be appreciated.



Thanks..Allen


Kanji
User / Moderator

Jan 16, 2001, 3:15 PM

Post #2 of 9 (3209 views)
Re: Accessing a array of arrays within a hash [In reply to] Can't Post

Use @{$ref} to dereference an array ref (ie, @{$hash{'key'}[1]} to get the second row of $hash{'key'} ).



awilson
Deleted

Jan 16, 2001, 3:31 PM

Post #3 of 9 (3206 views)
Re: Accessing a array of arrays within a hash [In reply to] Can't Post

Already tried that...

I can get at one element with the code:
@{$hash{'key'}[1][1]}

will give me one item but when I tried to reference the entire array with a foreach loop is where I am getting hung at

Thanks..Allen


Kanji
User / Moderator

Jan 16, 2001, 9:59 PM

Post #4 of 9 (3201 views)
Re: Accessing a array of arrays within a hash [In reply to] Can't Post

You appear to have ammended my suggestion to suit your needs erroneously, so for clarification ...

If you reference one element ( $hash{'key'}[1][1] ) you'll get one element.

If you reference the array instead ( $hash{'key'}[1], note the lack of a second [1] ), you'll get the whole array.



awilson
Deleted

Jan 17, 2001, 12:32 PM

Post #5 of 9 (3192 views)
Re: Accessing a array of arrays within a hash [In reply to] Can't Post

I tried your suggestion and it returned an array reference

Thanks..Allen


Kanji
User / Moderator

Jan 17, 2001, 7:38 PM

Post #6 of 9 (3189 views)
Re: Accessing a array of arrays within a hash [In reply to] Can't Post

Then you're going to have to post your code because I can't get @{ $hash{'key'}[1] } to work anyway other than you asked for, so you're either not using a hash of LoLs, or you're not misreading my posts.



BigRich
Novice

Jan 21, 2001, 11:45 AM

Post #7 of 9 (3176 views)
Re: Accessing a array of arrays within a hash [In reply to] Can't Post

Here are some simple examples that may help.

# Create a hash of arrays of arrays

## Define the second array of key 3
@k3a2vals = ('k3a2v1','k3a2v2','k3a2v3','k3a2v4');

%HofAsofAs = (

key1 => [['k1a1v1','k1a1v2','k1a1v3','k1a1v4'],['k1a2v1','k1a2v2','k1a2v3','k1a2v4']],
key2 => [['k2a1v1','k2a1v2','k2a1v3','k2a1v4'],['k2a2v1','k2a2v2','k2a2v3','k2a2v4']],
key3 => [['k3a1v1','k3a1v2','k3a1v3','k3a1v4'],[@k3a2vals]]

);

#accessing the arrays of arrays in a key
# and the values in each of those arrays

$cnt = 1;
$key_to_access = 'key3';

foreach $array_ref (@{$HofAsofAs{$key_to_access}}) {
print "Array number $cnt in $key_to_access contains:<p>";

foreach (@$array_ref) {
print $_.", ";
}

print "<p>";
$cnt++;
}


#accessing array values within the arrays

print "The elements in key three array 2 are @{$HofAsofAs{'key3'}[1]} <p>";

#accessing a specified value from all of the arrays

$array_val = 3; # get the 4th element of all arrays

print "The 4th value of each array is:<p>";
foreach $HoAkey (keys %HofAsofAs) {
my $array_num = 0;

foreach (@{$HofAsofAs{$HoAkey}}) {
print $HofAsofAs{$HoAkey}[$array_num][$array_val].", ";
$array_num++;
}

}


BigRich



awilson
Deleted

Mar 1, 2001, 3:57 PM

Post #8 of 9 (3125 views)
Re: Accessing a array of arrays within a hash [In reply to] Can't Post

Sorry it has taken me so long to reply..but been out of the net for awhile.

I looked the code snippet you sent and I appreciate it but to be honest (and I think it is my ignorance) it confused me and I did not understand it. So I thought that I might put in what I am try to do and have you simplify what you do....i would appreciate it....

Let say I have an array of arrays...

@defArray1 = (["apple", "orange", "banana"], ["ham", "chicken", "turkey"],);

and I have the following hash that I want to add the information to
%HTMLTable( );

The code I am using to add the information to the hash is
$def = "fruit" (in the actual program this will be pulled in from a file)

$HTMLTables{$def} = @defArray;

any series of array of arrays could be added to the hash. The final goal will be to retrieve the array of arrays from the hash and create an HTML table.

Thanks...for any insight you may provide

Thanks..Allen


Danni
Deleted

Mar 22, 2001, 11:46 AM

Post #9 of 9 (3096 views)
Re: Accessing a array of arrays within a hash [In reply to] Can't Post

I may have this wrong but are you trying to do something like this?


## compile into hash
push @test1, "hello test1";
push @test1, "hello test1";
push @test2, "hello test2";
push @test2, "hello test2";
push @test3, "hello test3";
push @test3, "hello test3";
push @test4, "hello test4";
push @test4, "hello test4";
$main[0] = join(":1", @test1);
$main[1] = join(":1", @test2);
$main[2] = join(":1", @test3);
$main[3] = join(":1", @test4);
$hash{'somekey'} = join(":2", @main);


## Decompile from hash
foreach $array (split(":2", $hash{'somekey'})) {
print "| ";
foreach $record (split(":1", $array)) {
print "$record | ";

}
print "\n"
}


 
 


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

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