Hello Gabor,
 
   you are correct on using the square brackets as opposed to the round 
brackets.
 
> $days{$day} = [$hour, $text];

Here is the Dumper output when I use the code,
 
push @{$project{$projectno}}, %days;

 
$VAR1 = {
          '2011-0101' => [
                           'SCADA Host Re-evaluation Project',
                           14,
                           3,
                           '6',
                           [
                             '4.00',
                             ''
                           ],
                           '3',
                           [
                             '6.00',
                             ''
                           ],
                           '5',
                           [
                             '4.00',
                             ''
                           ]
                         ]
        };
 
It does flatten out as you stated.
 
Here is the Dumper output when I use the code,
 
push @{$project{$projectno}}, \%days;
 
$VAR1 = {
          '2011-0101' => [
                           'SCADA Host Re-evaluation Project',
                           14,
                           3,
                           {
                             '6' => [
                                      '4.00',
                                      ''
                                    ],
                             '3' => [
                                      '6.00',
                                      ''
                                    ],
                             '5' => [
                                      '4.00',
                                      ''
                                    ]
                           }
                         ]
        };
 
 
When I execute the line,
 
print Dumper \%{$project{$projectno}[2]};
 
I still get the error,
 
Can't use string ("3") as a HASH ref while "strict refs" in use at 
c:\images\mactimesheet.pl line 40
1, <STDIN> line 1.
 
I will continue to experiment with the semantics. I believe I am close.
 
Paul
 
 
 

 

> Date: Tue, 8 Nov 2011 02:26:17 +0200
> Subject: Re: How to push a hash on to a hash of array entries
> From: [email protected]
> To: [email protected]
> CC: [email protected]
> 
> 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
> <[email protected]> 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
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to