simple hash

N

Nene

Why is this code not working?

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

my %years;
while (<DATA>) {
chomp;
my $year = (split /\t/)[3];
$years{$year}++;
}
foreach (sort keys %years) {
print "In $_, $years{$_} CDs were released.\n";
}

__DATA__
Bragg, Billy Workers' Playtime Cooking Vinyl 1987
Bragg, Billy Mermaid Avenue EMI 1998
Black, Mary The Holy Ground Grapevine 1993
Black, Mary Circus Grapevine 1996
Bowie, David Hunky Dory RCA 1971
Bowie, David Earthling EMI 1987
~


###

I keep getting:

rsimionis-macbook-pro:c2 Rod$ ./hash.pl
Use of uninitialized value in hash element at ./hash.pl line 9, <DATA>
line 1.
Use of uninitialized value in hash element at ./hash.pl line 9, <DATA>
line 2.
Use of uninitialized value in hash element at ./hash.pl line 9, <DATA>
line 3.
Use of uninitialized value in hash element at ./hash.pl line 9, <DATA>
line 4.
Use of uninitialized value in hash element at ./hash.pl line 9, <DATA>
line 5.
Use of uninitialized value in hash element at ./hash.pl line 9, <DATA>
line 6.
Use of uninitialized value in hash element at ./hash.pl line 9, <DATA>
line 7.
In , 7 CDs were released.
 
U

Uri Guttman

N> Why is this code not working?

a probable cause is below as well as some code review

N> #!/usr/bin/perl -w
N> use strict;
N> use warnings;

don't put -w and use warnings in. they basically do the same thing and
use warnings is better.

N> my %years;
N> while (<DATA>) {

don't use $_ as much as you do. use a named variable as it helps make
the code easier to understan.

N> chomp;

ever heard of indenting or formatting your code? if you need help, look
for perltidy which can do it for you.


N> my $year = (split /\t/)[3];

you are splitting on tabs. do you really know the data has tabs in it?
the data you pasted here seems to have spaces. now that could have
happened in pasting your data into your newsreader but it could be from
before. assuming spaces will force the split to return only 1 element as
it finds no tabs. indexing for the 4th element will get undef which is
what you are seeing.

also don't name a scalar and hash with such similar names. the hash
would be better named %year_cnt or something like that.

N> $years{$year}++;
N> }
N> foreach (sort keys %years) {

again, use a named variable here instead of $_

N> print "In $_, $years{$_} CDs were released.\n";
N> }

N> __DATA__
N> Bragg, Billy**Workers' Playtime**Cooking Vinyl**1987

those *'s are where you think tabs would be but they are spaces
here. check the actual data source or how you put it into DATA.

uri
 
J

Justin C

Why is this code not working?

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

my %years;
while (<DATA>) {
chomp;
my $year = (split /\t/)[3];
$years{$year}++;
}
foreach (sort keys %years) {
print "In $_, $years{$_} CDs were released.\n";
}

__DATA__
Bragg, Billy Workers' Playtime Cooking Vinyl 1987
###

I keep getting:

Use of uninitialized value in hash element at ./hash.pl line 9, <DATA>
line 1.

There are no tabs (\t) in your data. If you edit the data and make sure
that they are *real* tabs then it works. You may want to choose an
alternative field separator.


Justin.
 
B

Ben Bullock

Why is this code not working?
#!/usr/bin/perl -w
use strict;
use warnings;
my %years;
while (<DATA>) {
chomp;
my $year = (split /\t/)[3];
$years{$year}++;
}
foreach (sort keys %years) {
print "In $_, $years{$_} CDs were released.\n";
}
__DATA__
Bragg, Billy  Workers' Playtime  Cooking Vinyl  1987
###
I keep getting:
Use of uninitialized value in hash element at ./hash.pl line 9, <DATA>
line 1.

There are no tabs (\t) in your data. If you edit the data and make sure
that they are *real* tabs then it works. You may want to choose an
alternative field separator.

This works too:

my $year = (split /\t|\s{2,}/)[3];
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top