Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Can't detect Hash values that are empty

by SergioQ (Beadle)
on Dec 26, 2020 at 21:22 UTC ( #11125757=perlquestion: print w/replies, xml ) Need Help??

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

PLEASE IGNORE THIS QUESTION, I FEEL SO STUPID FOR POSTING IT. I WAS USING undef WHEN I SHOULD HAVE BEEN USING !define. Sorry

A delete question feature would be nice.

This is a new question but related to a recent post here of mine Having problems with DBI selectall_arrayref

Using the solution from my previous post, it works, and I can access both my column, and data from my MySQL table.

The data is there, but I am having problems finding empty fields. For instance the code below, loops through the hash, gives me the key(column) and value(MySQL field).

However, every item comes up as defined but empty, even though the code prints out its contents. You can see how far I've tried , i.e. moving the text from the hash to a new scalar.

my %currec; foreach my $row (@{$data_all}) { say $row; %currec = %{$row}; foreach my $columns (keys %currec) { my $text = $currec{$columns}; say $text; if(undef($text)) { say "$columns is NOT DEFINED"; } elsif (length($text) < 1) { say "$columns DEFINED, but empty"; } } exit; }

Replies are listed 'Best First'.
Re: Can't detect empty Hash from a MySQL table
by Corion (Pope) on Dec 26, 2020 at 21:35 UTC

    undef does not work the way you think it does:

    if(undef($text)) { ...

    This will always set a variable to undef. You likely want defined as in if( ! defined($text)) { ...:

    foreach my $row (@{$data_all}) { say $row; my %currec = %{$row}; foreach my $columns (keys %currec) { my $text = $currec{$columns}; say $text; if(! defined ($text)) { say "$columns is NOT DEFINED"; } elsif (length($text) < 1) { say "$columns DEFINED, but empty"; } } exit; }
Re: Can't detect Hash values that are empty
by GrandFather (Saint) on Dec 27, 2020 at 00:01 UTC

    There are very few silly questions, even if you feel shame faced afterwards. Keeping "brain fart" questions around can help newbies and others who have a momentary lapse of memory get back on track quickly and help show that we are all fallible. I'm sure I've posted some fairly sub-par questions, 02.5 == 25 - WTF? comes close. There are many silly nodes I could have posted that were caught while I was typing up the node.

    In any case, keeping your node around to help others who hit the same issue is a large part of what this site is about.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2021-02-24 20:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?