Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Checking for blank string

by Himaja (Novice)
on Sep 26, 2016 at 07:57 UTC ( [id://1172602]=perlquestion: print w/replies, xml ) Need Help??

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

I have a hash with uninitialised as well as initialsed strings. eg: 'A1'=>'Default' 'A2' => '' How do I detect an uninitialised string? I have tried things like if(defined $str) and next if ($str eq undef). TIA

Replies are listed 'Best First'.
Re: Checking for blank string
by choroba (Cardinal) on Sep 26, 2016 at 08:00 UTC
    What do you mean by "initialised"?

    In your example, it seems the value is the empty string , which is defined. undef is undefined. To check for them, use

    # empty string defined $value && '' eq $value # undefined ! defined $value

    Check for definedness is needed in the first case, as `undef` stringifies to the empty string.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      By initialised, i mean i get a warning saying "Use of uninitialized value $data in string eq at /path with the following code:

      next if ('' eq $data)

        In that case, your $data really is undefined. I would rewrite your statement like this:

        next unless defined $data;

        TIMTOWTDI, of course.

Re: Checking for blank string
by haukex (Archbishop) on Sep 26, 2016 at 08:15 UTC

    Hi Himaja,

    Could you show us your hash with Data::Dumper? E.g. use Data::Dumper; print Dumper(\%hash);

    You check for definedness with defined, e.g. if (!defined $hash{key}). One way to check for an empty string is with length, e.g. if (!length $hash{key}), but make sure to check defined first, because length(undef) will also be false (and before Perl v5.12, will throw a warning).

    Hope this helps,
    -- Hauke D

      Hi Hauke, The below code works, but still getting the waring mentioned above. Guess I can live with it though. Thanks :)

      $VAR1 = { 'X92' => '', 'AA571' => '0.00', 'AA842' => '0.00', 'B6' => '0', 'E47' => 'SRAM (ssbw)', 'M83' => '', 'B29' => '0' } if(defined $sheet->{$cell} && length($sheet->{$cell}))
Re: Checking for blank string
by davido (Cardinal) on Sep 26, 2016 at 15:13 UTC

    my %hash = ( foo => 'bar', # Exists, Defined, Positive length, True. baz => '', # Defined, False, Zero-length, Exists. blink => undef, # Exists, Not Defined, Zero-length, False. # buzz => '' # Does not exist, Not defined (because commented out +). ); foreach my $key (qw(foo baz buzz blink)) { print "$key ", (exists $hash{$key} ? "exists.\n" : "does not exist +.\n"); print "$key is ", (defined $hash{$key} ? "defined.\n" : "not defin +ed.\n"); print "$key has a length of ", (defined $hash{$key} ? length($hash +{$key}) : "zero because it is not defined"), ".\n"; print "$key\'s value is ", (defined $hash{$key} ? $hash{$key} : 'u +ndefined'), ".\n"; print "$key\'s Boolean value is ", ($hash{$key} ? "true.\n" : "fal +se.\n"); print "\n"; }

    That will produce the following output:

    foo exists. foo is defined. foo has a length of 3. foo's value is bar. foo's Boolean value is true. baz exists. baz is defined. baz has a length of 0. baz's value is . baz's Boolean value is false. buzz does not exist. buzz is not defined. buzz has a length of zero because it is not defined. buzz's value is undefined. buzz's Boolean value is false. blink exists. blink is not defined. blink has a length of zero because it is not defined. blink's value is undefined. blink's Boolean value is false.

    So, you can test exists to see if the key exists, you can test defined to see if it has a defined value, you can test length to see if it consists of an empty string, and you can test its Boolean value (even for undefined values) for truth and falsehood. Each test has its own unique perspective. Boolean tests, for example, will return false for both 0 and empty, and undefined.

    If you know what you are looking for, Perl provides a way to look for it.


    Dave

Re: Checking for blank string
by AnomalousMonk (Archbishop) on Sep 26, 2016 at 16:56 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-23 06:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found