ear,
Beyond the sagely advice already provided, I wanted to mention a few things for further consideration.

First, your hash keys make it difficult to sort on them chronologically which presumably you would want to do:

for my $week ( map { $_->[0] } sort { $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] } map { [$_, split /\./] } keys %IssuesByWeek ) { # ... }
If you can, it would be better to use a ISO date string that can be sorted and will only require conversion one time and not every time you need to print results:
for my $key ( keys %IssuesByWeek ) { # New key in format of YYYY-MM-DD $IssuesByWeek{ get_sunday( $key ) } = delete $IssuesByWeek{ $key } +; }
If you can't do that, then perhaps you could change your data structure to something like $data{year}{week}{issue} = $count; then it will be much easier sorting.

The second thing is that it is generally a waste to sort a list to find the top or bottom of the list. There are routines known as water-mark algorithms that do this much more efficiently. Instead of sorting the entire list and then throwing away most of the work, I would suggest using something similar to:

#!/usr/bin/perl use strict; use warnings; my @list = map { int(rand 1000) + 1 } 1 .. 50; print "Top 5:\n"; print "\t$_\n" for top_x(\@list, 5); sub top_x { my ($list, $x) = @_; $x--; my @top; $#top = $x; for my $item ( @$list ) { next if defined $top[ -1 ] && $item <= $top[ -1 ]; for my $id ( 0 .. $#top ) { $top[ $id ] = $item and last if ! defined $top[ $id ]; if ( $item > $top[ $id ] ) { @top[ $id .. $#top ] = ($item, @top[ $id .. $#top - 1] +); #splice(@top, $id, 0, $item), pop @top; last; } } } return @top; }
Note that the commented splice will effectively do the same thing as the array slice. Depending on the size of the array, it may or may not be more efficient.

Cheers - L~R


In reply to Re: Convert Week Of Year to Date by Limbic~Region
in thread Convert Week Of Year to Date by ear

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.