simple hash

Discussion in 'Perl Misc' started by Nene, Oct 2, 2009.

  1. Nene

    Nene Guest

    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.
    Nene, Oct 2, 2009
    #1
    1. Advertising

  2. Nene

    Uri Guttman Guest

    >>>>> "N" == Nene <> 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.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Oct 2, 2009
    #2
    1. Advertising

  3. Nene

    Justin C Guest

    On 2009-10-02, Nene <> 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.
    Justin C, Oct 2, 2009
    #3
  4. Nene

    Ben Bullock Guest

    On Oct 2, 6:52 pm, Justin C <> wrote:
    > On 2009-10-02, Nene <> 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];
    Ben Bullock, Oct 3, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Red Orchid
    Replies:
    3
    Views:
    1,026
  2. Pieter Claassen
    Replies:
    1
    Views:
    1,093
    CBFalconer
    Aug 4, 2004
  3. Bo Peng
    Replies:
    4
    Views:
    780
  4. rp
    Replies:
    1
    Views:
    494
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    596
    David A. Black
    Jul 2, 2008
Loading...

Share This Page