Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > simple hash

Reply
Thread Tools

simple hash

 
 
Nene
Guest
Posts: n/a
 
      10-02-2009
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.


 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      10-02-2009
>>>>> "N" == Nene <(E-Mail Removed)> writes:

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

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
 
 
 
Justin C
Guest
Posts: n/a
 
      10-02-2009
On 2009-10-02, Nene <(E-Mail Removed)> wrote:
> 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.

--
Justin C, by the sea.
 
Reply With Quote
 
Ben Bullock
Guest
Posts: n/a
 
      10-03-2009
On Oct 2, 6:52*pm, Justin C <(E-Mail Removed)> wrote:
> On 2009-10-02, Nene <(E-Mail Removed)> wrote:
>
>
>
>
>
> > 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];
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Is there a hash algorithm with direct access to hash elements andreference count? Bo Peng C++ 4 03-12-2006 05:57 AM
In 'HashMap.put', "if (e.hash == hash && eq(k, e.key))" ? Red Orchid Java 3 01-30-2006 07:04 PM
standard library for hash table storage and hash algorithm Pieter Claassen C Programming 1 08-04-2004 03:11 AM



Advertisments