http://qs1969.pair.com?node_id=284566

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to get a count of how many unique data values I have in my text file.
The text file has data like this:
123 56 123 56 234 45 123 56 678 93 678 93
I need the results of my script to look like this:
234 45 1 123 56 3 678 93 2
Here is my attempt and its not printing any data:
my $file = 'filehere.txt'; open(DATA, "$file") || die "Can not open: $!\n"; my @array = (<DATA>); close(DATA); open(DATA, "$file") || die "can t open: $!\n"; my %hash = (); foreach $item (@array) { $hash{$item}++; } print "$item\t$hash{$item}\n"; close(DATA);

Replies are listed 'Best First'.
Re: Hash and count of data values
by LTjake (Prior) on Aug 18, 2003 at 13:53 UTC

    Along with what bm said, it looks like you're forgetting to chomp off any newline characters.

    use strict; my %count; while( <DATA> ) { chomp; $count{ $_ }++; } print "$_\t$count{ $_ }\n" for keys %count; __DATA__ 123 56 123 56 234 45 123 56 678 93 678 93

    With chomp:

    678 93 2 234 45 1 123 56 3

    w/o chomp:

    678 93 1 678 93 1 123 56 3 234 45 1

    --
    "To err is human, but to really foul things up you need a computer." --Paul Ehrlich

Re: Hash and count of data values
by monktim (Friar) on Aug 18, 2003 at 13:53 UTC
    I think you want to store the record read from the file into a scalar, not an array.
    use strict; use warnings; my %hash = (); while (<DATA>) { chomp; $hash{$_}++; } foreach (keys %hash) { print "$_ appears $hash{$_} time(s).\n"; } __DATA__ 123 56 123 56 234 45 123 56 678 93 678 93
      I cant believe how many answers I got. Thanks!!!
      It now prints what I wanted.
      Thank you to all who helped me!!!!
Re: Hash and count of data values
by bm (Hermit) on Aug 18, 2003 at 13:45 UTC
    print "$item\t$hash{$item}\n";

    should probably be something like

    foreach (keys %hash) { print "$_\t$hash{$_}\n"; }

    Note that you also have two sets of open and close calls in your snippet. Maybe you should look at one open/close with a while(<DATA>) {...} loop that populates your hash.
    --
    bm

Re: Hash and count of data values
by gjb (Vicar) on Aug 18, 2003 at 13:51 UTC

    You try and print a single value of the hash since the print statement is not in a loop construction.

    foreach my $key (keys %hash) { print "$key $hash{$key}\n"; }
    should print the collected counts.

    Incidently, I don't see why you open the data file twice since you don't read any data the second time.

    Personally I'd put the data in the hash while reading since this avoids slurping the whole file which can potentially be large.

    my %hash; my $file = 'filehere.txt'; open(DATA, "$file") || die "Can not open: $!"; while (<DATA>) { chomp($_); $hash{$_}++; } close(DATA);

    Hope this helps, -gjb-

Re: Hash and count of data values
by Cine (Friar) on Aug 18, 2003 at 13:53 UTC
    cat file | sort | uniq -c

    T I M T O W T D I

      *tweeet*

      Useless use of cat. Five yards, repeat the down.

      sort file | uniq -c

      :)

Re: Hash and count of data values
by Anonymous Monk on Aug 18, 2003 at 13:54 UTC
    Instead of print "$item\t$hash{$item}\n"; try print DATA "$item\t$hash{$item}\n";. Also avoid calling your filehande DATA as this is the filehandle of the DATA section of the Perl program. And lastly, you can use my @array = <DATA>; instead of my @array = (<DATA>);.
    Hope this helped.