Really Stumped hour routine

B

blnukem

Hi All

I'm really stumped on this routine that I'm trying to create and don't have
nothing to show If someone could help me here it would be appreciated What
I'm tying to do is get our store hours that will get inputted via webform in
this format:

$OpenMonday = "9:30 AM";
$CloseMonday = "6:00 PM";

$OpenTuesday = "9:30 AM";
$CloseTuesday = "6:00 PM";

$OpenWensday = "9:30 AM";
$CloseWensday = "6:00 PM";

$OpenThursday = "9:30 AM";
$CloseThursday = "9:00 PM";

$OpenFriday = "9:30 AM";
$CloseFriday = "6:00 PM";

$OpenSaturday = "9:30 AM";
$CloseSaturday = "4:00 PM";

$OpenSunday = "CLOSED";
$CloseSunday = "CLOSED";

To create a output of:

Mon - Tue - Wed - Fri : 9:30 AM - 6:00 PM

Thurs: 9:30 AM - 9:00 PM

Sat: 9:30 AM - 4:00 PM

Sun: CLOSED

If you can help me thanks in advance
Blnukem
 
G

gnari

blnukem said:
I'm really stumped on this routine that I'm trying to create and don't have
nothing to show If someone could help me here it would be appreciated What
I'm tying to do is get our store hours that will get inputted via webform in
this format:

$OpenMonday = "9:30 AM";
$CloseMonday = "6:00 PM";

$OpenTuesday = "9:30 AM";
$CloseTuesday = "6:00 PM";
...
$OpenSunday = "CLOSED";
$CloseSunday = "CLOSED";

now first, this stinks. store this kind of data in arrays or hashes.
something like:
my %hours=(
Mon=>["9:30 AM","6:00 PM"],
Tues=>["9:30 AM","6:00 PM"],
...
Sun=>["CLOSED"]
);
...
To create a output of:
Mon - Tue - Wed - Fri : 9:30 AM - 6:00 PM
Thurs: 9:30 AM - 9:00 PM
Sat: 9:30 AM - 4:00 PM
Sun: CLOSED

collect hour-ranges with something like:
my %ranges;
for my $day (qw(Mon Tue ... Sun)) {
my $range=join(' - ',@{$hours{$day}});
push @{$ranges{$range}},$day;
}

now output list:
for my $range (keys %ranges) {
print join(' - ', @{$ranges{$range}}) , " : $range\n";
}

I will leave it to you to solve the sorting of the output.

gnari
 
J

John J. Trammell

I'm really stumped on this routine that I'm trying to create and don't have
nothing to show If someone could help me here it would be appreciated What
I'm tying to do is get our store hours that will get inputted via webform in
this format:

$OpenMonday = "9:30 AM";
$CloseMonday = "6:00 PM";

$OpenTuesday = "9:30 AM";
$CloseTuesday = "6:00 PM";
[snip]

Not the most flexible data format, I'd say!

How about this:

my @days = qw[ mon tue wed thu fri sat sun ];

my %hours = ( # much more flexible
mon => "$OpenMonday - $CloseMonday",
tue => "$OpenTuesday - $CloseTuesday",
wed => "$OpenWensday - $CloseWensday",
thu => "$OpenThursday - $CloseThursday",
fri => "$OpenFriday - $CloseFriday",
sat => "$OpenSaturday - $CloseSaturday",
sun => "$OpenSunday - $CloseSunday",
);

my %seen;
my @equiv;

# there's a slicker way to do this, but it's not as transparent

for my $d (@days) {
next if $seen{ $hours{$d} };
my @e = grep $hours{$_} eq $hours{$d}, @days;
$seen{ $hours{$d} } = 1;
push @equiv, \@e;
}

print "@$_ : $hours{ $_->[0] }" for @equiv
 
J

John J. Trammell

On Sat, 17 Jan 2004 14:46:07 +0000 (UTC), John J. Trammell wrote:
[snip]
print "@$_ : $hours{ $_->[0] }" for @equiv

OK, my cut-n-paste missed the trailing ';', and I had -l turned on
when I wrote that. This line may be better for you:

print "@$_ : $hours{ $_->[0] }\n" for @equiv;
 
A

A. Sinan Unur

Hi All

I'm really stumped on this routine that I'm trying to create and don't
have nothing to show If someone could help me here it would be
appreciated What I'm tying to do is get our store hours that will get
inputted via webform in this format:

$OpenMonday = "9:30 AM";
$CloseMonday = "6:00 PM";

.... so on and so forth.
To create a output of:
Mon - Tue - Wed - Fri : 9:30 AM - 6:00 PM
Thurs: 9:30 AM - 9:00 PM
Sat: 9:30 AM - 4:00 PM
Sun: CLOSED

C:\Home> cat hours.pl

#! perl

use strict;
use warnings;

# There is no point in having two variables per day. I
# do not know where the data come from so I am going to
# refrain from making specific suggestions but do read
# everything that has to do with data structures in
# perldoc perltoc

my %days = (
Mon => ['9:30 AM', '6:00 PM'],
Tue => ['9:30 AM', '6:00 PM'],
Wed => ['9:30 AM', '6:00 PM'],
Thurs => ['9:30 AM', '9:00 PM'],
Fri => ['9:30 AM', '6:00 PM'],
Sat => ['9:30 AM', '4:00 PM'],
Sun => ['CLOSED'],
);

my %hours;
foreach ( keys %days ) {
$hours{join(' - ', @{$days{$_}})}{$_} = 1;
}

my %output;
foreach my $h ( keys %hours ) {
my $d = join(' - ', sort cmp_day keys %{$hours{$h}});
$output{$d} = $h;
}

print "Store Hours\n";
foreach ( sort cmp_day keys %output ) {
print "$_ : ${output{$_}}\n";
}

sub cmp_day {
my %days = (mon => 1, tue => 2, wed => 3,
thu => 4, fri => 5, sat => 6, sun => 7,
);
return ($days{lc(substr($a, 0, 3))}
cmp $days{lc(substr($b, 0, 3))}
);
};

C:\Home\> hours.pl
Store Hours
Mon - Tue - Wed - Fri : 9:30 AM - 6:00 PM
Thurs : 9:30 AM - 9:00 PM
Sat : 9:30 AM - 4:00 PM
Sun : CLOSED
 
J

Jay Tilton

: I'm really stumped on this routine that I'm trying to create and don't have
: nothing to show If someone could help me here it would be appreciated What
: I'm tying to do is get our store hours that will get inputted via webform in
: this format:

Gah! Try punctuating sentences in future.

: $OpenMonday = "9:30 AM";
: $CloseMonday = "6:00 PM";
: $OpenTuesday = "9:30 AM";
: $CloseTuesday = "6:00 PM";
: $OpenWensday = "9:30 AM";
^^^^^^^
"Wednesday"

: $CloseWensday = "6:00 PM";
: $OpenThursday = "9:30 AM";
: $CloseThursday = "9:00 PM";
: $OpenFriday = "9:30 AM";
: $CloseFriday = "6:00 PM";
: $OpenSaturday = "9:30 AM";
: $CloseSaturday = "4:00 PM";
: $OpenSunday = "CLOSED";
: $CloseSunday = "CLOSED";

You've got to be kidding. Fourteen independent scalars?
Wrap them up into a decent data structure and the program will
practically write itself.

: To create a output of:
:
: Mon - Tue - Wed - Fri : 9:30 AM - 6:00 PM
:
: Thurs: 9:30 AM - 9:00 PM
:
: Sat: 9:30 AM - 4:00 PM
:
: Sun: CLOSED
:
: If you can help me thanks in advance

Eh. It's an interesting diversion this morning.

my @h =
map
[$$_[0], $$_[1] eq $$_[2] ? $$_[1] : "$$_[1] - $$_[2]"],
(
[Mon => $OpenMonday , $CloseMonday ],
[Tue => $OpenTuesday , $CloseTuesday ],
[Wed => $OpenWensday , $CloseWensday ],
[Thurs => $OpenThursday, $CloseThursday],
[Fri => $OpenFriday , $CloseFriday ],
[Sat => $OpenSaturday, $CloseSaturday],
[Sun => $OpenSunday , $CloseSunday ],
);

while( @h ) {
my $d = shift @h;
@h = grep
$$d[1] ne $$_[1] || ($$d[0] .= " - $$_[0]") x0 ,
@h;
local $" = ' : ';
print "@$d\n\n";
}
 
B

Bruce Horrocks

Jay Tilton said:
You've got to be kidding. Fourteen independent scalars? Wrap them up
into a decent data structure and the program will practically write
itself.

This is the fourth post I've read in this thread criticising the OP's
data structure. Well: webform, webform, webform, webform. And just in
case you didn't get that, I'll say it again: webform.

The OP said that his data came from a *web-form*. Therefore the data
will always arrive as separate scalars from the CGI param call.

I don't disagree that the first step is to transfer them into something
more amenable to processing but that is part of the solution, not
something to criticise the OP over.

Regards,
 
G

Gunnar Hjalmarsson

Bruce said:
The OP said that his data came from a *web-form*. Therefore the
data will always arrive as separate scalars from the CGI param
call.

Why on earth would you need to store the CGI data in scalar variables?
You'd better take a look at the CGI.pm docs in order to study
alternatives.

For instance, by just doing:

my %params = $q->Vars;

you store all the form data in %params.
 
G

gnari

Bruce Horrocks said:
This is the fourth post I've read in this thread criticising the OP's
data structure. Well: webform, webform, webform, webform. And just in
case you didn't get that, I'll say it again: webform.

The OP said that his data came from a *web-form*. Therefore the data
will always arrive as separate scalars from the CGI param call.

not really.
I don't disagree that the first step is to transfer them into something
more amenable to processing ...

and the OP put them into 14 scalars
... but that is part of the solution, not
something to criticise the OP over.

well, it seems to me like the OP main problem was that he
was stuck with his 14 scalars.
Too many programmers neglect the art of selecting a
proper data structure. as Jay was pointing out, the proper
data structure is often the key to a simple solution.

anyways, I do not think anyone was rude to the OP.

gnari
 
S

Sherm Pendley

The OP said that his data came from a *web-form*. Therefore the data
will always arrive as separate scalars from the CGI param call.

Not true - the Vars() method returns a reference to a hash of the form
data.

sherm--
 
B

Ben Morrow

gnari said:
Too many programmers neglect the art of selecting a
proper data structure. as Jay was pointing out, the proper
data structure is often the key to a simple solution.

cf. http://www.catb.org/~esr/writings/taoup/html/ch01s06.html#rule5

Rule 5. Data dominates. If you've chosen the right data structures and
organized things well, the algorithms will almost always be
self-evident. Data structures, not algorithms, are central to
programming.

Ben
 
W

Walter Roberson

:Rule 5. Data dominates. If you've chosen the right data structures and
: organized things well, the algorithms will almost always be
: self-evident. Data structures, not algorithms, are central to
: programming.

On the other hand, there are cases where there are no known
simple (or efficient) algorithms. Global minimization of a
function not expressible as finite polynomials, for example.
(imagine, for example, a function of 9000 variables involving
sine and e^x.)
 
A

A. Sinan Unur

This is the fourth post I've read in this thread criticising the OP's
data structure. Well: webform, webform, webform, webform. And just in
case you didn't get that, I'll say it again: webform.

The OP said that his data came from a *web-form*. Therefore the data
will always arrive as separate scalars from the CGI param call.

Hmmm ... I guess I'll say again ... hmmmmmmmmmm ... hmmmm ...

#! C:/Perl/bin/perl.exe

use strict;
use warnings;

$| = 1;

use CGI;

my $q = CGI->new();

if($q->param('submit')) {
process_form($q);
} else {
show_form($q);
}

sub process_form {
my ($q) = @_;
my %days;
foreach (qw/Mon Tue Wed Thurs Fri Sat Sun/) {
my @hours = $q->param($_);
$days{$_} = \@hours;
}
# Now do the rest of the stuff. Replace the code below
# code posted here previously to create the desired output
# using %days
print $q->header;
use Data::Dumper;
print $q->start_html, $q->pre(Dumper(\%days)), $q->end_html;
}

sub show_form {
my ($q) = @_;
print $q->header;
print <<HTML;
<html>
<body>
<h1>Hours</h1>
<form>
<table border="0">
<tr>
<td>Monday: </td>
<td><input name="Mon" type="text" size="10"></td>
<td><input name="Mon" type="text" size="10"></td>
</tr>
<tr>
<td>Tuesday: </td>
<td><input name="Tue" type="text" size="10"></td>
<td><input name="Tue" type="text" size="10"></td>
</tr>
<tr>
<td>Wednesday: </td>
<td><input name="Wed" type="text" size="10"></td>
<td><input name="Wed" type="text" size="10"></td>
</tr>
<tr>
<td>Thursday: </td>
<td><input name="Thurs" type="text" size="10"></td>
<td><input name="Thurs" type="text" size="10"></td>
</tr>
<tr>
<td>Friday: </td>
<td><input name="Fri" type="text" size="10"></td>
<td><input name="Fri" type="text" size="10"></td>
</tr>
<tr>
<td>Saturday: </td>
<td><input name="Sat" type="text" size="10"></td>
<td><input name="Sat" type="text" size="10"></td>
</tr>
<tr>
<td>Sunday: </td>
<td><input name="Sun" type="text" size="10"></td>
<td><input name="Sun" type="text" size="10"></td>
</tr>
<tr>
<td></td>
<td><input name="submit" type="submit"></td>
<td><input name="reset" type="reset"></td>
</tr>
</table>
HTML
}
 
A

A. Sinan Unur

sub show_form {
my ($q) = @_;
print $q->header;
print <<HTML;
<html>
<body>
...
HTML
}

Just correcting myself as usual. Forgot

</table>
</form>
</html>

before the end of the here doc above.

Sinan.
 
B

Bruce Horrocks

Gunnar said:
Why on earth would you need to store the CGI data in scalar variables?
You'd better take a look at the CGI.pm docs in order to study
alternatives.

For instance, by just doing:

my %params = $q->Vars;

you store all the form data in %params.

Of course, but that is not the point since it doesn't change the way the
data is laid out on the web-form.

You are criticising the original poster for not using a data structure
that simplifies the processing required when he didn't know how to do
the processing in the first place. That is hardly fair. And when I point
it out to the group, you come and criticise me as well. Don't shoot the
messenger.

Regards,
 
G

Gunnar Hjalmarsson

Of course, but that is not the point since it doesn't change the
way the data is laid out on the web-form.

Maybe I misunderstood you, Bruce, but I don't understand that either.
Could you possible explain with a simple example?
You are criticising the original poster for not using a data
structure that simplifies the processing required when he didn't
know how to do the processing in the first place. That is hardly
fair. And when I point it out to the group, you come and criticise
me as well. Don't shoot the messenger.

Well, to be honest, I hadn't even read the whole thread, so I'm not
criticizing OP. But your post, in which you claimed that form data
"will always arrive as separate scalars from the CGI param call",
caught my attention since the statement seemed to be incorrect. And
that statement was yours, not anybody else's, so I did not shoot the
messenger. I just shot you. ;-)
 
B

Bruce Horrocks

Gunnar said:
Well, to be honest, I hadn't even read the whole thread, so I'm not
criticizing OP. But your post, in which you claimed that form data
"will always arrive as separate scalars from the CGI param call",
caught my attention since the statement seemed to be incorrect. And
that statement was yours, not anybody else's, so I did not shoot the
messenger. I just shot you. ;-)

The OP's data was presented as:
$OpenMonday = "9:30 AM";
$CloseMonday = "6:00 PM";

$OpenTuesday = "9:30 AM";
$CloseTuesday = "6:00 PM";
etc for each day of the week.

It wasn't stated but presumably these variables reflect the names of the
fields on the webform and presumably they were retrieved with code
something like:

$OpenMonday = param('OpenMonday');
$CloseMonday = param('CloseMonday');
etc.

He can use the $q->Vars() method that you mention but to use the values
he still has to do something like:

$params = $q->Vars;
print $params->{'OpenMonday'};
print $params->{'CloseMonday'};
etc

This is just as clumsy as the original which is the point I was getting
at when I said that the data still comes as scalars.

I felt that the OP presented the data relating to his problem in a
simple and easy to understand way in order to make it easy for the
readers of this group to give him an answer, but all he got for it was
criticism. This irked me.

Regards,
 
A

A. Sinan Unur

$params = $q->Vars;
print $params->{'OpenMonday'};
print $params->{'CloseMonday'};
etc

This is just as clumsy as the original which is the point I was getting
at when I said that the data still comes as scalars.

But the data do not need to come in as scalars. See my other post.
I felt that the OP presented the data relating to his problem in a
simple and easy to understand way in order to make it easy for the
readers of this group to give him an answer, but all he got for it was
criticism. This irked me.

The OP's 'simple and easy to understand' variables completely obscured
straightforward ways of processing the data. This mistake was pointed out
as a benefit to the OP and others reading the forum. I can only presume you
are getting touchy because you are guilty of the same sins.

Your insistence that there is an untouchable *web-form* (what the ... is a
web-form anyway?) which cannot be redone to send data in a more convenient
way is pointless (see [email protected]).

Sinan.
 
G

gnari

Bruce Horrocks said:
He can use the $q->Vars() method that you mention but to use the values
he still has to do something like:

$params = $q->Vars;
print $params->{'OpenMonday'};
print $params->{'CloseMonday'};
etc

This is just as clumsy as the original which is the point I was getting
at when I said that the data still comes as scalars.

you still dont get it
there would of course be no point in reading the params into
a hash or array structure if you still access them as scalars one
by one. as soon as you have the values in a sensible structures,
many ways suggest themselves to solve the problem.

the $q->Vars solution is not the only one. did you see the
proposal by A. Sinan Unur in where the form values are read directly into a suitable
hash of arrays structure?

gnari
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Help with code 0
Help with my responsive home page 2
12 hour clock and offset problem 25
Tasks 1
Help with python code! 18
Java Problems (Really Need Help!) 2
help me please 2
Help with Hpricot and collect 0

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top