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

@intersect = qw~732341 732336 732314 732335 732333 732368 732325 732369 732334 732332 732312 732352 732353 732347 732322 732362 732331 732363 732326 732370 732320 732365 732349 732364 732356 732342 732328 732357 732319 732313 732361 732340 732338 732323 732367 732345 732327 732360 732337 732350 732358 732343 732355 732351 732346 732366 732339 732344 732371 732321 732359 732348 732354 732330 732329 732324~;

I am getting into a tangle trying extract the days, months and years from @intersect

I would like to achieve the following:
year1 month followed by list of days
          next month followed by list of days
year2 month followed by list of days

etcetera

I was working along the lines of:
my $pname; my @booked; my $daysb; my @yeara; my @yearb; foreach my $item (@intersect) { my ($year,$month,$day) = Add_Delta_Days(1,1,1, $item - 1); if ($year == $y) { push (@yeara, $item); } elsif ( $year == ($y + 1) ) { push (@yearb, $item); } else { print 'Sorry, the search period is too long - please go back a +nd try again'; exit; } } my %seen = (); my %cmyears; my %cmmonths; my @months_in_year = (); my @days_in_month = (); my %cmdays; my %yeara = (); @yeara = sort (@yeara); foreach my $item (@yeara) { my ($year,$month,$day) = Add_Delta_Days(1,1,1, $item - 1); $cmmonths{$item} = $month; push (@months_in_year, $month); } while ( my ($key, $value) = each (%cmmonths) ) { foreach my $item (@months_in_year) { if ($item == $value) { my ($year,$month,$day) = Add_Delta_Days(1,1,1, $key - 1); push (@days_in_month, $day); } } $cmdays{$key} = \@days_in_month; }
But I cannot extract myself from this morass, which is why I now seek your kind advice.

Replies are listed 'Best First'.
Re: putting dates int year, month date
by Skeeve (Parson) on Oct 18, 2005 at 11:51 UTC
    Why not store each date in a HoHoA?
    Example
    %dates= ( 2004 => { 11 => [ 14, 15, 16 ], 12 => [ 1, 2 ], }, 2005 => { 1 => [ 4, 5, 6 ], 2 => [ 18, 20 ], }, );
    So simply pump your dates into this structur, write 3 loops to print that structure and you're done.

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
      But it's exactly that structure that I'm trying to get from the original @intersect.

      Having sorted which dates belong to a particular year, I was trying to attach the months to the year then the days to the month. The structure that you identify seems the way to go but I don't see how to achieve it.

      Thanks for your advice.
        That's fairly easy:
        # Pseudocode! foreach $date (@dates) { split $date into $year,$month,$day push(@{$dates{$year}->{$month}}, $day); }

        s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
        +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

        What about just:

        my %dates; for ( @intersect ) { my ( $year, $month, $day ) = Add_Delta_Days( 1, 1, 1, $_ - 1 ); push @{ $dates{$year}->{$month} }, $day; }

        Then you just have to sort the days.

        Update: Looks like Skeeve beat me to it. Nevermind.