Re: defined on hash value does not work in my code :(



Thanks Chris,

Have used warnings but that does not give me any useful info on what is
happening. I have tried changing the unless to if but that gives me
different (still incorrect) results.

In an effort to debug this, I have tried to simplfy the code to the
simplest level. The code below is a working demonstration of the
problem. When run it gives the following output:-

debug: current = 00 next = cts = 2
debug: current = 01 next = cts = 3
debug: current = 02 next = cts = 4
debug: current = 06 next = cts = 5
debug: current = 07 next = cts = 6
debug: current = 08 next = cts = 7
debug: current = 09 next = cts = 8
debug: current = 12 next = cts = 9
debug: current = 13 next = cts = 10
debug: current = 14 next = cts = 11
debug: current = 15 next = cts = 12
debug: current = 16 next = cts = 13
debug: current = 17 next = cts = 14
debug: current = 18 next = cts = 15
debug: current = 19 next = cts = 16
debug: current = 20 next = cts = 17

When i am expecting this output:-

debug: current = 00 next = 01 cts = 1
debug: current = 01 next = 02 cts = 1
debug: current = 02 next = cts = 2
debug: current = 06 next = 07 cts = 2
debug: current = 07 next = 08 cts = 2
debug: current = 08 next = 09 cts = 2
debug: current = 09 next = cts = 3
debug: current = 12 next = 13 cts = 3
debug: current = 13 next = 14 cts = 3
debug: current = 14 next = 15 cts = 3
debug: current = 15 next = 16 cts = 3
debug: current = 16 next = 17 cts = 3
debug: current = 17 next = 18 cts = 3
debug: current = 18 next = 19 cts = 3
debug: current = 19 next = 20 cts = 3
debug: current = 20 next = cts = 4

I am trying to capture the "on" time periods for a pc that is switched
on and off throughout a 24 hour period.

Below is the simplified code I am using to replicate the problem. Any
advice would be gratefully received, thank you.

==== working example ====

#!/usr/bin/perl

use strict;
#use warnings;

my %db;

# initialize db with fake data for testing
# %db{month}{day}{computer}{hour} = {hour}

$db{"Jul"}{"Mon"}{"COMP1"}{'00'} = '00';
$db{"Jul"}{"Mon"}{"COMP1"}{'01'} = '01';
$db{"Jul"}{"Mon"}{"COMP1"}{'02'} = '02';
$db{"Jul"}{"Mon"}{"COMP1"}{'06'} = '06';
$db{"Jul"}{"Mon"}{"COMP1"}{'07'} = '07';
$db{"Jul"}{"Mon"}{"COMP1"}{'08'} = '08';
$db{"Jul"}{"Mon"}{"COMP1"}{'09'} = '09';
$db{"Jul"}{"Mon"}{"COMP1"}{'12'} = '12';
$db{"Jul"}{"Mon"}{"COMP1"}{'13'} = '13';
$db{"Jul"}{"Mon"}{"COMP1"}{'14'} = '14';
$db{"Jul"}{"Mon"}{"COMP1"}{'15'} = '15';
$db{"Jul"}{"Mon"}{"COMP1"}{'16'} = '16';
$db{"Jul"}{"Mon"}{"COMP1"}{'17'} = '17';
$db{"Jul"}{"Mon"}{"COMP1"}{'18'} = '18';
$db{"Jul"}{"Mon"}{"COMP1"}{'19'} = '19';
$db{"Jul"}{"Mon"}{"COMP1"}{'20'} = '20';

# print out info
foreach my $months (sort keys %db) {
foreach my $days (sort keys %{$db{$months}}) {
foreach my $computernames (sort keys %{$db{$months}{$days}}) {
my $cts = 1; # current time slot
foreach my $hours qw/00 01 02 03 04 05 06 07 08 09 10 11 12
13 14 15 16 17 18 19 20 21 22 23/ {
my $next = sprintf("%0.2d\n", ($hours + 1)) unless
$hours eq "23";
next unless $db{$months}{$days}{$computernames}{$hours};
# increment time slot if next value is non-existent
$cts++ unless
$db{$months}{$days}{$computernames}{$next};
printf "debug: \tcurrent = %s",
$db{$months}{$days}{$computernames}{$hours};
printf "\tnext = %s",
$db{$months}{$days}{$computernames}{$next};
printf "\t cts = %s\n", $cts;
};
}
}
}

# ==== end of working example ====

Chris wrote:
> saffy wrote:
> > Sorry, posted too early:-
> >
> > I have corrected the error and some others, but I still have the
> > problem:-
> >
> > Basically, the problem boils down to the following, in the code below:-
> >
> > this line works:-
> > $times1[$#times1 + 1] = sprintf "%s",
> > $db{$months}{$days}{$computernames}{$hours}
> > if ($db{$months}{$days}{$computernames}{$hours}
> > && $cts == 1);
> >
> > but this one doesn't:-
> > $cts++ unless defined($db{$months}{$days}{$computernames}{$next})
>
> It is working. It's just not doing what (I think) you're expecting. The
> above only increments $cts when
> $db{$months}{$days}{$computernames}{$next} is *not* defined, but what I
> think you what is to increment $cts when the hash *is* defined. Try
> changing the unless to if and see if that helps.
>
> > using the debugger shows that $next has the correct value i.e. (when
> > $hour = 00, $next = 01) but for some reason
> > $db{$months}{$days}{$computernames}{$next} does not appear to ever
> > contain a value, even when it should!!
> >
> > This is probably a silly syntax error on my part or there is something
> > about nested hashes that I don't understand.
> >
> I doubt it as perl would warn you of any syntax errors. You are using
> warnings, right?

.



Relevant Pages

  • Re: replace multiple spaces between words with single space.
    ... > Note that there are multiple blanks between the words. ... use strict; ... use warnings; ... syntax error at v.pl line 2, ...
    (comp.lang.perl.misc)
  • Re: Getting an error: Syntax error at the end of input
    ... So is the error because of these warnings or what???? ... But I am not finding any syntax error at the end ..... ... a missing right brace or right parenthesis, or an extra left brace or ... left parenthesis. ...
    (comp.unix.programmer)
  • Getting an error: Syntax error at the end of input
    ... I have one program and when I try to copile it using the make file it ... So is the error because of these warnings or what???? ... But I am not finding any syntax error at the end ..... ... I even not understanding what sohuld I ...
    (comp.unix.programmer)
  • syntax error at the end of input
    ... I have one program and when I try to copile it using the make file it ... So is the error because of these warnings or what???? ... But I am not finding any syntax error at the end ..... ... I even not understanding what sohuld I ...
    (comp.unix.bsd.freebsd.misc)