import-bot (20211) [Avatar] Offline
#1
Re: OK, so what am I missing?
[Originally posted by paulbeard]

> Well, you've done it almost exactly as I did in my solutions page
> which is linked from the source code page or just go directly to:
>
> http://www.spindoczine.com/epp/solutions.html

Well, I don't want to read the solutions prematurely. I think I tried to
forget they were available smilie

On the upside, I have for some reason never understood hashes all that week: I
hve always assumed they were more complicated. the light bulb is on, though
still a bit dim.

And I didn't read the text on 101 as saying those two while statements were
equivalent (and I have, from bitter experience, tried to program as
defensively as possible). I'll remember that for next time.

Thanks.
import-bot (20211) [Avatar] Offline
#2
Re: OK, so what am I missing?
[Originally posted by jandrew]


> Well, I don't want to read the solutions prematurely. I think I tried
> to forget they were available smilie

That's a good way to go -- they are there if one needs to peek, and
I'm here to clarify anything ... but you were sooo close smilie

> On the upside, I have for some reason never understood hashes all
> that week: I hve always assumed they were more complicated. the light
> bulb is on, though still a bit dim.

Hashes aren't complicated -- but unless you've used them before in
another language (and most languages don't have them as standard data
types), they'll feel a bit odd. Eventually you'll be using them for
a great deal of things though.

> And I didn't read the text on 101 as saying those two while
> statements were equivalent (and I have, from bitter experience, tried
> to program as defensively as possible). I'll remember that for next
> time.

Programming defensively is *never* a bad thing -- and there was nothing
wrong with your explicit tests: I just wanted to point out that this
is one area where the task is so common that Perl does the defensive
thing for you, and you'll see a lot of code that just does:

while(<FH>smilie{
chomp;
# ... more processing of $_
}

and that's a safe way of operating -- but it does assume that anyone
else who comes along to maintain it knows enough Perl to know that
assignment to $_ is being done for us and which operators/functions
work on $_ by default.

regards,
andrew
import-bot (20211) [Avatar] Offline
#3
[Originally posted by paulbeard]

Jeez, I feel pretty dumb. In exercise 6.6.1, I get what I want, but too much
of it. I get something like 800 lines of output from a 28 line data file. I'm
relived to see the math looks right, but why is the hash being printed out
repearedly? I'm not seeing where I ask for that smilie


#!/usr/bin/perl -w
use strict;

my %logintime;
while ( defined( $_ = <> )) {
next if m/^$/; # take care of trailing blank lines
my ($user, $sessiontime) = split(':', $_);
$logintime{$user} += $sessiontime;
foreach $user (keys %logintime) {
print "User: $user
Total time online: $logintime{$user}
minutes

";
}
}
import-bot (20211) [Avatar] Offline
#4
Re: OK, so what am I missing?
[Originally posted by jandrew]

Well, you've done it almost exactly as I did in my solutions page
which is linked from the source code page or just go directly to:

http://www.spindoczine.com/epp/solutions.html

The primary difference is that your output loop is inside your input
loop instead of following it. Remember, first we have to read in *all*
the data, adding the session times to the appropriate keys, then once
we are done reading all the input we can go through our data
structure (the simple hash) and create out report:

#!/usr/bin/perl -w
use strict;

my %logintime;
while ( defined( $_ = <> )) {
next if m/^$/; # take care of trailing blank lines
my ($user, $sessiontime) = split(':', $_);
$logintime{$user} += $sessiontime;
}

foreach my $user (keys %logintime) {
print "User: $user
";
print "Total time online: $logintime{$user}minutes

";
}

I changed your output to use two print statements as I think that
is easier to read and maintain. Also, for reading in data, remember
that Perl automatically handles assigning to $_ and checking if it
is defined (at least with 5.004 and later versions). So you can
safely replace

while ( defined( $_ = <>smilie) {

with just:

while (<>smilie {

as discussed on pages 101-103.

regards,
andrew