Dynamic check boxes from two arrays

Discussion in 'Perl Misc' started by Blnukem, Nov 30, 2003.

  1. Blnukem

    Blnukem Guest

    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
     
    Blnukem, Nov 30, 2003
    #1
    1. Advertising

  2. Blnukem wrote:
    > 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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 30, 2003
    #2
    1. Advertising

  3. Blnukem

    Bigus Guest

    "Blnukem" <> wrote in message
    news:9akyb.26771$...
    > 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
     
    Bigus, Nov 30, 2003
    #3
  4. Blnukem

    Blnukem Guest

    Bigus thanks that's exactly what I needed!


    "Bigus" <> wrote in message
    news:G_kyb.2110$...
    >
    > "Blnukem" <> wrote in message
    > news:9akyb.26771$...
    > > 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
    >
    >
    >
     
    Blnukem, Nov 30, 2003
    #4
  5. "Bigus" <> writes:

    > 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\\
     
    Brian McCauley, Dec 1, 2003
    #5
  6. Blnukem

    Bigus Guest

    "Brian McCauley" <> wrote in message
    news:...
    > "Bigus" <> writes:

    [..]
    > > 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.


    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'.
    >
    > > 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.


    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
     
    Bigus, Dec 1, 2003
    #6
  7. Blnukem

    Ben Morrow Guest

    "Bigus" <> wrote:
    > > 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=>'');


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

    Ben

    --
    $.=1;*g=sub{print@_};sub r($$\$){my($w,$x,$y)=@_;for(keys%$x){/main/&&next;*p=$
    $x{$_};/(\w)::$/&&(r($w.$1,$x.$_,$y),next);$y eq\$p&&&g("$w$_")}};sub t{for(@_)
    {$f&&($_||&g(" "));$f=1;r"","::",$_;$_&&&g(chr(0012))}};t #
    $J::u::s::t, $a::n::eek:::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $.
     
    Ben Morrow, Dec 1, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kris
    Replies:
    0
    Views:
    525
  2. kris
    Replies:
    0
    Views:
    489
  3. kris
    Replies:
    1
    Views:
    5,250
  4. Kev Jackson
    Replies:
    2
    Views:
    131
  5. Suresh
    Replies:
    2
    Views:
    408
    Suresh
    Apr 22, 2004
Loading...

Share This Page