Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what's up with my 'exists' clause 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
hi,

I'm trying to exclude some vars from a form collection when processing and decided i'd create an exclusion hash and then use the exists operator to evaluate.

Only it doesn't seem to be working, what am i doing wrong?

Code:
#set exclude variables
my %excl = {issuedate=>1,memno=>1,recid=>1,periodstart=>1,status=>1,periodend=>1,compdate=>1};

#set date

my @ukdate = &uk_date;
my $comp = &yank_it($ukdate[0]);
my $val = "Comp_Date ='$comp',Status='COMP'";

#get table vars
my @rs = &getSQL("mytable","*","Rec_ID=" . $cgi->param('ID'));

#get form vars
my %vars = $cgi->Vars();

print "Content-type: text/html\n\n";
#loop and set SQL string
foreach my $key (keys %{$rs[0]}){
    my $fld = lc($key);
    $fld =~ s/\_//g;

    [b]if(!exists $excl{$fld}){[/b]
           print "$fld <br>";
    }

}

All the fields in the table collection get printed, so the exists isn't matching anything?


thanks
1DMF


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
my bad , I declared the hash wrong!

should be...
Code:
my %excl = [b]([/b]issuedate=>1,memno=>1,recid=>1,periodstart=>1,status=>1,periodend=>1,compdate=>1[b])[/b];

I thought hashes were in curly braces not parentheses ?


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
references to hashes are in curly brackets, but a regular hash uses parentheses.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
keys don't need quotes if you use the comma operator => and digits don't need quotes, but maybe your comment was meant to be general and not specific to his example code.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Didn't think about it, but a little of both perhaps. I was just recalling a weird error I'd hit a long time ago when setting up some variable type to hold the months of the year (jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec) kind of thing.

Somehow, the way I was referencing / using the month value, whenever I used 'oct', the program behaved as if I was using the function oct (as in octal string). It didn't error out, but I was getting really weird errors with some date calculations. I eventually recognized that the errors were always associated with the 10th month. After putting 's across the month names, the errors went away.
 
That is also a good reason to compile all perl scripts with strict: You can't use unquoted strings.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
cheers for the input guys.

Don't worry I know about single quoting strings

And I always use
Code:
use CGI::Carp qw(fatalsToBrowser warningsToBrowser); 
use warnings;
use strict;
at the top of every script ;-)

The final code works great once i'd realised my stupid error...
Code:
#set exclude variables
my %exclude = (issuedate=>1,memno=>1,recid=>1,periodstart=>1,status=>1,periodend=>1,compdate=>1);


#set date
my @ukdate = &uk_date;
my $comp = &yank_it($ukdate[0]);
my $vals = "Comp_Date ='$comp',Status='COMP'";

#get table vars
my @rs = &getSQL("mytable","*","Rec_ID=" . $cgi->param('ID'));

#get form vars
my %vars = $cgi->Vars();

#loop and set SQL string
foreach my $key (keys %{$rs[0]}){
    my $fld = lc($key);
    $fld =~ s/\_//g;

    if(!exists $exclude{$fld}){

        my $data = $cgi->param($fld);
        $data =~ s/\'/\'\'/g;

        if($key =~ /\_Dets/ || $fld eq "declaration"){
            $vals .= ",$key = '$data'";
        }
        else{
            $vals .= ",$key = $data";
        }
    }

}

#update record
if(!&updSQL("mytable",$vals,"Rec_ID = " . $cgi->param('ID'))){
    die "Error updating RMAR, please seek support";
    exit();
}

# reload menu
&get_menu("SAVED");

I guess if i'd called the form fields the same name on the form as the DB it wouldn't even need the substitutions and case change, but I guess it's habbit, I don't like public facing stuff to match back end stuff, stupid I know!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Though this bit of code is how I got confused with the curly braces vs parentheses
Code:
my $i = 0;
my @sel;

        foreach my $key (keys %{$_}){
        #Build Select List Data
            if(substr($key,0,4) eq "Sel_" || $key eq "App_Person" || $key eq "LifeTime"){
                $i++;
                my $var = lc($key);
                $var =~ s/\_//g;
                [b]push @sel,{num=>$i,var=>$var,val=>$_->{$key}};[/b]
            }
        }
when pushing an 'on the fly' hash into an array you use curly braces, when defining a hash you use parentheses.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
That is an anonymous hash. Its basically the same thing as a reference to a hash.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
That is an anonymous hash.

Not to me it isn't, I know exactly who/what the hash is!

;-)

Thanks for your input Kevin, I take it is anonymous because it isn't represented by a variable name so
Code:
my %varname = (blah=>blah);
is not anonymous because it's called %varname , where as the other is a hash with no name!



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Thats right. [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top