http://qs321.pair.com?node_id=597044


in reply to Re: help formatting output of particular loop
in thread help formatting output of particular loop

geez i do have brain damage.

what you have written i already figured out (thanks, though). but i completely forgot to add a crucial component to this problem.

if the start_time of the following day is earlier that 09:00, i want it printed with the previous day. for example, if this were the output:
id_number CA456789 2007-01-29 16:00:00 CA456712 2007-01-30 03:00:00
i would like it to be output like this:
2007-01-29 CA456789 16:00:00 CA456712 03:00:00
thanks again for you help

Replies are listed 'Best First'.
Re^3: help formatting output of particular loop
by shmem (Chancellor) on Jan 29, 2007 at 09:09 UTC
    Modify tinita's code as follows
    if ($row->{start_date} ne $last_date && (split/:/,$row->start_time)[0] + > 9) {

    update: Oops. That will output incorrect results if all entries for a day are from before 9:00. Make a hash keyed with unix timestamps of each date, push each entry onto an array for the appropriate day:

    use Time::Local; ... for my $row (@rows) { my @l = reverse split /-/, $row->{start_date}; $l[1]--; # months range is 0..11 my $key = timelocal(0,0,0,@l); my $line = join("\t", $row->{id_number}, $row->{start_time}); # wh +atever formatting if(split/:/,$row->start_time)[0] > 9) { push @{$hash{$key-86400}}, $line; } else { push @{$hash{$key}}, $line; } } foreach my $key (sort %hash) { my @l = (localtime $key)[5,4,3]; $l[1]++; # month... print join('-', @l), "\n"; print $_,"\n" for @{$hash{$key}}; }

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      AHA! Thank you so much!

      That worked...oh man, I can finally start working on fixing any reversible damage all that thinking may have inflicted on my brain!
    A reply falls below the community's threshold of quality. You may see it by logging in.