Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: help formatting output of particular loop

by tinita (Parson)
on Jan 29, 2007 at 08:47 UTC ( [id://597041]=note: print w/replies, xml ) Need Help??


in reply to help formatting output of particular loop

untested (it seems your rows are already sorted):
my $last_date; for my $row (@rows) { if ($row->{start_date} ne $last_date) { print "$row->{start_date}\n"; $last_date = $row->{start_date}; } printf "%8s %8s", $row->{id_number}, $row->{start_time}; }

Replies are listed 'Best First'.
Re^2: help formatting output of particular loop
by eisdrache (Novice) on Jan 29, 2007 at 09:00 UTC
    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
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://597041]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-18 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found