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: Intermediate:
accessing hash of arrays:

 



liven
Novice

Mar 10, 2008, 1:47 PM

Post #1 of 3 (400 views)
accessing hash of arrays: Can't Post

loading an array of hashes:



Code
sub read_bingo_data 
{
open (excel_data, $descriptive_file) || die("Couldn't open bingo description file\n");
@data_lines=<excel_data>;

foreach $record (@data_lines)
{
@holder = split(/,/, $record);
$bingo_name = shift(@holder);
push@{$descriptive_hash->{'bingo'}{$bingo_name}},"@holder[1..4]";
}


close(excel_data);
}




But when I try to step back through the array It only shows up as one element.

I use this line:


Code
@holder = split(/,/, $record);


and I use this line:

Code
$bingo_name = shift(@holder);


to name the array in the hash. I need each array to be named the first (element[0]) of the array or line from the excel data...

to add the data from an excell sheet to an array. but I don't need the first element of the array so that is why I do this:


Code
push@{$descriptive_hash->{'bingo'}{$bingo_name}},"@holder[1..4]";



Everything works, except the line:

@{$descriptive_hash->{'bingo'}{$bingo_name}},"@holder[1..4]"

acts as if it is just one element not 4....

What did I do wrong? I want it to be 4 separate elements that I can iterate through.


KevinR
Veteran


Mar 10, 2008, 3:38 PM

Post #2 of 3 (399 views)
Re: [liven] accessing hash of arrays: [In reply to] Can't Post

When you double-quote an array it becomes a scalar, so remove the double-quotes from around the array slice. The whole thing can be written like so:


Code
	foreach $record (@data_lines)   
{
@holder = split(/,/, $record);
push@{$descriptive_hash->{'bingo'}{$holder[0]}},@holder[1..4];
}


shifting the array and assigning the first element to a scalar are really unecessary if that is all you are doing with @holder.
-------------------------------------------------


liven
Novice

Mar 11, 2008, 4:28 AM

Post #3 of 3 (392 views)
Re: [liven] accessing hash of arrays: [In reply to] Can't Post

Kevin...

Now look I am going to try to say this in as polite a way a possible.

Your one smart fella.

Thanks!

 
 


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

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