On Tue, 12 Aug 2003, steve silvers wrote: > I don't understand why the output of the below snippet kind of blows up.. > > --TEXT FILE test.txt-- > > 03|04|09|14|15|18|24|27 > 09|23|24|26|27|28|33|35 > 10|11|13|15|17|18|19|22 > 07|08|13|17|22|23|24|25 > 03|06|07|08|11|12|16|17 > 02|05|06|09|12|18|19|22 > > --SCRIPT > > #!Perl -w > > use strict; > use vars qw(%counts $numbers @numbers); > > open(FILE,"test.txt") || die "Can't open file $^E"; > while(<FILE>) { > push(@numbers, split(/\|/,$_)); > } > close(FILE); > > # --------------------- > # Get Count.. > > $counts{$_}++ foreach @numbers; > > foreach (sort { $counts{$b} <=> $counts{$a} } keys %counts) { > print qq( $_ - $counts{$_} \n); > } > > > -- RESULTS -- > > 18 - 3 > 24 - 3 > 09 - 3 > 07 - 2 > 23 - 2 > 15 - 2 > 08 - 2 > 17 - 2 > 19 - 2 > 03 - 2 > 11 - 2 > 12 - 2 > 13 - 2 > 06 - 2 > 22 - 2 > 22 > - 1 > 25 > - 1 > 17 > - 1 > 35 > - 1 > 27 > - 1 > 02 - 1 > 05 - 1 > 16 - 1 > 33 - 1 > 04 - 1 > 26 - 1 > 10 - 1 > 14 - 1 > 27 - 1 > 28 - 1 > > > Why do the last numbers as it seems is blowing out and not keep a nice form > like the rest of the results? > It seems likes it's just the last numbers? > > 22 > - 1 > 25 > - 1 > 17 > - 1 > 35 > - 1 > 27 > - 1 >
It's because you aren't chomp'ing the lines when you read them. Note that each number with a problem is the the last one on its line, i.e. you are not using 27 you are using the value of "27\n". Note for example in your output 27 occurs twice. This is not possible for a hash key. But you have two separate hash keys, "27" and "27\n". **** [EMAIL PROTECTED] <Carl Jolley> **** All opinions are my own and not necessarily those of my employer **** _______________________________________________ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs