TASdvlper has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I have the following code:

for ( my $i = 0; $i < scalar(@builds); $i++ ) { my ($str, $date, $time) = split(' ', `/msg/spgear/tools/bin/cset +ool fabx describe --milestone V${builds[$i]} | grep firstReportTime | + head -1`); $time{$builds[$i]} => { 'start_date' => $date, 'start_time' => $time, 'group' => 'integration' }; if ( $i != 0 ) { $time{$previous} => { 'end_date' => $date, 'end_time' => $time }; } $previous = $builds[$i]; } print "$_ $_->{'start_date'} <br> \n" for (sort keys %time);
But, at the end, I'm not getting any output. My guess is I'm not setting my hash table correctly. I've tried several things and I just can't seem to get it to work. Any help would be greatly appreciated.

Replies are listed 'Best First'.
Re: Setting a hash element to a anoymous hash
by kvale (Monsignor) on May 18, 2004 at 21:03 UTC
    There are two problems. First, assignment is denoted by '=', not '=>', which is a glorified comma:
    $time{$previous} = { 'end_date' => $date, 'end_time' => $time };
    Second, in the print statement, you need to dereferene the hash, not the key:
    print "$_ $time{$_}{start_date} <br> \n" for (sort keys %time);

    -Mark

Re: Setting a hash element to a anoymous hash
by CountZero (Bishop) on May 18, 2004 at 21:08 UTC
    A general comment: if you deal with such data-constructs, remember that Data::Dumper is your friend.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Setting a hash element to a anoymous hash
by Belgarion (Chaplain) on May 18, 2004 at 21:00 UTC

    I believe when the following code executes:

    if ( $i != 0 ) { $time{$previous} = { 'end_date' => $date, 'end_time' => $time }; }

    it's overwriting the previously stored hashref. In other words, you're not appending the new information, but replacing the old. You could use something like this:

    if ( $i != 0 ) { $time{$previous} = { %{$time{$previous}}, 'end_date' => $date, 'end_time' => $time }; }

    to add the new information to the end. This will dereference the previous hashref before adding the new code.

    Update Fixed the code to use an assignment. Also, see kvale's comment below.