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

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

I am trying to do the following

I have some data which looks like

1,ABC,X,1.203000e+02 1,ABC,Y,7.830000e+00 2,DEF,X,1.212400e+02 2,DEF,Y,8.810000e+00 3,GHI,X,1.180700e+02 3,GHI,Y,8.550000e+00 5,JKL,X,1.193500e+02 5,JKL,Y,7.270000e+00 . . .

Anyway, I want to use the first column in each line as a unique key for a hash, (e.g. 1,2,3,5 .. and so on). I am doing this as follows

while (<NEWDATA>){ # where NEWDATA points to a file with the comma del +imited data $currLine = $_; # current, comma-delimited line chomp $currLine; @formatDataArray = split(/,/,$currLine); #split at comma push (@newArray, $formatDataArray[0]); # push first column into ne +wArray # print OUT "@newArray\n"; } foreach my $item(@newArray){ # get unique array elements $seen{$item}++; } # numerical sort on keys and add to array @uniq = sort {$seen{$a} <=> $seen{$b} } keys %seen; # print to see if things look right foreach my $element(@uniq){ print OUT "$element\n"; }

the results of the print command at the end are still random, i.e. not

1 2 3 5

Any ideas. I am sure I am overlooking something very trivial.

mndoci

"What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?"-Sherlock Holmes in 'A study in scarlet'

Replies are listed 'Best First'.
Re: Sorting a hash. What am I doing wrong?
by Enlil (Parson) on Jan 14, 2003 at 00:28 UTC
    what you are doing with this:
    @uniq = sort {$seen{$a} <=> $seen{$b} } keys %seen;
    is sorting the values stored in $seen{$a} against $seen{$b}, what you want is to sort the hash keys (not the hash values) then you want to just do something like the following:
    @uniq = sort {$a <=> $b } keys %seen;

    -enlil

      Duh

      And you are quite correct, of course.

      Thanks

      mndoci

      "What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?"-Sherlock Holmes in 'A study in scarlet'

Re: Sorting a hash. What am I doing wrong?
by Arien (Pilgrim) on Jan 14, 2003 at 00:45 UTC

    Its name suggests that @uniq should be filled like this:

    my @uniq = sort { $a <=> $b } grep { $seen{$_} == 1 } keys %seen;

    In other words: get the keys that occur only once and sort them numerically.

    — Arien

      A hash's keys are always unique.

      #!/usr/bin/perl -w use strict; my %hash = ( key1 => '1', key1 => '2' ); print join("\n", keys %hash);

      prints 'key1' only...

Re: Sorting a hash. What am I doing wrong?
by waswas-fng (Curate) on Jan 14, 2003 at 01:00 UTC
    while (<DATA>){ chomp; ($itemone) = split/,/; #split at comma $seen{$itemone}++; } foreach my $element (sort { $a <=> $b} keys %seen){ print "$element\n"; } exit; __DATA__ 1,ABC,X,1.203000e+02 1,ABC,Y,7.830000e+00 2,DEF,X,1.212400e+02 2,DEF,Y,8.810000e+00 3,GHI,X,1.180700e+02 3,GHI,Y,8.550000e+00 5,JKL,X,1.193500e+02 5,JKL,Y,7.270000e+00
    you forgot to look at the keys for the seen hash, also look how I used $_ those chomp lines etc were real ugly.

    -Waswas
Re: Sorting a hash. What am I doing wrong?
by Anonymous Monk on Jan 14, 2003 at 00:44 UTC
    You need:
    @uniq = sort {$a <=> $b } keys %seen;
    You already have key values, and you need to sort them, not the items they refer to in the hash.