Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Validating hash values

by rodry (Beadle)
on Sep 16, 2000 at 03:05 UTC ( #32757=perlquestion: print w/replies, xml ) Need Help??

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

I have the following code for a function

sub MysqlIN { my %dateparts = ( YEAR => '', MONTH => '', DAY => '', @_, ); my $element; foreach $element (%dateparts) { if (!$dateparts{$element}) { die "Invalid date value"; exit; } my $mysqldate = join ("-", $dateparts{YEAR}, $dateparts{MONTH}, $datep +art{DAY}); return $mysqldate; }

For some reason, the test inside the foreach loop is always true. What I want to accomplish with this function is simple: verify that the arguments have number values (not done yet) and that they are not empty.

Thanks for the help.

Replies are listed 'Best First'.
Re: Validating hash values
by reptile (Monk) on Sep 16, 2000 at 03:13 UTC

    You need to change that foreach to be keys %dateparts. As it is, it's going over both the keys and the values, and when it gets to value, it's false because that key doesn't exist yet.

    foreach my $element (keys %dateparts) { ... }

    local $_ = "0A72656B636148206C72655020726568746F6E41207473754A"; while(s/..$//) { print chr(hex($&)) }

Re: Validating hash values
by tye (Sage) on Sep 16, 2000 at 03:18 UTC
    sub MysqlIN { my %dateparts= ( YEAR => '', MONTH => '', DAY => '', @_ ); my $element; foreach $element ( keys %dateparts ) { if ( 0 < $dateparts{$element} ) { die "Invalid date value"; } } my $mysqldate= join "-", @dateparts{qw(YEAR MONTH DAY)}; return $mysqldate; }

            - tye (but my friends call me "Tye")
(crazyinsomniac) Re: Validating hash values
by crazyinsomniac (Prior) on Sep 16, 2000 at 03:12 UTC
Re: Validating hash values
by geektron (Curate) on Sep 16, 2000 at 03:21 UTC
    you need a combination of two of the replies:
    foreach my $element ( keys %dateparts ) { die "No datepart!: $!" unless defined $dateparts{$element}; }

    the 'exit' is redundant. if the code dies, the program is exiting.

    i also don't think your assignment is going to work correctly. if you're passing in date values, you really should just use something like:

    my %dateparts = @_;

    assuming that your input args are in the form of:

    sub MySQLIN( YEAR => '1999', DAY => '10', MONTH => '3');

    the join will work even if the values to join are undefined.

      But if you don't initialize the hash, then passing in no arguments won't be properly detected, for example.

              - tye (but my friends call me "Tye")
(ar0n) Re: Validating hash values
by ar0n (Priest) on Sep 16, 2000 at 03:24 UTC
    tye beat me to it. see his post above. oh well, just ignore me :)

    I'd change
    my $mysqldate = join ("-", $dateparts{YEAR}, $dateparts{MONTH}, $datep +art{DAY});
    into
    my $mysqldate = join ("-", @dateparts{ qw(YEAR MONTH DAY) });
    @dateparts{qw(YEAR MONTH DAY)} is just an easier way of saying
    @dateparts{("YEAR", "MONTH", "DAY")}
    which is an easy way of saying
    ($dateparts{YEAR}, $dateparts{MONTH}, $datepart{DAY})

    -- ar0n (just another perl joe)

Re: Validating hash values
by isotope (Chaplain) on Sep 16, 2000 at 03:14 UTC
    Uhhh... maybe you'd have more success using defined and $var ne "" and the like.

    --isotope
      When testing if a hash key is defined, its better to use exists.
RE: Validating hash values
by runrig (Abbot) on Sep 16, 2000 at 03:25 UTC
    Update: Never mind, there's enough responses here already

    As mentioned before, you need to change that loop. Here is my offering:

    sub MysqlIN { # Why is that '@_' in there, are you passing in an # array of three values? # That's what I'll assume, so I'll change this line #my %dateparts = ( YEAR => '', MONTH => '', DAY => '', @_, ); # To this: my %dateparts = ( YEAR=>shift, MONTH=>shift, DAY=>shift ); my $element; while (my ($name, $value) = each %dateparts) { # Number validation added die "Invalid value for $name" unless $value and $value=~/^\d+$/; # You don't need to exit, you already died! # exit; } my $mysqldate = join ("-", @dateparts{qw(YEAR MONTH DAY)}); return $mysqldate; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2020-11-25 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?