Hi Paul,

there are a couple of issues I could see in the code you sent,
let me point out some of them and hope those will help.

In general I'd recommend using the Dumper function of Data::Dumper
to check what kind of data structure you got.



On Tue, Nov 8, 2011 at 1:41 AM, Paul Rousseau
<paulrousseau...@hotmail.com> wrote:
> Hello Perl Community,
>
>    I want to build a complex hash.
>
> I want my hash structure to look like this:
>
> # %project = (<projectno1> => [<projdesc1>,
> #                                               <hourtotal1>,
> #                                               %days1 = (<day x> =>
> [<hours>,<desc>],
> #                                                               <day y> =>
> [<hours>, <desc>]
> #                                                              )
> #                                             ],
> #                                              <projdesc2>,
> #                                               <hourtotal2>,
> #                                               %days2 = (<day x> =>
> [<hours>,<desc>],
> #                                                               <day y> =>
> [<hours>, <desc>]
> #                                                              )
> #                                             ]
> #             )
>
> I am having trouble building up and populating the individual %days hash.
>
> Here is what I have so far.
>
> use strict;

I'd recommend adding "use warnings;" as well.

>
> my (
>       %project,
>       $projectno, $desc,
>       $day,
>       %days,
>       $i
>      );
>
> $projectno = "12345";
> "$desc = "testing first project";
>
> $project{$projectno} = [$desc, "0"];   # zero hours
>
> #later on, my code reads in some records such as
>
> # 3, 12, "painted a room"
> # 5, 6, "added a chair"
> # 14, 2, "made some calls"
>
> # for, say, project "12345"
>
> For my first test, I populated a smaller hash called %days.
>
> for ($i = 1; $i <=3; $i++)
>    {
>     ($day, $hour, $text) = split (',', <inline>);
>      $days{$day} = ($hour, $text);
>    }
>

If I am not mistaken you want to pass an array reference there
and not an array so need square brackets:

 $days{$day} = [$hour, $text];

Try this:
use Data::Dumper qw(Dumper);
print Dumper \%days;



> #
> # now that I have finished populating a smaller hash, I want to copy the
> hash on to the bigger hash.
> #
>
> push @{$project{$projectno}}, %days;

In this case perl will flatten the hash and you will push all the keys
and values of it to the array.
Try printing out the result with the Dumper.

I am almost sure you will want to write:

push @{$project{$projectno}}, \%days;

See the back-slash in-front of the hash.


regards
   Gabor

-- 
Gabor Szabo
Perl Tutorial:      http://szabgab.com/perl_tutorial.html
Perl Weekly:      http://perlweekly.com/
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to