Reputation: 1
I have a Perl hash like %word
. The key is the word and the value is its count. Now I want to display %word
like:
the 20 array 10 print 2
a 18 perl 8 function 1
of 12 code 5
I search and Perl format can solve this, and I learn this page perlform, but I still don't how to do it.
Upvotes: 0
Views: 352
Reputation: 954
I knew about format
and that it could be vary handy to generate nice forms... at the time we still had a world where all was monospaced...
So, I researched it a bit and found the following solution:
use strict;
use warnings;
my %word = (
the => 20,
array => 10,
print => 2,
a => 18,
perl => 8,
function => 1,
of => 12,
code => 5,
);
my @word = %word; # turn the hash into a list
format =
@<<<<<<<<<<< @>>>> @<<<<<<<<<<< @>>>> @<<<<<<<<<<< @>>>>~~
shift @word, shift @word, shift @word, shift @word, shift @word, shift @word
.
write;
The nasty problem sits in the ~~
which makes the line repeating and that for each field in the format line you do need a corresponding scalar value... In order to get those scalar values, I shifted them off from the @word
array.
There is a lot more to know about format
and write
.
Have fun!
Upvotes: 0