Dynamic check boxes from two arrays

B

Blnukem

Hi All
Could someone please look at this and tell me why I cant get it to work
please.

#!/usr/bin/perl

print "Content-type: text/html\n\n";

my @DaysPlayedCheckBox;

my @GamingDays = qw(Sun Mon Tue Wed Thur Fri Sat);
my @DaysPlayerPlays = qw(Mon Tue Wed Sat);

my $Count = 0;
foreach my $DaysPlayed (@GamingDays) {

if ($DaysPlayed eq $DaysPlayerPlays[$Count]){
$xdata = "<input type=\"checkbox\" name=\"$DaysPlayed\"
value=\"$DaysPlayed\" checked>$DaysPlayed";
}else{
$xdata = "<input type=\"checkbox\" name=\"$DaysPlayed\"
value=\"$DaysPlayed\">$DaysPlayed";
}
push (@DaysPlayedCheckBox, $xdata);
$Count ++;
}

print @DaysPlayedCheckBox;



Thanks in adavnce
Blnukem
 
G

Gunnar Hjalmarsson

Blnukem said:
Could someone please look at this and tell me why I cant get it to
work please.

Please ask questions that make sense!!
- What did you expect the code to do?
- What does it do?

Please use indenting for better readability.

Assuming that you wonder why no checkboxes get checked, this is a
suggestion (the changes make the $Count variable redundant):
my @DaysPlayerPlays = qw(Mon Tue Wed Sat);

Instead of that you may want a hash:

my %DaysPlayerPlays;
@DaysPlayerPlays{qw(Mon Tue Wed Sat)} = ();
if ($DaysPlayed eq $DaysPlayerPlays[$Count]){

Instead of that you may want:

my $xdata;
if (exists $DaysPlayerPlays{$DaysPlayed}){

The problem is dealt with in the FAQ:

perldoc -q certain
 
B

Bigus

Blnukem said:
Hi All
Could someone please look at this and tell me why I cant get it to work
please.

#!/usr/bin/perl

print "Content-type: text/html\n\n";

my @DaysPlayedCheckBox;

my @GamingDays = qw(Sun Mon Tue Wed Thur Fri Sat);
my @DaysPlayerPlays = qw(Mon Tue Wed Sat);

my $Count = 0;
foreach my $DaysPlayed (@GamingDays) {

if ($DaysPlayed eq $DaysPlayerPlays[$Count]){
$xdata = "<input type=\"checkbox\" name=\"$DaysPlayed\"
value=\"$DaysPlayed\" checked>$DaysPlayed";
}else{
$xdata = "<input type=\"checkbox\" name=\"$DaysPlayed\"
value=\"$DaysPlayed\">$DaysPlayed";
}
push (@DaysPlayedCheckBox, $xdata);
$Count ++;
}

print @DaysPlayedCheckBox;

Follow the foreach through. You are saying:

if ($DaysPlayed eq $DaysPlayerPlays[0]), which translates to - if (Sun =
Mon)
if ($DaysPlayed eq $DaysPlayerPlays[1]), which translates to - if (Mon =
Tue)
if ($DaysPlayed eq $DaysPlayerPlays[2]), which translates to - if (Tue =
Wed)
if ($DaysPlayed eq $DaysPlayerPlays[3]), which translates to - if (Wed =
Sat)
if ($DaysPlayed eq $DaysPlayerPlays[4]), which translates to - if (Thu = )
if ($DaysPlayed eq $DaysPlayerPlays[5]), which translates to - if (Fri = )
if ($DaysPlayed eq $DaysPlayerPlays[6]), which translates to - if (Sat = )

Sth like this would do what you want it to do:

foreach $DaysPlayed ( @GamingDays ) {
if ("@DaysPlayerPlays" =~ /$DaysPlayed/) {
# checked
}
else {
# not checked
}
}


Bigus
 
B

Blnukem

Bigus thanks that's exactly what I needed!


Bigus said:
Blnukem said:
Hi All
Could someone please look at this and tell me why I cant get it to work
please.

#!/usr/bin/perl

print "Content-type: text/html\n\n";

my @DaysPlayedCheckBox;

my @GamingDays = qw(Sun Mon Tue Wed Thur Fri Sat);
my @DaysPlayerPlays = qw(Mon Tue Wed Sat);

my $Count = 0;
foreach my $DaysPlayed (@GamingDays) {

if ($DaysPlayed eq $DaysPlayerPlays[$Count]){
$xdata = "<input type=\"checkbox\" name=\"$DaysPlayed\"
value=\"$DaysPlayed\" checked>$DaysPlayed";
}else{
$xdata = "<input type=\"checkbox\" name=\"$DaysPlayed\"
value=\"$DaysPlayed\">$DaysPlayed";
}
push (@DaysPlayedCheckBox, $xdata);
$Count ++;
}

print @DaysPlayedCheckBox;

Follow the foreach through. You are saying:

if ($DaysPlayed eq $DaysPlayerPlays[0]), which translates to - if (Sun =
Mon)
if ($DaysPlayed eq $DaysPlayerPlays[1]), which translates to - if (Mon =
Tue)
if ($DaysPlayed eq $DaysPlayerPlays[2]), which translates to - if (Tue =
Wed)
if ($DaysPlayed eq $DaysPlayerPlays[3]), which translates to - if (Wed =
Sat)
if ($DaysPlayed eq $DaysPlayerPlays[4]), which translates to - if (Thu = )
if ($DaysPlayed eq $DaysPlayerPlays[5]), which translates to - if (Fri = )
if ($DaysPlayed eq $DaysPlayerPlays[6]), which translates to - if (Sat = )

Sth like this would do what you want it to do:

foreach $DaysPlayed ( @GamingDays ) {
if ("@DaysPlayerPlays" =~ /$DaysPlayed/) {
# checked
}
else {
# not checked
}
}


Bigus
 
B

Brian McCauley

Bigus said:
Follow the foreach through. You are saying:

if ($DaysPlayed eq $DaysPlayerPlays[0]), which translates to - if (Sun =
Mon)
if ($DaysPlayed eq $DaysPlayerPlays[1]), which translates to - if (Mon =
Tue)
if ($DaysPlayed eq $DaysPlayerPlays[2]), which translates to - if (Tue =
Wed)
if ($DaysPlayed eq $DaysPlayerPlays[3]), which translates to - if (Wed =
Sat)
if ($DaysPlayed eq $DaysPlayerPlays[4]), which translates to - if (Thu = )
if ($DaysPlayed eq $DaysPlayerPlays[5]), which translates to - if (Fri = )
if ($DaysPlayed eq $DaysPlayerPlays[6]), which translates to - if (Sat = )

Sth like this would do what you want it to do:

foreach $DaysPlayed ( @GamingDays ) {
if ("@DaysPlayerPlays" =~ /$DaysPlayed/) {
# checked
}
else {
# not checked
}
}

A few points.

You should always declare all variables as lexically scoped in the
smallest applicable lexical scope unless there is a reason to do
otherwise. This is not perculliar to Perl, it applies in all
programming languges.

In this case that mens you should insert 'my' between 'foreach' and
'$DaysPlayed'.
if ("@DaysPlayerPlays" =~ /$DaysPlayed/) {

Some people would criticise the unecessary use of m// where index()
would do. But I won't - sometimes the runtime saving does not
justify the uglyness.

Some people would criticise the lack of \Q - but I suppose you know
that $DaysPlayed won't contain any regex metacharaters.

Some people would criticise the lack of exclosing delimiters but I
suppose you know that @DaysPlayerPlays won't contain any words that
contain day names as part of them.

Still I would, as a matter of good habit, say:

if (" @DaysPlayerPlays " =~ / \Q$DaysPlayed /) {

Of course, you are still assuming no spaces in eiher @DaysPlayerPlays
or $DaysPlayed.

The biggest problem is that you are not using the natural
representation for the data-type. In Perl the natural representation
of a set is a hash with keys but no data. Replacing @DaysPlayerPlays
with %DaysPlayerPlays we get:

if ( exists $DaysPlayerPlays{$DaysPlayed} ) {

Or, if you prefer, a hash where all the data items are 1.

if ( $DaysPlayerPlays{$DaysPlayed} ) {

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
B

Bigus

Brian McCauley said:
A few points.

You should always declare all variables as lexically scoped in the
smallest applicable lexical scope unless there is a reason to do
otherwise. This is not perculliar to Perl, it applies in all
programming languges.

Ooops, yes, forgot it. I've been very lazy over the past 2 years and never
used strictures or warnings.. it's never caused any problems for the types
of application I write, but for this latest project I've decided to start
being discplined (largely because I intend to make it available to others)
and it's taking some getting used to!
In this case that mens you should insert 'my' between 'foreach' and
'$DaysPlayed'.


Some people would criticise the unecessary use of m// where index()
would do. But I won't - sometimes the runtime saving does not
justify the uglyness.

Some people would criticise the lack of \Q - but I suppose you know
that $DaysPlayed won't contain any regex metacharaters.

Some people would criticise the lack of exclosing delimiters but I
suppose you know that @DaysPlayerPlays won't contain any words that
contain day names as part of them.

Indeed, and it would be easier for him to do things like make it
case-insenstive by just adding "i" instead of wrapping in lc(). I originally
was going to put \b's round $DaysPlayed too, but for a simple task like that
where he knows exactly what data he's matching there's little point. I also
thought, correct me if I'm wrong, that the more you put in the regexp the
more time it takes to process.
Still I would, as a matter of good habit, say:

if (" @DaysPlayerPlays " =~ / \Q$DaysPlayed /) {

Of course, you are still assuming no spaces in eiher @DaysPlayerPlays
or $DaysPlayed.

The biggest problem is that you are not using the natural
representation for the data-type. In Perl the natural representation
of a set is a hash with keys but no data. Replacing @DaysPlayerPlays
with %DaysPlayerPlays we get:

if ( exists $DaysPlayerPlays{$DaysPlayed} ) {

Or, if you prefer, a hash where all the data items are 1.

if ( $DaysPlayerPlays{$DaysPlayed} ) {

Yeah that would be easiest.. sometimes though the "natural" data-type is not
always the most "comfortable" to declare, eg:

He was using an array to hold the days:

my @DaysPlayerPlays = qw(Mon Tue Wed Sat);

but to declare that as a has you'd, correct me if I'm wrong, have to do sth
like this:

my %DaysPlayerPlays = (Mon=>'',Tue=>'',Wed=>'',Sat=>'');

or replace the empty quotes with an arbitary value, which seems a bit messy
to me.

Bigus
 
B

Ben Morrow

Bigus said:
Yeah that would be easiest.. sometimes though the "natural" data-type is not
always the most "comfortable" to declare, eg:

He was using an array to hold the days:

my @DaysPlayerPlays = qw(Mon Tue Wed Sat);

but to declare that as a has you'd, correct me if I'm wrong, have to do sth
like this:

my %DaysPlayerPlays = (Mon=>'',Tue=>'',Wed=>'',Sat=>'');

my %DaysPlayerPlays;
@DaysPlayerPlays{qw/Mon Tue Wed Sat/} = ();

Ben
 

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

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top