RE: How to push a hash on to a hash of array entries

2011-11-08 Thread Paul Rousseau

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: szab...@gmail.com
 To: paulrousseau...@hotmail.com
 CC: perl-win32-users@listserv.activestate.com
 
 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 

Re: How to push a hash on to a hash of array entries

2011-11-08 Thread will trillich
On Tue, Nov 8, 2011 at 11:20 AM, Paul Rousseau
paulrousseau...@hotmail.comwrote:

 Here is the Dumper output when I use the code,

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


Right, so $project{$projectno} is an ARRAYREF, and each entry pushed in
this way will be a HASHREF.

Note that other entries may exist in your array before this loop, such as
'SCADA...' and 14 and 3 as shown below.

These are all valid:
print $project{$projectno}[0]; # SCADA...
print $project{$projectno}[1]; # 14
print $project{$projectno}[2]; # 3
print $project{$projectno}[3]{'6'}[0]; # 4.00
print $project{$projectno}[4]{'3'}[0]; # 6.00
etc.

$VAR1 = {
   '2011-0101' = [

 # the first three here are throwing you off
your game:

'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: szab...@gmail.com
  To: paulrousseau...@hotmail.com
  CC: perl-win32-users@listserv.activestate.com

 
  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
 

Re: How to push a hash on to a hash of array entries

2011-11-08 Thread Gabor Szabo
On Tue, Nov 8, 2011 at 7:47 PM, Paul Rousseau
paulrousseau...@hotmail.com wrote:
 I am getting closer, Gabor.

I am glad to hear that :)


 I had the wrong index. These two lines

 push @{$project{$projectno}}, \%days;
 print Dumper %{$project{$projectno}[3]};

I think it is better to always give a reference as a parameter to Dumper
(put the back-slash in front of the %)

 Now to get at the description within the %days hash. This next line prints a
 count of the array pointed at by the '3' key.

 print test 1  . @{${$project{$projectno}[3]}{3}};

 prints test 1 2


It looks too complex to me. I wonder if you could change the
strategy and work with smaller structure. That will be easier to comprehend.
Leaving spaces inside the brackets can also help.
It also seems you have an extra, unnecessary pair of curly braces:

This might be slightly more readable:

@{ $project{ $projectno }[3]{3} }

regards
   Gabor
   Perl Tutorial
   http://szabgab.com/perl_tutorial.html
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: How to push a hash on to a hash of array entries

2011-11-08 Thread Ken Cornetet
I usually find that when I start getting data structures so complicated that I 
have to think about it, it is time to move to objects. Using objects usually 
makes the complicated data structures go away.

Ken Cornetet 812.482.8499
To err is human - to moo, bovine.

-Original Message-
From: perl-win32-users-boun...@listserv.activestate.com 
[mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of Gabor 
Szabo
Sent: Tuesday, November 08, 2011 1:06 PM
To: Paul Rousseau
Cc: perl Win32-users
Subject: Re: How to push a hash on to a hash of array entries

On Tue, Nov 8, 2011 at 7:47 PM, Paul Rousseau paulrousseau...@hotmail.com 
wrote:
 I am getting closer, Gabor.

I am glad to hear that :)


 I had the wrong index. These two lines

 push @{$project{$projectno}}, \%days;
 print Dumper %{$project{$projectno}[3]};

I think it is better to always give a reference as a parameter to Dumper (put 
the back-slash in front of the %)

 Now to get at the description within the %days hash. This next line 
 prints a count of the array pointed at by the '3' key.

 print test 1  . @{${$project{$projectno}[3]}{3}};

 prints test 1 2


It looks too complex to me. I wonder if you could change the strategy and work 
with smaller structure. That will be easier to comprehend.
Leaving spaces inside the brackets can also help.
It also seems you have an extra, unnecessary pair of curly braces:

This might be slightly more readable:

@{ $project{ $projectno }[3]{3} }

regards
   Gabor
   Perl Tutorial
   http://szabgab.com/perl_tutorial.html
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: How to push a hash on to a hash of array entries

2011-11-07 Thread Gabor Szabo
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