Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Matching Dates

by htmanning (Friar)
on Mar 22, 2020 at 01:39 UTC ( [id://11114535]=perlquestion: print w/replies, xml ) Need Help??

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

Monks, I'm a little stumped here. I am printing out a sort of list view of a calendar. I'm trying to only print the date heading once if there is more than ne reservation on that date. It would look something like this:
2020-03-21 | 2:00 pm | 3:00 pm 2020-03-22 | 2:00:pm
I am looping through, but I can't figure out how to NOT print the date again if it has already been printed. I have something like the following (this is not the actual code):

$SQL = "SELECT * from $reservations_table where approved!='declined' a +nd date > '$dateadded2' OR (date = '$dateadded2' and militaryendtime +> '$hourmin' and approved!='declined' and approved!='cancelled') orde +r by date asc, militarytime asc"; &Do_SQL; $startcount = ($currentpage - 1) * $titlesperpage + 1; $stopcount = $currentpage * $titlesperpage; $current_count = 0; while (($pointer = $sth->fetchrow_hashref) && ($current_count +<= $stopcount)){ $current_count++; if ($current_count >= $startcount && $current_count <= $st +opcount) { print <<HTML; <table border=0 cellpadding=5 cellspacing=5> <tr> <td width=100 align=center><b>$date</b></td> <td width=100 align=center>$time_lc</td> <td>-</td> <td width=100 align=center>$endtime_lc</td> <td width=100 align=center>$area</td> <td width=100 align=center>$unit</td> </tr> </table> } }

I tried to set a $lastdate = $date after the loop, then compare it on the next loop but I'm doing something wrong. Any ideas? Thanks!

Replies are listed 'Best First'.
Re: Matching Dates
by choroba (Cardinal) on Mar 22, 2020 at 02:26 UTC
    Without seeing the actual code I can't help you with the details, but the general idea is correct:
    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use enum qw( DATE TIME ); my @reservations = ([ '2020-03-21', '2:00 pm' ], [ '2020-03-21', '3:00 pm' ], [ '2020-03-22', '2:00:pm' ]); my $last_date = ""; for my $reservation (@reservations) { print $last_date eq $reservation->[DATE] ? ' ' x 10 : $reservation +->[DATE]; say ' ', $reservation->[TIME]; $last_date = $reservation->[DATE]; }
    Output:
    2020-03-21 2:00 pm 3:00 pm 2020-03-22 2:00:pm
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      I wasn't familiar with enum in perl as opposed to C, so I found this interesting to replicate and then supplement from the examples given in enum.pm:

      date is 0 time is 1 2020-03-21 2:00 pm 3:00 pm 2020-03-22 2:00:pm 6 is SIX pref is 2 date is Months_Feb 1 c is 22
      #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use enum qw( DATE TIME ); say "date is ", DATE; say "time is ", TIME; my @reservations = ( [ '2020-03-21', '2:00 pm' ], [ '2020-03-21', '3:00 pm' ], [ '2020-03-22', '2:00:pm' ] ); my $last_date = ""; for my $reservation (@reservations) { print $last_date eq $reservation->[DATE] ? ' ' x 10 : $reservation-> +[DATE]; say ' ', $reservation->[TIME]; $last_date = $reservation->[DATE]; } use enum qw(Forty=40 FortyOne Five=5 Six Seven); # Yes, you can change the start indexs at any time as in C say "6 is ", "SIX"; use enum qw(:Prefix_ One Two Three); ## Creates Prefix_One, Prefix_Two, Prefix_Three say "pref is ", Prefix_Three; use enum qw( :Months_=0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec :Days_=0 Sun Mon Tue Wed Thu Fri Sat :Letters_=20 A..Z ); say "date is Months_Feb ", Months_Feb; say "c is ", Letters_C;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-29 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found