learningmanylang has asked for the wisdom of the Perl Monks concerning the following question:
I am learning the basics of Perl, after finishing learning the basics of Groovy, Python, C#, Scala and adding onto my Java this semester.
My main goal is to have a program read in a file that contains a number on each line like
1 2 3 3 2 2 1 2
and take those numbers and print them out in order with a "*" mark for every occurrence, making a histogram type program. I can post my code in Java or something else if it would help anyone understand what I am talking about.
The output I need looks like:
1:**
2:****
3:**
open(FILE, '/location/data/p4.txt') or die "Cant't open file: $!\n"; my %myhash = (); my @data = (); chomp(@data = <FILE>); foreach (@data) { %myhash = (@data => $k++); } foreach $k (sort {$a <=> %b} keys %myhash) { print "$myhash{$k} : ". '*' x $myhash{$k} . "\n"; }
This is what I have done so far from using code in another problem I made. How do I up the count? It doesn't seem like my loop is working properly on this problem either.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Learning Perl, Hashes Help
by toolic (Bishop) on Apr 29, 2014 at 00:56 UTC | |
by learningmanylang (Initiate) on Apr 29, 2014 at 01:05 UTC | |
by soonix (Chancellor) on Apr 29, 2014 at 08:19 UTC | |
|
Re: Learning Perl, Hashes Help
by Jim (Curate) on Apr 29, 2014 at 01:33 UTC |