Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing elements of data structure 1

Status
Not open for further replies.

tonykent

IS-IT--Management
Jun 13, 2002
251
GB
I am having difficulty accessing the lowest elements of this data structure:

Code:
$VAR1 = {
          'MoveCompletedEndsBack' => '0',
          'NewTasksEstimated' => '1',
          'Resources' => {
                         'Resource' => [
                                       {
                                         'RemainingCost' => '0',
                                         'UID' => '0',
                                         'PeakUnits' => '0.00',
                                         'ACWP' => '0.00',
                                         'Type' => '1',
                                         'CreationDate' => '2011-01-19T09:06:00',
                                         'CV' => '0.00',
                                         'OvertimeRate' => '0',
                                         'BCWS' => '0.00',
                                         'StandardRate' => '0',
                                       },
                                       {
                                         'RemainingCost' => '5',
                                         'UID' => '1',
                                         'PeakUnits' => '3.00',
                                         'ACWP' => '0.00',
                                         'Type' => '1',
                                         'CreationDate' => '2011-01-19T09:06:00',
                                         'CV' => '0.00',
                                         'OvertimeRate' => '0',
                                         'BCWS' => '0.00',
                                         'StandardRate' => '0',
                                       },
                                     ]
                       },
          'DefaultFinishTime' => '17:00:00',
          'DefaultStartTime' => '08:00:00',
          'CreationDate' => '2009-11-17T11:25:00',
          'FiscalYearStart' => '0',
          'WeekStartDay' => '1',
          'NewTasksAreManual' => '0',
          'MinutesPerDay' => '480'
        };

I would like to print out the 'RemaingCost' values (0 and 5). This does not work:

Code:
print "$tree->{Resources}->{Resource}->{RemainingCost}\n";

That just returns 'Not a HASH reference'. Can anyone point me in the right direction?
 
Hi

You missed an array : the structure is HASH HASH ARRAY HASH.
So use [tt][navy]$tree[/navy][teal]->[/teal][teal]{[/teal]Resources[teal]}[/teal][teal]->[/teal][teal]{[/teal]Resource[teal]}[/teal][teal]->[[/teal][purple]0[/purple][teal]]->[/teal][teal]{[/teal]RemainingCost[teal]}[/teal][/tt] and change that 0 index to get the other RemainingCost.


Feherke.
 
Also, per perldsc - References, it is unnecessary to use the -> operator between hashes and arrays as the {} and [] automatically dereference. The following is sufficient:

$tree->{Resources}{Resource}[0]{RemainingCost}

- Miller
 
Thanks for the additional comments MillerH.

@feherke: I see that I can print out the second array element with $tree->{Resources}{Resource}[1]{RemainingCost}, but as that part of the data structure is actually repeated many times I realise now that I could do with putting it into a loop to print/access them all. With an array I would normally use

Code:
foreach $element[@array] {
    do something with $element

or

Code:
for ($i=0;$i<$#array;$i++) {
     do something with $array[$i]

but I'm uncertain as to how to either find the size of this array inside the hash/hash or how to make reference to it. Could you assist again?
 
Hi

Perl:
[b]print[/b] scalar @[teal]{[/teal][navy]$tree[/navy][teal]->[/teal][teal]{[/teal]Resources[teal]}[/teal][teal]->[/teal][teal]{[/teal]Resource[teal]}}[/teal][teal],[/teal][green][i]" values :\n"[/i][/green][teal];[/teal]
[b]for[/b] [teal]([/teal][navy]$i[/navy][teal]=[/teal][purple]0[/purple][teal];[/teal][navy]$i[/navy][teal]<[/teal]@[teal]{[/teal][navy]$tree[/navy][teal]->[/teal][teal]{[/teal]Resources[teal]}[/teal][teal]->[/teal][teal]{[/teal]Resource[teal]}}[/teal][teal];[/teal][navy]$i[/navy][teal]++)[/teal] [teal]{[/teal]
  [b]print[/b] [navy]$i[/navy][teal],[/teal][green][i]' : '[/i][/green][teal],[/teal][navy]$tree[/navy][teal]->[/teal][teal]{[/teal]Resources[teal]}[/teal][teal]->[/teal][teal]{[/teal]Resource[teal]}[/teal][teal]->[[/teal][navy]$i[/navy][teal]]->[/teal][teal]{[/teal]RemainingCost[teal]}[/teal][teal],[/teal][green][i]"\n"[/i][/green][teal];[/teal]
[teal]}[/teal]
Code:
2 values :
0 : 0
1 : 5
Or without counter, if knowing the array index is not important :
Code:
[b]foreach[/b] [navy]$one[/navy] [teal]([/teal]@[teal]{[/teal][navy]$tree[/navy][teal]->[/teal][teal]{[/teal]Resources[teal]}[/teal][teal]->[/teal][teal]{[/teal]Resource[teal]}}[/teal][teal])[/teal] [teal]{[/teal]
  [b]print[/b] [navy]$one[/navy][teal]->[/teal][teal]{[/teal]RemainingCost[teal]}[/teal][teal],[/teal][green][i]"\n"[/i][/green][teal];[/teal]
[teal]}[/teal]

Feherke.
 
My thanks once again Feherke, that is working beautifully.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top