The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by rei]

I tried out some of the code examples of
the book dmp, but for I couldn't make sense of them.
For example: at the first code "a simple hash". By storing reading
`cd.txt` from a file (instead stdin) I got the following:

Use of uninitialized value in hash element at cd_sol1.pl line 10, <D> line 1.
Use of uninitialized value in hash element at cd_sol1.pl line 10, <D> line 1.
Use of uninitialized value in hash element at cd_sol1.pl line 10, <D> line 2.
Use of uninitialized value in hash element at cd_sol1.pl line 10, <D> line 3.
Use of uninitialized value in hash element at cd_sol1.pl line 10, <D> line 4.
Use of uninitialized value in hash element at cd_sol1.pl line 10, <D> line 5.
Use of uninitialized value in hash element at cd_sol1.pl line 10, <D> line 6.
Use of uninitialized value in hash element at cd_sol1.pl line 10, <D> line 7.
Use of uninitialized value in hash element at cd_sol1.pl line 10, <D> line 8.
Use of uninitialized value in hash element at cd_sol1.pl line 10, <D> line 9.
Use of uninitialized value in hash element at cd_sol1.pl line 10, <D> line 10.
Use of uninitialized value in hash element at cd_sol1.pl line 10, <D> line 11.
Use of uninitialized value in hash element at cd_sol1.pl line 10, <D> line 12.
Use of uninitialized value in hash element at cd_sol1.pl line 10, <D> line 13.
In , 13 CDs were released.

Using the following code:

#!/usr/bin/perl -w

open(D, "cd.txt");

my %years;
while (<D>smilie {
#while (<STDIN>smilie {
# chomp;
my $year = (split / /)[3];
$years{$year}++;
}

foreach (sort keys %years) {
print "In $_, $years{$_} CDs were released.
";
}

I was wondering where I missed something ...

By the way, is there a debbuging funcionality on Perl ?

Thanks for any help.

Reinaldo
import-bot (20211) [Avatar] Offline
#2
Re: source code
[Originally posted by trelane]

> I tried out some of the code examples of the book

> Using the following code:
>
> #!/usr/bin/perl -w
>
> open(D, "cd.txt"smilie;
>
> my %years;
> while (<D>smilie {
> #while (<STDIN>smilie {
> # chomp;
> my $year = (split / /)[3];
> $years{$year}++;
> }
>
> foreach (sort keys %years) {
> print "In $_, $years{$_} CDs were released.
> ";
> }
>
> I was wondering where I missed something ...

The data that Dave talks about on page 21 of the book is tab seperated
and the regular expression he uses splits on tabs. You've changed the
code from

> my $year = (split / /)[3] # backslash-t

to

> my $year = (split / /)[3] # just a space

which will spilt on space not a tab.

Are you sure that the input data is loading corretly? If the split
doesn't have four single space seperated parts per line then $year
won't contain anything but undef...and that could be what the
warnings are warning you about. Also it would seem that the only
key in your hash is undef as the only line the program prints when
it runs is

In , 13 CDs were released.

which would seem to confirm it. Try making sure your data has at least 4
collums seperated by just one space, the final one of which contains the
year. (Or change to using tabs and backslash-t

> By the way, is there a debbuging funcionality on Perl ?

Congratulations on using the -w switch, that's often the right thing
to do. Also may I suggest that you try putting 'use strict' at the
top of your code as that often catches any typos or other sillyness
you may have accidentally done. I've debugged code for hours before
putting in use strict and then kicked myself for missing something it
catches straight away.

In terms of further debuggers there's a whole set of documentation
about the perl debugger that can be accessed with 'perldoc perldebug'
from the command line, but to be honest it's not beginners stuff
(I never use it myself.) Instead try using the Data:smilieumper module to
print out data structures when you're running your code to see what's
going on...this is what I tend to do.

For example, before the foreach loop you might want to write:

use Data:smilieumper;
print Dumper \%years;

To print out what the hash looks like. There's more info on
Data:smilieumper in section 3.3 of the book.